use super::*;

#[test]
fn projects_company_and_chairman_context_from_save_slice() {
    let save_slice = SmpLoadedSaveSlice {
        file_extension_hint: Some("gms".to_string()),
        container_profile_family: Some("rt3-classic-save-container-v1".to_string()),
        mechanism_family: "classic-save-rehydrate-v1".to_string(),
        mechanism_confidence: "grounded".to_string(),
        trailer_family: None,
        bridge_family: None,
        profile: None,
        candidate_availability_table: None,
        named_locomotive_availability_table: None,
        locomotive_catalog: None,
        cargo_catalog: None,
        world_issue_37_state: None,
        world_economic_tuning_state: None,
        world_finance_neighborhood_state: None,
        world_locomotive_policy_state: None,
        company_roster: Some(save_company_roster()),
        chairman_profile_table: Some(save_chairman_profile_table()),
        region_collection: None,
        region_fixed_row_run_summary: None,
        placed_structure_collection: None,
        placed_structure_dynamic_side_buffer_summary: None,
        special_conditions_table: None,
        event_runtime_collection: None,
        notes: vec![],
    };

    let input = build_runtime_state_input_from_save_slice(
        &save_slice,
        "save-native-company-chairman",
        None,
    )
    .expect("save slice should project");

    assert_eq!(input.state.companies.len(), 2);
    assert_eq!(input.state.selected_company_id, Some(1));
    assert_eq!(input.state.chairman_profiles.len(), 2);
    assert_eq!(input.state.selected_chairman_profile_id, Some(1));
    assert_eq!(input.state.companies[0].book_value_per_share, 2620);
    assert_eq!(input.state.chairman_profiles[0].current_cash, 500);
    assert_eq!(input.state.service_state.company_market_state.len(), 2);
    assert_eq!(
        input
            .state
            .service_state
            .company_market_state
            .get(&1)
            .map(|state| state.cached_share_price_raw_u32),
        Some(0x42200000)
    );
}

#[test]
fn projects_placed_structure_collection_metadata_from_save_slice() {
    let save_slice = SmpLoadedSaveSlice {
        file_extension_hint: Some("gms".to_string()),
        container_profile_family: Some("rt3-classic-save-container-v1".to_string()),
        mechanism_family: "classic-save-rehydrate-v1".to_string(),
        mechanism_confidence: "grounded".to_string(),
        trailer_family: None,
        bridge_family: None,
        profile: None,
        candidate_availability_table: None,
        named_locomotive_availability_table: None,
        locomotive_catalog: None,
        cargo_catalog: None,
        world_issue_37_state: None,
        world_economic_tuning_state: None,
        world_finance_neighborhood_state: None,
        world_locomotive_policy_state: None,
        company_roster: None,
        chairman_profile_table: None,
        region_collection: Some(save_region_collection()),
        region_fixed_row_run_summary: Some(save_region_fixed_row_run_summary()),
        placed_structure_collection: Some(save_placed_structure_collection()),
        placed_structure_dynamic_side_buffer_summary: Some(
            save_placed_structure_dynamic_side_buffer_summary(),
        ),
        special_conditions_table: None,
        event_runtime_collection: None,
        notes: vec![],
    };

    let input = build_runtime_state_input_from_save_slice(
        &save_slice,
        "save-native-placed-structures",
        None,
    )
    .expect("save slice should project");

    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_collection_source_kind")
            .map(String::as_str),
        Some("save-region-record-triplets")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_collection_entry_count")
            .map(String::as_str),
        Some("2")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_collection_profile_entry_count")
            .map(String::as_str),
        Some("3")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_collection_nonzero_prefix_count")
            .map(String::as_str),
        Some("1")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_collection_nonzero_reserved_policy_count")
            .map(String::as_str),
        Some("1")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_fixed_row_run_source_kind")
            .map(String::as_str),
        Some("save-region-fixed-row-run-candidates")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_fixed_row_run_candidate_count")
            .map(String::as_str),
        Some("1")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_fixed_row_run_best_rows_offset_hex")
            .map(String::as_str),
        Some("0x5310")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.region_fixed_row_run_best_shape_signature")
            .map(String::as_str),
        Some("dword0:f32,dword1:zero")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_collection_source_kind")
            .map(String::as_str),
        Some("save-placed-structure-record-triplets")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_collection_entry_count")
            .map(String::as_str),
        Some("2")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_collection_farm_growth_stage_count")
            .map(String::as_str),
        Some("1")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_collection_nondefault_status_count")
            .map(String::as_str),
        Some("2")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_dynamic_side_buffer_source_kind")
            .map(String::as_str),
        Some("save-placed-structure-dynamic-side-buffer-records")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_dynamic_side_buffer_name_pair_count")
            .map(String::as_str),
        Some("9")
    );
    assert_eq!(
        input
            .state
            .metadata
            .get("save_slice.placed_structure_dynamic_side_buffer_triplet_overlap_count")
            .map(String::as_str),
        Some("2")
    );
}
