use std::collections::BTreeMap;

use crate::inspect::smp::save_load::SmpLoadedSaveSlice;
use crate::state::RuntimeSaveProfileState;

pub(super) fn project_save_profile(
    save_slice: &SmpLoadedSaveSlice,
    metadata: &mut BTreeMap<String, String>,
) -> RuntimeSaveProfileState {
    let Some(profile) = &save_slice.profile else {
        return RuntimeSaveProfileState::default();
    };

    metadata.insert(
        "save_slice.profile_kind".to_string(),
        profile.profile_kind.clone(),
    );
    metadata.insert(
        "save_slice.profile_family".to_string(),
        profile.profile_family.clone(),
    );
    metadata.insert(
        "save_slice.packed_profile_offset".to_string(),
        profile.packed_profile_offset.to_string(),
    );
    metadata.insert(
        "save_slice.packed_profile_len".to_string(),
        profile.packed_profile_len.to_string(),
    );
    metadata.insert(
        "save_slice.leading_word_0_hex".to_string(),
        profile.leading_word_0_hex.clone(),
    );
    metadata.insert(
        "save_slice.profile_byte_0x77_hex".to_string(),
        profile.profile_byte_0x77_hex.clone(),
    );
    metadata.insert(
        "save_slice.profile_byte_0x82_hex".to_string(),
        profile.profile_byte_0x82_hex.clone(),
    );
    metadata.insert(
        "save_slice.profile_byte_0x97_hex".to_string(),
        profile.profile_byte_0x97_hex.clone(),
    );
    metadata.insert(
        "save_slice.profile_byte_0xc5_hex".to_string(),
        profile.profile_byte_0xc5_hex.clone(),
    );
    if let Some(header_flag_word_3_hex) = &profile.header_flag_word_3_hex {
        metadata.insert(
            "save_slice.header_flag_word_3_hex".to_string(),
            header_flag_word_3_hex.clone(),
        );
    }
    if let Some(map_path) = &profile.map_path {
        metadata.insert("save_slice.map_path".to_string(), map_path.clone());
    }
    if let Some(display_name) = &profile.display_name {
        metadata.insert("save_slice.display_name".to_string(), display_name.clone());
    }

    RuntimeSaveProfileState {
        profile_kind: Some(profile.profile_kind.clone()),
        profile_family: Some(profile.profile_family.clone()),
        map_path: profile.map_path.clone(),
        display_name: profile.display_name.clone(),
        selected_year_profile_lane: Some(profile.profile_byte_0x77),
        sandbox_enabled: Some(profile.profile_byte_0x82 != 0),
        campaign_scenario_enabled: Some(profile.profile_byte_0xc5 != 0),
        staged_profile_copy_on_restore: Some(profile.profile_byte_0x97 != 0),
    }
}
