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

pub(super) fn decode_catalog_effect(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
    descriptor_metadata: RealGroupedEffectDescriptorMetadata,
) -> Option<RuntimeEffect> {
    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.descriptor_id == 109
        && row.row_shape == "scalar_assignment"
    {
        return Some(RuntimeEffect::SetCandidateAvailability {
            name: runtime_candidate_availability_name(descriptor_metadata.label),
            value: row.raw_scalar_value as u32,
        });
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.parameter_family == "cargo_price_scalar"
        && row.row_shape == "scalar_assignment"
        && row.raw_scalar_value >= 0
    {
        return match row.descriptor_id {
            105 => Some(RuntimeEffect::SetCargoPriceOverride {
                target: RuntimeCargoPriceTarget::All,
                value: row.raw_scalar_value as u32,
            }),
            descriptor_id => grounded_named_cargo_price_label(descriptor_id).map(|name| {
                RuntimeEffect::SetCargoPriceOverride {
                    target: RuntimeCargoPriceTarget::Named {
                        name: name.to_string(),
                    },
                    value: row.raw_scalar_value as u32,
                }
            }),
        };
    }

    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.parameter_family == "cargo_production_scalar"
        && row.row_shape == "scalar_assignment"
        && row.raw_scalar_value >= 0
    {
        return match descriptor_metadata.descriptor_id {
            177 => Some(RuntimeEffect::SetCargoProductionOverride {
                target: RuntimeCargoProductionTarget::All,
                value: row.raw_scalar_value as u32,
            }),
            178 => Some(RuntimeEffect::SetCargoProductionOverride {
                target: RuntimeCargoProductionTarget::Factory,
                value: row.raw_scalar_value as u32,
            }),
            179 => Some(RuntimeEffect::SetCargoProductionOverride {
                target: RuntimeCargoProductionTarget::FarmMine,
                value: row.raw_scalar_value as u32,
            }),
            230..=240 => {
                let slot = descriptor_metadata.descriptor_id.checked_sub(229)?;
                Some(RuntimeEffect::SetCargoProductionSlot {
                    slot,
                    value: row.raw_scalar_value as u32,
                })
            }
            _ => None,
        };
    }

    None
}
