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

use crate::GameState;
use bevy_asset_loader::prelude::*;
// use bevy_egui::{EguiContexts, egui, egui::ProgressBar};
use bevy_kira_audio::{AudioPlugin, AudioSource};
// use iyes_progress::{ProgressCounter, ProgressPlugin};

/// Loads resources and assets for the game.
/// See assets/main.assets.ron for the actual paths used.
pub(super) fn plugin(app: &mut App) {
    app.add_plugins(AudioPlugin).add_loading_state(
        LoadingState::new(GameState::Loading)
            .continue_to_state(GameState::Menu)
            .with_dynamic_assets_file::<StandardDynamicAssetCollection>("main.assets.ron")
            .load_collection::<ImageAssets>()
            .load_collection::<FlashlightAssets>()
            .load_collection::<GltfAssets>()
            .load_collection::<AudioAssets>(),
        // .load_collection::<TextureAssets>()
        // .load_collection::<GrassAssets>()
        // .load_collection::<ConfigAssets>(),
    );
}

// the following asset collections will be loaded during the State `GameState::InitialLoading`
// when done loading, they will be inserted as resources (see <https://github.com/NiklasEi/bevy_asset_loader>)

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct AudioAssets {
    #[asset(key = "flashlight_click")]
    pub(crate) flash_click: Handle<AudioSource>,
}

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct GltfAssets {
    #[asset(key = "library", collection(typed, mapped))]
    pub(crate) library: HashMap<String, Handle<Gltf>>,
    #[asset(key = "id_card")]
    pub(crate) card: Handle<Gltf>,
}

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct TextureAssets {}

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct GrassAssets {}

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct ConfigAssets {}

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct ImageAssets {
    #[asset(key = "lebron")]
    pub(crate) king: Handle<Image>,
    #[asset(key = "id_card_toolbar")]
    pub(crate) id_card: Handle<Image>,
}

#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct FlashlightAssets {
    #[asset(key = "flash_hold_4")]
    pub(crate) flash_hold_4: Handle<Image>,
    #[asset(key = "flash_hold_4_pressed")]
    pub(crate) flash_hold_4_pressed: Handle<Image>,
}
