use crate::inspect::smp::events::SmpLoadedPackedEventGroupedEffectRowSummary;

pub(crate) fn real_grouped_row_is_world_state_family(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
) -> bool {
    row.target_mask_bits == Some(0x08)
        || row.parameter_family.as_deref().is_some_and(|family| {
            family.starts_with("whole_game_")
                || family.starts_with("special_condition")
                || family.starts_with("candidate_availability")
                || family.starts_with("world_flag")
        })
        || row.descriptor_label.as_deref().is_some_and(|label| {
            label.contains("Special Condition")
                || label.contains("Candidate Availability")
                || label.contains("World Flag")
                || label == "Economic Status"
        })
}

pub(crate) fn real_grouped_row_is_shell_owned_descriptor_family(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
) -> bool {
    real_grouped_row_has_runtime_status(row, "shell_owned")
}

pub(crate) fn real_grouped_row_is_evidence_blocked_descriptor_family(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
) -> bool {
    real_grouped_row_has_runtime_status(row, "evidence_blocked")
}

pub(crate) fn real_grouped_row_is_variant_or_scope_blocked_descriptor_family(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
) -> bool {
    real_grouped_row_has_runtime_status(row, "variant_or_scope_blocked")
}

pub(crate) fn real_grouped_row_has_runtime_status(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
    status: &str,
) -> bool {
    crate::inspect::smp::catalog::grouped_effect_descriptor_runtime_status_name(row.descriptor_id)
        .is_some_and(|runtime_status| runtime_status == status)
}

pub(crate) fn real_grouped_row_is_unsupported_executable_descriptor_variant(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
) -> bool {
    if !real_grouped_row_has_runtime_status(row, "executable") {
        return false;
    }
    match row.descriptor_id {
        1 => !(row.opcode == 8 && row.row_shape == "multivalue_scalar"),
        2 => !(row.opcode == 8 && row.row_shape == "multivalue_scalar"),
        8 | 108 | 109 | 122 => row.row_shape != "scalar_assignment",
        13 | 14 => !(row.row_shape == "bool_toggle" && row.raw_scalar_value != 0),
        56 | 57 => row.row_shape != "scalar_assignment",
        _ => match row.parameter_family.as_deref() {
            Some("world_scalar_override") => row.row_shape != "scalar_assignment",
            Some("runtime_variable_scalar") => row.row_shape != "scalar_assignment",
            Some("locomotive_availability_scalar") => {
                !(row.row_shape == "scalar_assignment" && row.raw_scalar_value >= 0)
            }
            Some("locomotive_cost_scalar") => {
                !(row.row_shape == "scalar_assignment" && row.raw_scalar_value >= 0)
            }
            Some("cargo_price_scalar") => {
                !(row.row_shape == "scalar_assignment" && row.raw_scalar_value >= 0)
            }
            Some("cargo_production_scalar") => {
                matches!(row.descriptor_id, 177 | 178 | 179 | 230..=240)
                    && !(row.row_shape == "scalar_assignment" && row.raw_scalar_value >= 0)
            }
            Some("territory_access_cost_scalar") => {
                !(row.row_shape == "scalar_assignment" && row.raw_scalar_value >= 0)
            }
            _ => false,
        },
    }
}
