use crate::inspect::smp::events::*;
use crate::inspect::smp::*;

pub(in crate::inspect::smp) fn derive_negative_sentinel_scope_summary(
    rows: &[SmpLoadedPackedEventConditionRowSummary],
    control: &SmpLoadedPackedEventCompactControlSummary,
) -> Option<SmpLoadedPackedEventNegativeSentinelScopeSummary> {
    let source_row_indexes = rows
        .iter()
        .filter(|row| row.raw_condition_id == -1)
        .map(|row| row.row_index)
        .collect::<Vec<_>>();
    if source_row_indexes.is_empty() {
        return None;
    }

    Some(SmpLoadedPackedEventNegativeSentinelScopeSummary {
        company_test_scope: decode_company_condition_test_scope(control.modifier_flag_0x7f9)?,
        player_test_scope: decode_player_condition_test_scope(control.modifier_flag_0x7fa)?,
        territory_scope_selector_is_0x63: control.primary_selector_0x7f0 == 0x63,
        source_row_indexes,
    })
}

pub(in crate::inspect::smp) fn decode_company_condition_test_scope(
    value: u8,
) -> Option<RuntimeCompanyConditionTestScope> {
    match value {
        0 => Some(RuntimeCompanyConditionTestScope::Disabled),
        1 => Some(RuntimeCompanyConditionTestScope::AllCompanies),
        2 => Some(RuntimeCompanyConditionTestScope::SelectedCompanyOnly),
        3 => Some(RuntimeCompanyConditionTestScope::AiCompaniesOnly),
        4 => Some(RuntimeCompanyConditionTestScope::HumanCompaniesOnly),
        _ => None,
    }
}

pub(in crate::inspect::smp) fn decode_player_condition_test_scope(
    value: u8,
) -> Option<RuntimePlayerConditionTestScope> {
    match value {
        0 => Some(RuntimePlayerConditionTestScope::Disabled),
        1 => Some(RuntimePlayerConditionTestScope::AllPlayers),
        2 => Some(RuntimePlayerConditionTestScope::SelectedPlayerOnly),
        3 => Some(RuntimePlayerConditionTestScope::AiPlayersOnly),
        4 => Some(RuntimePlayerConditionTestScope::HumanPlayersOnly),
        _ => None,
    }
}
