use askama::Template;
use food::Recipes;
use ingredient::Ingredient;
use pro_codes::{ProCode, ProCodeFight, ProCodeFights, ProCodes};

use crate::{
	RuntimeModule,
	common::{Game, drops::Drops, enemy::Enemy, synthesis::Synthesis},
	create_file,
};

mod food;
mod ingredient;
mod pro_codes;

const ENEMIES_PATH: &str = "./input/kh3/enemies";
const RECIPES_PATH: &str = "./input/kh3/recipes.toml";
const INGREDIENTS_PATH: &str = "./input/kh3/ingredients";
const PRO_CODES_PATH: &str = "./input/kh3/pro-codes/codes.toml";
const PRO_CODE_FIGHTS_PATH: &str = "./input/kh3/pro-codes/fights.toml";
const SYNTHESIS_PATH: &str = "./input/kh3/synthesis.toml";

#[derive(Template)]
#[template(path = "pages/kh3/drops.html")]
struct DropsTemplate {
	pub data: Drops,
}

#[derive(Template)]
#[template(path = "pages/kh3/food-sim.html")]
struct RecipesTemplate {
	pub recipes: Recipes,
}

#[derive(Template)]
#[template(path = "pages/kh3/ingredients.html")]
struct IngredientsTemplate {
	pub ingredients: Vec<Ingredient>,
}

#[derive(Template)]
#[template(path = "pages/kh3/pro-codes-sim.html")]
struct ProCodesTemplate {
	pub pro_codes: Vec<ProCode>,
	pub fights: Vec<ProCodeFight>,
}

#[derive(Template)]
#[template(path = "pages/kh3/synth.html")]
struct SynthTemplate {
	pub data: Synthesis,
}

pub struct Module;

impl RuntimeModule for Module {
	fn start_module() {
		tracing::info!("Loading KH3 enemy data from {}", ENEMIES_PATH);
		let enemies = Enemy::import(ENEMIES_PATH);
		let drops = Drops::new(Game::Kh3, enemies);

		tracing::info!("Loading KH3 ingredients data from {}", ENEMIES_PATH);
		let ingredients = Ingredient::import(INGREDIENTS_PATH);

		tracing::info!("Loading KH3 recipes data from {}", RECIPES_PATH);
		let recipes_str = std::fs::read_to_string(RECIPES_PATH).unwrap();
		let recipes = toml::from_str::<Recipes>(&recipes_str).unwrap();

		tracing::info!("Loading KH3 pro codes data from {}", PRO_CODES_PATH);
		let pro_codes_str = std::fs::read_to_string(PRO_CODES_PATH).unwrap();
		let pro_codes = toml::from_str::<ProCodes>(&pro_codes_str).unwrap();
		let pro_code_fights_str = std::fs::read_to_string(PRO_CODE_FIGHTS_PATH).unwrap();
		let pro_code_fights = toml::from_str::<ProCodeFights>(&pro_code_fights_str).unwrap();

		tracing::info!("Loading KH3 synthesis data from {}", SYNTHESIS_PATH);
		let synth = Synthesis::new(SYNTHESIS_PATH);

		tracing::info!("Generating KH3 drops template");
		let drops_template = DropsTemplate { data: drops };
		create_file("./out/kh3", "drops", drops_template).unwrap();

		tracing::info!("Generating KH3 ingredients template");
		let ingredients_template = IngredientsTemplate { ingredients };
		create_file("./out/kh3", "ingredients", ingredients_template).unwrap();

		tracing::info!("Generating KH3 recipes template");
		let food_template = RecipesTemplate { recipes };
		create_file("./out/kh3", "food-sim", food_template).unwrap();

		tracing::info!("Generating KH3 pro codes template");
		let pro_codes_template = ProCodesTemplate {
			pro_codes: pro_codes.codes,
			fights: pro_code_fights.fights,
		};
		create_file("./out/kh3", "pro-codes-sim", pro_codes_template).unwrap();

		tracing::info!("Generating KH3 synth template");
		let synth_template = SynthTemplate { data: synth };
		create_file("./out/kh3", "synth", synth_template).unwrap();
	}
}
