use bevy::ecs::error::Result;
use bevy::prelude::*;
use bevy::render::view::RenderLayers;

use crate::consts::BACKGROUND_COLOR;
use crate::resources::{AppState, Easy, Modifiers, MonospaceFont, Mute, NumbersFont, TitleFont, SFX};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Component)]
pub struct Load;

impl Load {

    pub fn init(
        mut commands: Commands,
        assets: Res<AssetServer>,
    ) -> Result<()> {
        commands.insert_resource(ClearColor(BACKGROUND_COLOR));
        commands.insert_resource(Easy::default());
        commands.insert_resource(Modifiers::default());
        commands.insert_resource(Mute::default());
        commands.insert_resource(TitleFont::new(&assets)?);
        commands.insert_resource(NumbersFont::new(&assets)?);
        commands.insert_resource(MonospaceFont::new(&assets)?);
        commands.insert_resource(SFX::new(&assets));
        commands.spawn(Camera2d).insert(RenderLayers::layer(0));
        commands.spawn(Camera2d).insert(RenderLayers::layer(1));
        Ok(())
    }

    pub fn finished_loading(mut next_state: ResMut<NextState<AppState>>) {
        next_state.set(AppState::Title);
    }
}
