use super::inputs::SelectedCompanyInputs;
use super::model::SelectedCompanySummaryFields;

use crate::derived::runtime_company_unassigned_share_pool;
use crate::summary::{raw_u32_to_f32_text, raw_u64_to_f64_text};

pub(super) fn fill_selected_company_market_fields(
    fields: &mut SelectedCompanySummaryFields,
    inputs: &SelectedCompanyInputs<'_>,
) {
    fields.selected_company_outstanding_shares = inputs
        .market_state
        .map(|market_state| market_state.outstanding_shares);
    fields.selected_company_bond_count = inputs
        .market_state
        .map(|market_state| market_state.bond_count);
    fields.selected_company_largest_live_bond_principal = inputs
        .market_state
        .and_then(|market_state| market_state.largest_live_bond_principal);
    fields.selected_company_highest_coupon_live_bond_principal = inputs
        .market_state
        .and_then(|market_state| market_state.highest_coupon_live_bond_principal);
    fields.selected_company_assigned_share_pool = inputs
        .annual_finance_state
        .as_ref()
        .map(|finance_state| finance_state.assigned_share_pool);
    fields.selected_company_unassigned_share_pool = inputs
        .selected_company_id
        .and_then(|company_id| runtime_company_unassigned_share_pool(inputs.state, company_id));
    fields.selected_company_cached_share_price = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.cached_share_price);
    fields.selected_company_cached_share_price_value_f32_text = inputs
        .market_state
        .map(|market_state| raw_u32_to_f32_text(market_state.cached_share_price_raw_u32));
    fields.selected_company_recent_per_share_cache_absolute_counter = inputs
        .market_state
        .map(|market_state| market_state.recent_per_share_cache_absolute_counter);
    fields.selected_company_recent_per_share_cached_value_f64_text = inputs
        .market_state
        .map(|market_state| raw_u64_to_f64_text(market_state.recent_per_share_cached_value_bits));
    fields.selected_company_recent_per_share_subscore_value_f32_text = inputs
        .market_state
        .map(|market_state| raw_u32_to_f32_text(market_state.recent_per_share_subscore_raw_u32));
    fields.selected_company_mutable_support_scalar_value_f32_text = inputs
        .market_state
        .map(|market_state| raw_u32_to_f32_text(market_state.mutable_support_scalar_raw_u32));
    fields.selected_company_stat_band_root_0cfb_count = inputs
        .market_state
        .map(|market_state| market_state.stat_band_root_0cfb_candidates.len())
        .unwrap_or(0);
    fields.selected_company_stat_band_root_0d7f_count = inputs
        .market_state
        .map(|market_state| market_state.stat_band_root_0d7f_candidates.len())
        .unwrap_or(0);
    fields.selected_company_stat_band_root_1c47_count = inputs
        .market_state
        .map(|market_state| market_state.stat_band_root_1c47_candidates.len())
        .unwrap_or(0);
    fields.selected_company_last_dividend_year = inputs
        .market_state
        .map(|market_state| market_state.last_dividend_year)
        .filter(|year| *year != 0);
    fields.selected_company_years_since_founding = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.years_since_founding);
    fields.selected_company_years_since_last_bankruptcy = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.years_since_last_bankruptcy);
    fields.selected_company_years_since_last_dividend = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.years_since_last_dividend);
    fields.selected_company_current_partial_year_weight_numerator = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.current_partial_year_weight_numerator);
    fields.selected_company_current_issue_absolute_counter = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.current_issue_absolute_counter);
    fields.selected_company_prior_issue_absolute_counter = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.prior_issue_absolute_counter);
    fields.selected_company_current_issue_age_absolute_counter_delta = inputs
        .annual_finance_state
        .as_ref()
        .and_then(|finance_state| finance_state.current_issue_age_absolute_counter_delta);
    fields.selected_company_chairman_bonus_year = inputs
        .market_state
        .map(|market_state| market_state.chairman_bonus_year)
        .filter(|year| *year != 0);
    fields.selected_company_chairman_bonus_amount = inputs
        .market_state
        .map(|market_state| market_state.chairman_bonus_amount)
        .filter(|amount| *amount != 0);
}
