use crate::{deck::*, *};
use bevy::{prelude::*, utils::HashMap};

/// boot systems kick off the game
pub struct BootPlugin;

impl Plugin for BootPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Startup, (load, start_setup).chain());
    }
}

/// loads assets for the game
pub(crate) fn load(
    mut commands: Commands,
    server: Res<AssetServer>,
    mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
) {
    let cards: HashMap<Card, Sprite> = Deck::iter_cards()
        .map(|card| {
            let sprite_info = card.sprite();
            let texture_atlas = TextureAtlas {
                layout: texture_atlases.add(sprite_info.1),
                index: sprite_info.2 as usize,
            };
            let sprite = Sprite {
                image: server.load(sprite_info.0),
                texture_atlas: Some(texture_atlas),
                ..default()
            };

            (card, sprite)
        })
        .collect();

    let deck = Deck { cards };

    commands.insert_resource(deck);
}

/// Finish boot state by starting setup
pub(crate) fn start_setup(mut game_state: ResMut<NextState<GameState>>) {
    game_state.set(GameState::Setup);
}
