use crate::RuntimeState;
use crate::derived::{
    runtime_company_annual_bond_policy_state, runtime_company_annual_creditor_pressure_state,
    runtime_company_annual_deep_distress_state, runtime_company_annual_dividend_policy_state,
    runtime_company_annual_finance_policy_state, runtime_company_annual_finance_state,
    runtime_company_annual_stock_issue_state, runtime_company_annual_stock_repurchase_state,
    runtime_company_periodic_service_state,
};
use crate::state::{
    RuntimeCompanyAnnualBondPolicyState, RuntimeCompanyAnnualCreditorPressureState,
    RuntimeCompanyAnnualDeepDistressState, RuntimeCompanyAnnualDividendPolicyState,
    RuntimeCompanyAnnualFinancePolicyState, RuntimeCompanyAnnualFinanceState,
    RuntimeCompanyAnnualStockIssueState, RuntimeCompanyAnnualStockRepurchaseState,
    RuntimeCompanyMarketState, RuntimeCompanyPeriodicServiceState,
    RuntimeCompanyPeriodicSideLatchState,
};

pub(super) struct SelectedCompanyInputs<'a> {
    pub(super) state: &'a RuntimeState,
    pub(super) selected_company_id: Option<u32>,
    pub(super) market_state: Option<&'a RuntimeCompanyMarketState>,
    pub(super) periodic_side_latch_state: Option<&'a RuntimeCompanyPeriodicSideLatchState>,
    pub(super) periodic_service_state: Option<RuntimeCompanyPeriodicServiceState>,
    pub(super) annual_finance_state: Option<RuntimeCompanyAnnualFinanceState>,
    pub(super) creditor_pressure_state: Option<RuntimeCompanyAnnualCreditorPressureState>,
    pub(super) deep_distress_state: Option<RuntimeCompanyAnnualDeepDistressState>,
    pub(super) annual_bond_state: Option<RuntimeCompanyAnnualBondPolicyState>,
    pub(super) stock_repurchase_state: Option<RuntimeCompanyAnnualStockRepurchaseState>,
    pub(super) stock_issue_state: Option<RuntimeCompanyAnnualStockIssueState>,
    pub(super) dividend_state: Option<RuntimeCompanyAnnualDividendPolicyState>,
    pub(super) annual_finance_policy_state: Option<RuntimeCompanyAnnualFinancePolicyState>,
}

impl<'a> SelectedCompanyInputs<'a> {
    pub(super) fn from_state(state: &'a RuntimeState) -> Self {
        let selected_company_id = state.selected_company_id;
        Self {
            state,
            selected_company_id,
            market_state: selected_company_id
                .and_then(|company_id| state.service_state.company_market_state.get(&company_id)),
            periodic_side_latch_state: selected_company_id.and_then(|company_id| {
                state
                    .service_state
                    .company_periodic_side_latch_state
                    .get(&company_id)
            }),
            periodic_service_state: selected_company_id
                .and_then(|company_id| runtime_company_periodic_service_state(state, company_id)),
            annual_finance_state: selected_company_id
                .and_then(|company_id| runtime_company_annual_finance_state(state, company_id)),
            creditor_pressure_state: selected_company_id.and_then(|company_id| {
                runtime_company_annual_creditor_pressure_state(state, company_id)
            }),
            deep_distress_state: selected_company_id.and_then(|company_id| {
                runtime_company_annual_deep_distress_state(state, company_id)
            }),
            annual_bond_state: selected_company_id
                .and_then(|company_id| runtime_company_annual_bond_policy_state(state, company_id)),
            stock_repurchase_state: selected_company_id.and_then(|company_id| {
                runtime_company_annual_stock_repurchase_state(state, company_id)
            }),
            stock_issue_state: selected_company_id
                .and_then(|company_id| runtime_company_annual_stock_issue_state(state, company_id)),
            dividend_state: selected_company_id.and_then(|company_id| {
                runtime_company_annual_dividend_policy_state(state, company_id)
            }),
            annual_finance_policy_state: selected_company_id.and_then(|company_id| {
                runtime_company_annual_finance_policy_state(state, company_id)
            }),
        }
    }
}
