use crate::inspect::smp::events::{
    RealGroupedTargetSubject, SmpLoadedPackedEventCompactControlSummary,
    SmpLoadedPackedEventGroupedEffectRowSummary,
};

pub(in crate::inspect::smp) fn derive_real_grouped_target_subject(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
    compact_control: &SmpLoadedPackedEventCompactControlSummary,
) -> Option<RealGroupedTargetSubject> {
    if row.parameter_family.as_deref() == Some("company_governance_scalar") {
        return Some(RealGroupedTargetSubject::Company);
    }
    if row.parameter_family.as_deref() == Some("world_scalar_override") {
        return Some(RealGroupedTargetSubject::WholeGame);
    }
    match row.target_mask_bits {
        Some(0x08) => Some(RealGroupedTargetSubject::WholeGame),
        Some(0x01) => Some(RealGroupedTargetSubject::Company),
        Some(0x04) => Some(RealGroupedTargetSubject::Territory),
        Some(0x02) => match compact_control
            .grouped_scope_checkboxes_0x7ff
            .get(row.group_index)
            .copied()
        {
            Some(2) => Some(RealGroupedTargetSubject::Chairman),
            _ => Some(RealGroupedTargetSubject::Player),
        },
        _ if row.descriptor_id == 3 => Some(RealGroupedTargetSubject::Territory),
        _ if row.descriptor_id == 15
            && compact_control
                .grouped_territory_selectors_0x80f
                .get(row.group_index)
                .is_some_and(|selector| *selector >= 0) =>
        {
            Some(RealGroupedTargetSubject::Territory)
        }
        _ => None,
    }
}

pub(in crate::inspect::smp) fn real_grouped_target_subject_name(
    subject: RealGroupedTargetSubject,
) -> &'static str {
    match subject {
        RealGroupedTargetSubject::Company => "company",
        RealGroupedTargetSubject::Player => "player",
        RealGroupedTargetSubject::Chairman => "chairman",
        RealGroupedTargetSubject::Territory => "territory",
        RealGroupedTargetSubject::WholeGame => "whole_game",
    }
}

pub(in crate::inspect::smp) fn derive_real_grouped_target_scope_name(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
    compact_control: &SmpLoadedPackedEventCompactControlSummary,
    target_subject: Option<RealGroupedTargetSubject>,
    target_scope_ordinal: Option<u8>,
) -> Option<String> {
    match target_subject {
        Some(RealGroupedTargetSubject::Company) => target_scope_ordinal
            .map(real_grouped_company_scope_name)
            .map(str::to_string),
        Some(RealGroupedTargetSubject::Player) => target_scope_ordinal
            .map(real_grouped_player_scope_name)
            .map(str::to_string),
        Some(RealGroupedTargetSubject::Chairman) => target_scope_ordinal
            .map(real_grouped_chairman_scope_name)
            .map(str::to_string),
        Some(RealGroupedTargetSubject::Territory) => compact_control
            .grouped_territory_selectors_0x80f
            .get(row.group_index)
            .copied()
            .filter(|selector| *selector >= 0)
            .map(|_| "specified_territories".to_string()),
        Some(RealGroupedTargetSubject::WholeGame) => Some("whole_game".to_string()),
        None => None,
    }
}

pub(in crate::inspect::smp) fn real_grouped_company_scope_name(ordinal: u8) -> &'static str {
    match ordinal {
        0 => "condition_true_company",
        1 => "selected_company",
        2 => "human_companies",
        3 => "ai_companies",
        _ => "unsupported_company_scope",
    }
}

pub(in crate::inspect::smp) fn real_grouped_player_scope_name(ordinal: u8) -> &'static str {
    match ordinal {
        0 => "condition_true_player",
        1 => "selected_player",
        2 => "human_players",
        3 => "ai_players",
        _ => "unsupported_player_scope",
    }
}

pub(in crate::inspect::smp) fn real_grouped_chairman_scope_name(ordinal: u8) -> &'static str {
    match ordinal {
        0 => "condition_true_chairman",
        1 => "selected_chairman",
        2 => "human_chairmen",
        3 => "ai_chairmen",
        _ => "unsupported_chairman_scope",
    }
}
