use paste::paste;
use std::sync::OnceLock;

mod avatar_excel_config;
mod open_state_excel_config;
mod scene_excel_config;
mod world_excel_config;

pub(crate) mod tsv_util;
pub use avatar_excel_config::{AvatarData, AvatarUseType};
pub use open_state_excel_config::OpenStateData;
pub use scene_excel_config::{SceneData, SceneType};
pub use world_excel_config::WorldData;

macro_rules! tables {
    ($($table_type:ident;)*) => {
        $(paste! {
            static [<$table_type:snake:upper _VEC>]: OnceLock<Vec<$table_type>> = OnceLock::new();
        })*

        pub fn load_txt_data() -> Result<(), Box<dyn std::error::Error>> {
            $(paste! {
                let path = concat!("data/txt/", stringify!($table_type), ".txt");
                let data = std::fs::read_to_string(path)?;
                let _ = [<$table_type:snake:upper _VEC>].set(tsv_util::from_str(&data)?);
            })*

            Ok(())
        }

        $(paste! {
            pub fn [<iter_ $table_type:snake>]() -> ::std::slice::Iter<'static, $table_type> {
                [<$table_type:snake:upper _VEC>].get().unwrap().iter()
            }
        })*
    };
}

tables! {
    AvatarData;
    WorldData;
    SceneData;
    OpenStateData;
}

pub fn get_default_world_main_scene_id() -> u32 {
    iter_world_data().next().unwrap().main_scene_id
}
