use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::state::RuntimeCompanyStatBandCandidate;

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct RuntimeCompanyMarketState {
    #[serde(default)]
    pub outstanding_shares: u32,
    #[serde(default)]
    pub bond_count: u8,
    #[serde(default)]
    pub largest_live_bond_principal: Option<u32>,
    #[serde(default)]
    pub highest_coupon_live_bond_principal: Option<u32>,
    #[serde(default)]
    pub mutable_support_scalar_raw_u32: u32,
    #[serde(default)]
    pub young_company_support_scalar_raw_u32: u32,
    #[serde(default)]
    pub support_progress_word: u32,
    #[serde(default)]
    pub recent_per_share_cache_absolute_counter: u32,
    #[serde(default)]
    pub recent_per_share_cached_value_bits: u64,
    #[serde(default)]
    pub recent_per_share_subscore_raw_u32: u32,
    #[serde(default)]
    pub cached_share_price_raw_u32: u32,
    #[serde(default)]
    pub chairman_salary_baseline: u32,
    #[serde(default)]
    pub chairman_salary_current: u32,
    #[serde(default)]
    pub chairman_bonus_year: u32,
    #[serde(default)]
    pub chairman_bonus_amount: i32,
    #[serde(default)]
    pub founding_year: u32,
    #[serde(default)]
    pub last_bankruptcy_year: u32,
    #[serde(default)]
    pub last_dividend_year: u32,
    #[serde(default)]
    pub current_issue_calendar_word: u32,
    #[serde(default)]
    pub current_issue_calendar_word_2: u32,
    #[serde(default)]
    pub prior_issue_calendar_word: u32,
    #[serde(default)]
    pub prior_issue_calendar_word_2: u32,
    #[serde(default)]
    pub city_connection_latch: bool,
    #[serde(default)]
    pub linked_transit_latch: bool,
    #[serde(default)]
    pub linked_transit_route_anchor_entry_id: Option<u32>,
    #[serde(default)]
    pub linked_transit_route_anchor_fallback_counts: Vec<u32>,
    #[serde(default)]
    pub stat_band_root_0cfb_candidates: Vec<RuntimeCompanyStatBandCandidate>,
    #[serde(default)]
    pub stat_band_root_0d7f_candidates: Vec<RuntimeCompanyStatBandCandidate>,
    #[serde(default)]
    pub stat_band_root_1c47_candidates: Vec<RuntimeCompanyStatBandCandidate>,
    #[serde(default)]
    pub year_stat_family_qword_bits: Vec<u64>,
    #[serde(default)]
    pub special_stat_family_232a_qword_bits: Vec<u64>,
    #[serde(default)]
    pub issue_opinion_terms_raw_i32: Vec<i32>,
    #[serde(default)]
    pub live_bond_slots: Vec<RuntimeCompanyBondSlot>,
    #[serde(default)]
    pub direct_control_transfer_float_fields_raw_u32: BTreeMap<u32, u32>,
    #[serde(default)]
    pub direct_control_transfer_int_fields_raw_u32: BTreeMap<u32, u32>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyBondSlot {
    pub slot_index: u32,
    pub principal: u32,
    #[serde(default)]
    pub maturity_year: u32,
    pub coupon_rate_raw_u32: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct RuntimeCompanyPeriodicSideLatchState {
    #[serde(default)]
    pub preferred_locomotive_engine_type_raw_u8: Option<u8>,
    #[serde(default)]
    pub city_connection_latch: bool,
    #[serde(default)]
    pub linked_transit_latch: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeCompanyPeriodicServiceState {
    pub company_id: u32,
    #[serde(default)]
    pub preferred_locomotive_engine_type_raw_u8: Option<u8>,
    pub city_connection_latch: bool,
    pub linked_transit_latch: bool,
    #[serde(default)]
    pub base_route_preference_raw_u8: Option<u8>,
    #[serde(default)]
    pub effective_route_preference_raw_u8: Option<u8>,
    pub electric_route_preference_override_active: bool,
    pub effective_route_quality_multiplier_basis_points: i64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeWorldRoutePreferenceOverrideState {
    pub company_id: u32,
    #[serde(default)]
    pub base_route_preference_raw_u8: Option<u8>,
    #[serde(default)]
    pub effective_route_preference_raw_u8: Option<u8>,
    pub electric_route_preference_override_active: bool,
}
