use crate::derived::runtime_company_annual_dividend_policy_state;
use crate::engine::service::annual_finance::constants::COMPANY_DIRECT_DIVIDEND_RATE_FIELD_SLOT;
use crate::state::RuntimeState;

pub(super) fn apply_dividend_adjustment(state: &mut RuntimeState, company_id: u32) -> bool {
    let Some(dividend_state) = runtime_company_annual_dividend_policy_state(state, company_id)
    else {
        return false;
    };
    let Some(proposed_tenths) = dividend_state.proposed_dividend_per_share_tenths else {
        return false;
    };
    let Some(market_state) = state
        .service_state
        .company_market_state
        .get_mut(&company_id)
    else {
        return false;
    };
    let raw_bits = ((proposed_tenths as f32) / 10.0).to_bits();
    let prior_bits = market_state
        .direct_control_transfer_float_fields_raw_u32
        .insert(COMPANY_DIRECT_DIVIDEND_RATE_FIELD_SLOT, raw_bits);
    if prior_bits != Some(raw_bits) {
        state.service_state.annual_dividend_adjustment_commit_count += 1;
        true
    } else {
        false
    }
}
