//   ▄████████▄      +        ███ +  ▄█████████ ███     +
//  ███▀    ▀███ +         +  ███    ███▀  +    ███ +       +
//  ███  +   ███ ███   ███ █████████ ███        ███  ███   ███
//  ███     +███ ███   ███    ███    ███▐██████ ███  ███   ███
//  ███ +    ███ ███+  ███   +███    ███     +  ███  ███ + ███
//  ███▄    ▄███ ███▄  ███    ███ +  ███  +     ███  ███▄  ███
//   ▀████████▀ + ▀███████    ███▄   ███▄       ▀████ ▀███████
//       +                  +                +             ███
//  +   ▀████████████████████████████████████████████████████▀

use blend::Blend;
use std::fs::File;
use std::io::Write;

fn main() -> std::io::Result<()> {
    let target = std::env::var("TARGET").unwrap();
    if target.contains("windows") {
        println!("cargo:warning=Embedding Windows Icon");
        embed_resource::compile("build/windows/icon.rc");
    }

    let file = File::create("src/data/scenes.in");
    if let Ok(mut file) = file {
        write!(&file, "// THIS FILE IS AUTOGENERATED BY build.rs BASED ON DATA IN src/blender/scene_*.blend FILES!\n")?;
        write!(
            &file,
            "// DO NOT MODIFY MANUALLY, CHANGES WILL BE OVERWRITTEN!\n"
        )?;
        write!(&file, "[\n")?;
        extract_scene(&mut file, "test", "src/blender/scene_test.blend")?;
        extract_scene(&mut file, "workshop", "src/blender/scene_workshop.blend")?;
        extract_scene(
            &mut file,
            "greenhouse",
            "src/blender/scene_greenhouse.blend",
        )?;
        write!(&file, "]\n")?;
    }
    Ok(())
}

fn extract_scene(file: &mut File, scene_name: &str, blend_file: &str) -> std::io::Result<()> {
    let blend = Blend::from_path(blend_file).expect("error loading blend file");
    for obj in blend.instances_with_code(*b"OB") {
        let loc: Vec<f32> = if obj.is_valid("loc") {
            obj.get_f32_vec("loc")
        } else {
            vec![0.0, 0.0, 0.0]
        };
        let rot: Vec<f32> = if obj.is_valid("rot") {
            obj.get_f32_vec("rot")
        } else {
            vec![0.0, 0.0, 0.0]
        };
        let name = obj.get("id").get_string("name");
        if let Some(name) = get_scene_object_name(name.as_str()) {
            write!(file, "({scene_name:?}, {name:?}, {loc:?}, {rot:?}),\n")?;
        }
    }
    Ok(())
}

fn get_scene_object_name(full_id: &str) -> Option<&str> {
    let prefix = "OBLOAD=";
    if full_id.starts_with(prefix) {
        let remainder: &str = &full_id[prefix.len()..];
        let parts: Vec<&str> = remainder.split('.').collect();
        let name = parts[0];
        return Some(name);
    }
    return None;
}