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

pub(super) fn decode_world_effect(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
    descriptor_metadata: RealGroupedEffectDescriptorMetadata,
) -> Option<RuntimeEffect> {
    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.descriptor_id == 8
        && row.row_shape == "scalar_assignment"
    {
        return Some(RuntimeEffect::SetEconomicStatusCode {
            value: row.raw_scalar_value,
        });
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.descriptor_id == 108
        && row.row_shape == "scalar_assignment"
    {
        return Some(RuntimeEffect::SetSpecialCondition {
            label: descriptor_metadata.label.to_string(),
            value: row.raw_scalar_value as u32,
        });
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.descriptor_id == 122
        && row.row_shape == "scalar_assignment"
    {
        return Some(RuntimeEffect::SetLimitedTrackBuildingAmount {
            value: row.raw_scalar_value,
        });
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.parameter_family == "world_scalar_override"
        && row.row_shape == "scalar_assignment"
    {
        return Some(RuntimeEffect::SetWorldScalarOverride {
            key: runtime_world_scalar_key(descriptor_metadata)?,
            value: i64::from(row.raw_scalar_value),
        });
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.parameter_family == "territory_access_cost_scalar"
        && row.row_shape == "scalar_assignment"
        && row.raw_scalar_value >= 0
    {
        return Some(RuntimeEffect::SetTerritoryAccessCost {
            value: row.raw_scalar_value as u32,
        });
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.parameter_family == "world_flag_toggle"
        && row.row_shape == "bool_toggle"
    {
        return Some(RuntimeEffect::SetWorldFlag {
            key: runtime_world_flag_key(descriptor_metadata)?,
            value: row.raw_scalar_value != 0,
        });
    }

    None
}
