use crate::derived::runtime_annual_bond_principal_flow_relation_label;
use crate::state::RuntimeState;
use crate::summary::RuntimeSummary;

pub(super) struct CollectionsSummaryFields {
    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 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,
}

pub(super) fn build_collections_summary_fields(state: &RuntimeState) -> CollectionsSummaryFields {
    CollectionsSummaryFields {
        player_count: state.players.len(),
        chairman_profile_count: state.chairman_profiles.len(),
        active_chairman_profile_count: state
            .chairman_profiles
            .iter()
            .filter(|profile| profile.active)
            .count(),
        selected_chairman_profile_id: state.selected_chairman_profile_id,
        linked_chairman_company_count: state
            .companies
            .iter()
            .filter(|company| company.linked_chairman_profile_id.is_some())
            .count(),
        company_takeover_cooldown_count: state
            .companies
            .iter()
            .filter(|company| company.takeover_cooldown_year.is_some())
            .count(),
        company_merger_cooldown_count: state
            .companies
            .iter()
            .filter(|company| company.merger_cooldown_year.is_some())
            .count(),
        train_count: state.trains.len(),
        active_train_count: state.trains.iter().filter(|train| train.active).count(),
        retired_train_count: state.trains.iter().filter(|train| train.retired).count(),
        locomotive_catalog_count: state.locomotive_catalog.len(),
        cargo_catalog_count: state.cargo_catalog.len(),
        territory_count: state.territories.len(),
        company_territory_track_count: state.company_territory_track_piece_counts.len(),
        event_runtime_record_count: state.event_runtime_records.len(),
        candidate_availability_count: state.candidate_availability.len(),
        zero_candidate_availability_count: state
            .candidate_availability
            .values()
            .filter(|value| **value == 0)
            .count(),
        named_locomotive_availability_count: state.named_locomotive_availability.len(),
        zero_named_locomotive_availability_count: state
            .named_locomotive_availability
            .values()
            .filter(|value| **value == 0)
            .count(),
        named_locomotive_cost_count: state.named_locomotive_cost.len(),
        cargo_production_override_count: state.cargo_production_overrides.len(),
        world_runtime_variable_count: state.world_runtime_variables.len(),
        company_runtime_variable_owner_count: state.company_runtime_variables.len(),
        player_runtime_variable_owner_count: state.player_runtime_variables.len(),
        territory_runtime_variable_owner_count: state.territory_runtime_variables.len(),
        world_scalar_override_count: state.world_scalar_overrides.len(),
        special_condition_count: state.special_conditions.len(),
        enabled_special_condition_count: state
            .special_conditions
            .values()
            .filter(|value| **value != 0)
            .count(),
        save_profile_kind: state.save_profile.profile_kind.clone(),
        save_profile_family: state.save_profile.profile_family.clone(),
        save_profile_map_path: state.save_profile.map_path.clone(),
        save_profile_display_name: state.save_profile.display_name.clone(),
        save_profile_selected_year_profile_lane: state.save_profile.selected_year_profile_lane,
        save_profile_sandbox_enabled: state.save_profile.sandbox_enabled,
        save_profile_campaign_scenario_enabled: state.save_profile.campaign_scenario_enabled,
        save_profile_staged_profile_copy_on_restore: state
            .save_profile
            .staged_profile_copy_on_restore,
        total_event_record_service_count: state.service_state.total_event_record_services,
        periodic_boundary_call_count: state.service_state.periodic_boundary_calls,
        annual_finance_service_call_count: state.service_state.annual_finance_service_calls,
        periodic_route_preference_override_apply_count: state
            .service_state
            .periodic_route_preference_override_apply_count,
        periodic_route_preference_override_restore_count: state
            .service_state
            .periodic_route_preference_override_restore_count,
        annual_dividend_adjustment_commit_count: state
            .service_state
            .annual_dividend_adjustment_commit_count,
        annual_bond_last_retired_principal_total: state
            .service_state
            .annual_bond_last_retired_principal_total,
        annual_bond_last_issued_principal_total: state
            .service_state
            .annual_bond_last_issued_principal_total,
        annual_bond_last_principal_flow_relation:
            runtime_annual_bond_principal_flow_relation_label(
                state.service_state.annual_bond_last_retired_principal_total,
                state.service_state.annual_bond_last_issued_principal_total,
            )
            .map(str::to_string),
        annual_stock_repurchase_last_share_count: state
            .service_state
            .annual_stock_repurchase_last_share_count,
        annual_stock_issue_last_share_count: state
            .service_state
            .annual_stock_issue_last_share_count,
        total_trigger_dispatch_count: state.service_state.trigger_dispatch_counts.values().sum(),
        dirty_rerun_count: state.service_state.dirty_rerun_count,
        total_company_cash: state
            .companies
            .iter()
            .map(|company| company.current_cash)
            .sum(),
    }
}

pub(super) fn apply_collections_summary_fields(
    summary: &mut RuntimeSummary,
    fields: CollectionsSummaryFields,
) {
    let CollectionsSummaryFields {
        player_count,
        chairman_profile_count,
        active_chairman_profile_count,
        selected_chairman_profile_id,
        linked_chairman_company_count,
        company_takeover_cooldown_count,
        company_merger_cooldown_count,
        train_count,
        active_train_count,
        retired_train_count,
        locomotive_catalog_count,
        cargo_catalog_count,
        territory_count,
        company_territory_track_count,
        event_runtime_record_count,
        candidate_availability_count,
        zero_candidate_availability_count,
        named_locomotive_availability_count,
        zero_named_locomotive_availability_count,
        named_locomotive_cost_count,
        cargo_production_override_count,
        world_runtime_variable_count,
        company_runtime_variable_owner_count,
        player_runtime_variable_owner_count,
        territory_runtime_variable_owner_count,
        world_scalar_override_count,
        special_condition_count,
        enabled_special_condition_count,
        save_profile_kind,
        save_profile_family,
        save_profile_map_path,
        save_profile_display_name,
        save_profile_selected_year_profile_lane,
        save_profile_sandbox_enabled,
        save_profile_campaign_scenario_enabled,
        save_profile_staged_profile_copy_on_restore,
        total_event_record_service_count,
        periodic_boundary_call_count,
        annual_finance_service_call_count,
        periodic_route_preference_override_apply_count,
        periodic_route_preference_override_restore_count,
        annual_dividend_adjustment_commit_count,
        annual_bond_last_retired_principal_total,
        annual_bond_last_issued_principal_total,
        annual_bond_last_principal_flow_relation,
        annual_stock_repurchase_last_share_count,
        annual_stock_issue_last_share_count,
        total_trigger_dispatch_count,
        dirty_rerun_count,
        total_company_cash,
    } = fields;

    summary.player_count = player_count;
    summary.chairman_profile_count = chairman_profile_count;
    summary.active_chairman_profile_count = active_chairman_profile_count;
    summary.selected_chairman_profile_id = selected_chairman_profile_id;
    summary.linked_chairman_company_count = linked_chairman_company_count;
    summary.company_takeover_cooldown_count = company_takeover_cooldown_count;
    summary.company_merger_cooldown_count = company_merger_cooldown_count;
    summary.train_count = train_count;
    summary.active_train_count = active_train_count;
    summary.retired_train_count = retired_train_count;
    summary.locomotive_catalog_count = locomotive_catalog_count;
    summary.cargo_catalog_count = cargo_catalog_count;
    summary.territory_count = territory_count;
    summary.company_territory_track_count = company_territory_track_count;
    summary.event_runtime_record_count = event_runtime_record_count;
    summary.candidate_availability_count = candidate_availability_count;
    summary.zero_candidate_availability_count = zero_candidate_availability_count;
    summary.named_locomotive_availability_count = named_locomotive_availability_count;
    summary.zero_named_locomotive_availability_count = zero_named_locomotive_availability_count;
    summary.named_locomotive_cost_count = named_locomotive_cost_count;
    summary.cargo_production_override_count = cargo_production_override_count;
    summary.world_runtime_variable_count = world_runtime_variable_count;
    summary.company_runtime_variable_owner_count = company_runtime_variable_owner_count;
    summary.player_runtime_variable_owner_count = player_runtime_variable_owner_count;
    summary.territory_runtime_variable_owner_count = territory_runtime_variable_owner_count;
    summary.world_scalar_override_count = world_scalar_override_count;
    summary.special_condition_count = special_condition_count;
    summary.enabled_special_condition_count = enabled_special_condition_count;
    summary.save_profile_kind = save_profile_kind;
    summary.save_profile_family = save_profile_family;
    summary.save_profile_map_path = save_profile_map_path;
    summary.save_profile_display_name = save_profile_display_name;
    summary.save_profile_selected_year_profile_lane = save_profile_selected_year_profile_lane;
    summary.save_profile_sandbox_enabled = save_profile_sandbox_enabled;
    summary.save_profile_campaign_scenario_enabled = save_profile_campaign_scenario_enabled;
    summary.save_profile_staged_profile_copy_on_restore =
        save_profile_staged_profile_copy_on_restore;
    summary.total_event_record_service_count = total_event_record_service_count;
    summary.periodic_boundary_call_count = periodic_boundary_call_count;
    summary.annual_finance_service_call_count = annual_finance_service_call_count;
    summary.periodic_route_preference_override_apply_count =
        periodic_route_preference_override_apply_count;
    summary.periodic_route_preference_override_restore_count =
        periodic_route_preference_override_restore_count;
    summary.annual_dividend_adjustment_commit_count = annual_dividend_adjustment_commit_count;
    summary.annual_bond_last_retired_principal_total = annual_bond_last_retired_principal_total;
    summary.annual_bond_last_issued_principal_total = annual_bond_last_issued_principal_total;
    summary.annual_bond_last_principal_flow_relation = annual_bond_last_principal_flow_relation;
    summary.annual_stock_repurchase_last_share_count = annual_stock_repurchase_last_share_count;
    summary.annual_stock_issue_last_share_count = annual_stock_issue_last_share_count;
    summary.total_trigger_dispatch_count = total_trigger_dispatch_count;
    summary.dirty_rerun_count = dirty_rerun_count;
    summary.total_company_cash = total_company_cash;
}
