use serde::{Deserialize, Serialize};

use crate::state::CalendarPoint;

pub(super) fn raw_u32_to_f32_text(raw: u32) -> String {
    format!("{:.6}", f32::from_bits(raw))
}

pub(super) fn raw_u64_to_f64_text(raw: u64) -> String {
    format!("{:.6}", f64::from_bits(raw))
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeSummary {
    pub calendar: CalendarPoint,
    pub calendar_projection_source: Option<String>,
    pub calendar_projection_is_placeholder: bool,
    pub world_flag_count: usize,
    pub world_restore_selected_year_profile_lane: Option<u8>,
    pub world_restore_campaign_scenario_enabled: Option<bool>,
    pub world_restore_sandbox_enabled: Option<bool>,
    pub world_restore_seed_tuple_written_from_raw_lane: Option<bool>,
    pub world_restore_absolute_counter_requires_shell_context: Option<bool>,
    pub world_restore_absolute_counter_reconstructible_from_save: Option<bool>,
    pub world_restore_packed_year_word_raw_u16: Option<u16>,
    pub world_restore_partial_year_progress_raw_u8: Option<u8>,
    pub world_restore_current_calendar_tuple_word_raw_u32: Option<u32>,
    pub world_restore_current_calendar_tuple_word_2_raw_u32: Option<u32>,
    pub world_restore_absolute_counter_raw_u32: Option<u32>,
    pub world_restore_absolute_counter_mirror_raw_u32: Option<u32>,
    pub world_restore_disable_cargo_economy_special_condition_slot: Option<u8>,
    pub world_restore_disable_cargo_economy_special_condition_reconstructible_from_save:
        Option<bool>,
    pub world_restore_disable_cargo_economy_special_condition_write_side_grounded: Option<bool>,
    pub world_restore_disable_cargo_economy_special_condition_enabled: Option<bool>,
    pub world_restore_use_bio_accelerator_cars_enabled: Option<bool>,
    pub world_restore_use_wartime_cargos_enabled: Option<bool>,
    pub world_restore_disable_train_crashes_enabled: Option<bool>,
    pub world_restore_disable_train_crashes_and_breakdowns_enabled: Option<bool>,
    pub world_restore_ai_ignore_territories_at_startup_enabled: Option<bool>,
    pub world_restore_limited_track_building_amount: Option<i32>,
    pub world_restore_economic_status_code: Option<i32>,
    pub world_restore_territory_access_cost: Option<u32>,
    pub world_restore_linked_site_removal_follow_on_gate_raw_u8: Option<u8>,
    pub world_restore_linked_site_removal_follow_on_gate_enabled: Option<bool>,
    pub world_restore_auto_show_grade_during_track_lay_raw_u8: Option<u8>,
    pub world_restore_starting_building_density_level_raw_u8: Option<u8>,
    pub world_restore_post_text_building_density_growth_raw_u8: Option<u8>,
    pub world_restore_leftover_simulation_time_accumulator_raw_u32: Option<u32>,
    pub world_restore_leftover_simulation_time_accumulator_value_f32_text: Option<String>,
    pub world_restore_selected_year_lane_snapshot_raw_u8: Option<u8>,
    pub world_restore_all_steam_locomotives_available_raw_u8: Option<u8>,
    pub world_restore_all_steam_locomotives_available_enabled: Option<bool>,
    pub world_restore_all_diesel_locomotives_available_raw_u8: Option<u8>,
    pub world_restore_all_diesel_locomotives_available_enabled: Option<bool>,
    pub world_restore_all_electric_locomotives_available_raw_u8: Option<u8>,
    pub world_restore_all_electric_locomotives_available_enabled: Option<bool>,
    pub world_restore_issue_37_value: Option<u32>,
    pub world_restore_issue_38_value: Option<u32>,
    pub world_restore_issue_39_value: Option<u32>,
    pub world_restore_issue_3a_value: Option<u32>,
    pub world_restore_issue_37_multiplier_raw_u32: Option<u32>,
    pub world_restore_issue_37_multiplier_value_f32_text: Option<String>,
    pub world_restore_stock_issue_and_buyback_policy_raw_u8: Option<u8>,
    pub world_restore_bond_issue_and_repayment_policy_raw_u8: Option<u8>,
    pub world_restore_bankruptcy_policy_raw_u8: Option<u8>,
    pub world_restore_dividend_policy_raw_u8: Option<u8>,
    pub world_restore_building_density_growth_setting_raw_u32: Option<u32>,
    pub world_restore_stock_issue_and_buyback_allowed: Option<bool>,
    pub world_restore_bond_issue_and_repayment_allowed: Option<bool>,
    pub world_restore_bankruptcy_allowed: Option<bool>,
    pub world_restore_dividend_adjustment_allowed: Option<bool>,
    pub world_restore_finance_neighborhood_count: usize,
    pub world_restore_finance_neighborhood_labels: Vec<String>,
    pub world_restore_economic_tuning_mirror_raw_u32: Option<u32>,
    pub world_restore_economic_tuning_mirror_value_f32_text: Option<String>,
    pub world_restore_economic_tuning_lane_count: usize,
    pub world_restore_economic_tuning_lane_value_f32_text: Vec<String>,
    pub world_restore_cached_available_locomotive_rating_raw_u32: Option<u32>,
    pub world_restore_cached_available_locomotive_rating_value_f32_text: Option<String>,
    pub world_restore_selected_year_bucket_scalar_raw_u32: Option<u32>,
    pub world_restore_selected_year_bucket_scalar_value_f32_text: Option<String>,
    pub world_restore_selected_year_bucket_direct_lane_count: usize,
    pub world_restore_selected_year_bucket_direct_lane_value_f32_text: Vec<String>,
    pub world_restore_selected_year_bucket_complement_lane_count: usize,
    pub world_restore_selected_year_bucket_complement_lane_value_f32_text: Vec<String>,
    pub world_restore_selected_year_bucket_scaled_companion_lane_count: usize,
    pub world_restore_selected_year_bucket_scaled_companion_lane_value_f32_text: Vec<String>,
    pub world_restore_selected_year_gap_scalar_raw_u32: Option<u32>,
    pub world_restore_selected_year_gap_scalar_value_f32_text: Option<String>,
    pub world_restore_absolute_counter_restore_kind: Option<String>,
    pub world_restore_absolute_counter_adjustment_context: Option<String>,
    pub metadata_count: usize,
    pub company_count: usize,
    pub active_company_count: usize,
    pub company_market_state_owner_count: usize,
    pub selected_company_outstanding_shares: Option<u32>,
    pub selected_company_bond_count: Option<u8>,
    pub selected_company_largest_live_bond_principal: Option<u32>,
    pub selected_company_highest_coupon_live_bond_principal: Option<u32>,
    pub selected_company_assigned_share_pool: Option<u32>,
    pub selected_company_unassigned_share_pool: Option<u32>,
    pub selected_company_cached_share_price: Option<i64>,
    pub selected_company_cached_share_price_value_f32_text: Option<String>,
    pub selected_company_recent_per_share_cache_absolute_counter: Option<u32>,
    pub selected_company_recent_per_share_cached_value_f64_text: Option<String>,
    pub selected_company_recent_per_share_subscore_value_f32_text: Option<String>,
    pub selected_company_mutable_support_scalar_value_f32_text: Option<String>,
    pub selected_company_stat_band_root_0cfb_count: usize,
    pub selected_company_stat_band_root_0d7f_count: usize,
    pub selected_company_stat_band_root_1c47_count: usize,
    pub selected_company_last_dividend_year: Option<u32>,
    pub selected_company_years_since_founding: Option<u32>,
    pub selected_company_years_since_last_bankruptcy: Option<u32>,
    pub selected_company_years_since_last_dividend: Option<u32>,
    pub selected_company_current_partial_year_weight_numerator: Option<i64>,
    pub selected_company_current_issue_absolute_counter: Option<u32>,
    pub selected_company_prior_issue_absolute_counter: Option<u32>,
    pub selected_company_current_issue_age_absolute_counter_delta: Option<i64>,
    pub selected_company_periodic_side_latch_preferred_locomotive_engine_type_raw_u8: Option<u8>,
    pub selected_company_periodic_side_latch_city_connection_latch: Option<bool>,
    pub selected_company_periodic_side_latch_linked_transit_latch: Option<bool>,
    #[serde(default)]
    pub selected_company_linked_transit_route_anchor_entry_id: Option<u32>,
    #[serde(default)]
    pub selected_company_linked_transit_route_anchor_fallback_counts: Vec<u32>,
    pub selected_company_periodic_service_base_route_preference_raw_u8: Option<u8>,
    pub selected_company_periodic_service_effective_route_preference_raw_u8: Option<u8>,
    pub selected_company_periodic_service_electric_route_preference_override_active: Option<bool>,
    pub selected_company_periodic_service_route_quality_multiplier_basis_points: Option<i64>,
    pub active_periodic_route_preference_override_company_id: Option<u32>,
    pub active_periodic_route_preference_override_effective_raw_u8: Option<u8>,
    pub last_periodic_route_preference_override_company_id: Option<u32>,
    pub last_periodic_route_preference_override_effective_raw_u8: Option<u8>,
    pub selected_company_chairman_bonus_year: Option<u32>,
    pub selected_company_chairman_bonus_amount: Option<i32>,
    pub selected_company_creditor_pressure_recent_bad_net_profit_year_count: Option<u32>,
    pub selected_company_creditor_pressure_recent_peak_revenue: Option<i64>,
    pub selected_company_creditor_pressure_recent_three_year_net_profit_total: Option<i64>,
    pub selected_company_creditor_pressure_cash_floor: Option<i64>,
    pub selected_company_creditor_pressure_cash_plus_slot_12_total: Option<i64>,
    pub selected_company_creditor_pressure_share_price_floor: Option<i64>,
    pub selected_company_creditor_pressure_share_price_scalar: Option<i64>,
    pub selected_company_creditor_pressure_current_fuel_cost: Option<i64>,
    pub selected_company_creditor_pressure_current_fuel_cost_floor: Option<i64>,
    pub selected_company_creditor_pressure_eligible_for_bankruptcy_branch: Option<bool>,
    pub selected_company_deep_distress_current_cash: Option<i64>,
    pub selected_company_deep_distress_recent_first_three_net_profit_years: Vec<i64>,
    pub selected_company_deep_distress_cash_floor: Option<i64>,
    pub selected_company_deep_distress_net_profit_floor: Option<i64>,
    pub selected_company_deep_distress_eligible_for_bankruptcy_fallback: Option<bool>,
    pub selected_company_annual_bond_linked_transit_latch: Option<bool>,
    pub selected_company_annual_bond_live_bond_count: Option<u8>,
    pub selected_company_annual_bond_live_bond_principal_total: Option<u32>,
    pub selected_company_annual_bond_matured_live_bond_count: Option<u32>,
    pub selected_company_annual_bond_matured_live_bond_principal_total: Option<u32>,
    pub selected_company_annual_bond_next_live_bond_maturity_year: Option<u32>,
    pub selected_company_annual_bond_live_bond_coupon_burden_total: Option<i64>,
    pub selected_company_annual_bond_current_cash: Option<i64>,
    pub selected_company_annual_bond_cash_after_full_repayment: Option<i64>,
    pub selected_company_annual_bond_issue_cash_floor: Option<i64>,
    pub selected_company_annual_bond_issue_principal_step: Option<u32>,
    pub selected_company_annual_bond_proposed_issue_bond_count: Option<u32>,
    pub selected_company_annual_bond_proposed_issue_total_principal: Option<u32>,
    pub selected_company_annual_bond_proposed_issue_years_to_maturity: Option<u32>,
    pub selected_company_annual_bond_eligible_for_issue_branch: Option<bool>,
    pub selected_company_stock_repurchase_city_connection_latch: Option<bool>,
    pub selected_company_stock_repurchase_building_density_growth_setting: Option<u32>,
    pub selected_company_stock_repurchase_linked_chairman_profile_id: Option<u32>,
    pub selected_company_stock_repurchase_linked_chairman_personality_raw_u8: Option<u8>,
    pub selected_company_stock_repurchase_batch_size: Option<u32>,
    pub selected_company_stock_repurchase_factor_basis_points: Option<i64>,
    pub selected_company_stock_repurchase_current_cash: Option<i64>,
    pub selected_company_stock_repurchase_stock_value_gate_cash_floor: Option<i64>,
    pub selected_company_stock_repurchase_support_adjusted_share_price_scalar: Option<i64>,
    pub selected_company_stock_repurchase_affordability_cash_floor: Option<i64>,
    pub selected_company_stock_repurchase_unassigned_share_pool: Option<u32>,
    pub selected_company_stock_repurchase_eligible_for_single_batch: Option<bool>,
    pub selected_company_stock_issue_live_bond_count: Option<u8>,
    pub selected_company_stock_issue_initial_batch_size: Option<u32>,
    pub selected_company_stock_issue_trimmed_batch_size: Option<u32>,
    pub selected_company_stock_issue_share_pressure_basis_points: Option<i64>,
    pub selected_company_stock_issue_pressured_share_price_scalar: Option<i64>,
    pub selected_company_stock_issue_pressured_proceeds: Option<i64>,
    pub selected_company_stock_issue_book_value_per_share_floor_applied: Option<i64>,
    pub selected_company_stock_issue_price_to_book_ratio_basis_points: Option<i64>,
    pub selected_company_stock_issue_current_cash: Option<i64>,
    pub selected_company_stock_issue_highest_coupon_live_bond_principal: Option<u32>,
    pub selected_company_stock_issue_highest_coupon_live_bond_rate_basis_points: Option<i64>,
    pub selected_company_stock_issue_current_issue_age_absolute_counter_delta: Option<i64>,
    pub selected_company_stock_issue_current_issue_cooldown_floor: Option<i64>,
    pub selected_company_stock_issue_minimum_price_to_book_ratio_basis_points: Option<i64>,
    pub selected_company_stock_issue_passes_share_price_floor: Option<bool>,
    pub selected_company_stock_issue_passes_proceeds_floor: Option<bool>,
    pub selected_company_stock_issue_passes_cash_gate: Option<bool>,
    pub selected_company_stock_issue_passes_issue_cooldown_gate: Option<bool>,
    pub selected_company_stock_issue_passes_coupon_price_to_book_gate: Option<bool>,
    pub selected_company_stock_issue_eligible_for_double_tranche: Option<bool>,
    pub selected_company_dividend_weighted_recent_net_profit_total: Option<i64>,
    pub selected_company_dividend_weighted_recent_net_profit_average: Option<i64>,
    pub selected_company_dividend_current_cash: Option<i64>,
    pub selected_company_dividend_tiny_unassigned_share_cash_supplement_branch: Option<bool>,
    pub selected_company_dividend_tentative_target_per_share_tenths: Option<i64>,
    pub selected_company_dividend_current_per_share_tenths: Option<i64>,
    pub selected_company_dividend_growth_adjusted_current_per_share_tenths: Option<i64>,
    pub selected_company_dividend_board_approved_ceiling_tenths: Option<i64>,
    pub selected_company_dividend_proposed_per_share_tenths: Option<i64>,
    pub selected_company_dividend_eligible_for_adjustment_branch: Option<bool>,
    pub selected_company_annual_finance_policy_action: Option<String>,
    pub selected_company_annual_finance_news_family_candidate: Option<String>,
    pub selected_company_annual_finance_last_news_selector: Option<String>,
    pub annual_finance_last_news_event_count: usize,
    pub selected_company_annual_finance_policy_creditor_pressure_bankruptcy_eligible: Option<bool>,
    pub selected_company_annual_finance_policy_deep_distress_bankruptcy_fallback_eligible:
        Option<bool>,
    pub selected_company_annual_finance_policy_bond_issue_eligible: Option<bool>,
    pub selected_company_annual_finance_policy_stock_repurchase_eligible: Option<bool>,
    pub selected_company_annual_finance_policy_stock_issue_eligible: Option<bool>,
    pub selected_company_annual_finance_policy_dividend_adjustment_eligible: Option<bool>,
    pub player_count: usize,
    pub chairman_profile_count: usize,
    pub active_chairman_profile_count: usize,
    pub selected_chairman_profile_id: Option<u32>,
    pub linked_chairman_company_count: usize,
    pub company_takeover_cooldown_count: usize,
    pub company_merger_cooldown_count: usize,
    pub train_count: usize,
    pub active_train_count: usize,
    pub retired_train_count: usize,
    pub locomotive_catalog_count: usize,
    pub cargo_catalog_count: usize,
    pub territory_count: usize,
    pub company_territory_track_count: usize,
    pub packed_event_collection_present: bool,
    pub packed_event_record_count: usize,
    pub packed_event_decoded_record_count: usize,
    pub packed_event_imported_runtime_record_count: usize,
    pub packed_event_parity_only_record_count: usize,
    pub packed_event_unsupported_record_count: usize,
    pub packed_event_blocked_missing_company_context_count: usize,
    pub packed_event_blocked_missing_selection_context_count: usize,
    pub packed_event_blocked_missing_company_role_context_count: usize,
    pub packed_event_blocked_missing_player_context_count: usize,
    pub packed_event_blocked_missing_player_selection_context_count: usize,
    pub packed_event_blocked_missing_player_role_context_count: usize,
    pub packed_event_blocked_missing_chairman_context_count: usize,
    pub packed_event_blocked_chairman_target_scope_count: usize,
    pub packed_event_blocked_missing_condition_context_count: usize,
    pub packed_event_blocked_missing_player_condition_context_count: usize,
    pub packed_event_blocked_company_condition_scope_disabled_count: usize,
    pub packed_event_blocked_player_condition_scope_count: usize,
    pub packed_event_blocked_territory_condition_scope_count: usize,
    pub packed_event_blocked_missing_territory_context_count: usize,
    pub packed_event_blocked_named_territory_binding_count: usize,
    pub packed_event_blocked_unmapped_ordinary_condition_count: usize,
    pub packed_event_blocked_unmapped_world_condition_count: usize,
    pub packed_event_blocked_missing_compact_control_count: usize,
    pub packed_event_blocked_shell_owned_descriptor_count: usize,
    pub packed_event_blocked_evidence_blocked_descriptor_count: usize,
    pub packed_event_blocked_variant_or_scope_blocked_descriptor_count: usize,
    pub packed_event_blocked_unmapped_real_descriptor_count: usize,
    pub packed_event_blocked_unmapped_world_descriptor_count: usize,
    pub packed_event_blocked_territory_access_variant_count: usize,
    pub packed_event_blocked_territory_access_scope_count: usize,
    pub packed_event_blocked_missing_train_context_count: usize,
    pub packed_event_blocked_missing_train_territory_context_count: usize,
    pub packed_event_blocked_missing_locomotive_catalog_context_count: usize,
    pub packed_event_blocked_confiscation_variant_count: usize,
    pub packed_event_blocked_retire_train_variant_count: usize,
    pub packed_event_blocked_retire_train_scope_count: usize,
    pub packed_event_blocked_structural_only_count: usize,
    pub event_runtime_record_count: usize,
    pub candidate_availability_count: usize,
    pub zero_candidate_availability_count: usize,
    pub named_locomotive_availability_count: usize,
    pub zero_named_locomotive_availability_count: usize,
    pub named_locomotive_cost_count: usize,
    pub cargo_production_override_count: usize,
    pub world_runtime_variable_count: usize,
    pub company_runtime_variable_owner_count: usize,
    pub player_runtime_variable_owner_count: usize,
    pub territory_runtime_variable_owner_count: usize,
    pub world_scalar_override_count: usize,
    pub special_condition_count: usize,
    pub enabled_special_condition_count: usize,
    pub save_profile_kind: Option<String>,
    pub save_profile_family: Option<String>,
    pub save_profile_map_path: Option<String>,
    pub save_profile_display_name: Option<String>,
    pub save_profile_selected_year_profile_lane: Option<u8>,
    pub save_profile_sandbox_enabled: Option<bool>,
    pub save_profile_campaign_scenario_enabled: Option<bool>,
    pub save_profile_staged_profile_copy_on_restore: Option<bool>,
    pub total_event_record_service_count: u64,
    pub periodic_boundary_call_count: u64,
    pub annual_finance_service_call_count: u64,
    pub periodic_route_preference_override_apply_count: u64,
    pub periodic_route_preference_override_restore_count: u64,
    pub annual_dividend_adjustment_commit_count: u64,
    pub annual_bond_last_retired_principal_total: u64,
    pub annual_bond_last_issued_principal_total: u64,
    pub annual_bond_last_principal_flow_relation: Option<String>,
    pub annual_stock_repurchase_last_share_count: u64,
    pub annual_stock_issue_last_share_count: u64,
    pub total_trigger_dispatch_count: u64,
    pub dirty_rerun_count: u64,
    pub total_company_cash: i64,
}
