use serde::{Deserialize, Serialize};

use crate::event::metrics::RuntimeCompanyMetric;
use crate::event::records::RuntimeEventRecordTemplate;
use crate::event::targets::{
    RuntimeCargoPriceTarget, RuntimeCargoProductionTarget, RuntimeChairmanTarget,
    RuntimeCompanyTarget, RuntimePlayerTarget, RuntimeTerritoryTarget,
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum RuntimeEffect {
    SetWorldFlag {
        key: String,
        value: bool,
    },
    SetLimitedTrackBuildingAmount {
        value: i32,
    },
    SetEconomicStatusCode {
        value: i32,
    },
    SetCompanyCash {
        target: RuntimeCompanyTarget,
        value: i64,
    },
    SetPlayerCash {
        target: RuntimePlayerTarget,
        value: i64,
    },
    SetChairmanCash {
        target: RuntimeChairmanTarget,
        value: i64,
    },
    SetCompanyGovernanceScalar {
        target: RuntimeCompanyTarget,
        metric: RuntimeCompanyMetric,
        value: i64,
    },
    DeactivatePlayer {
        target: RuntimePlayerTarget,
    },
    DeactivateChairman {
        target: RuntimeChairmanTarget,
    },
    SetCompanyTerritoryAccess {
        target: RuntimeCompanyTarget,
        territory: RuntimeTerritoryTarget,
        value: bool,
    },
    ConfiscateCompanyAssets {
        target: RuntimeCompanyTarget,
    },
    DeactivateCompany {
        target: RuntimeCompanyTarget,
    },
    SetCompanyTrackLayingCapacity {
        target: RuntimeCompanyTarget,
        value: Option<u32>,
    },
    RetireTrains {
        #[serde(default)]
        company_target: Option<RuntimeCompanyTarget>,
        #[serde(default)]
        territory_target: Option<RuntimeTerritoryTarget>,
        #[serde(default)]
        locomotive_name: Option<String>,
    },
    AdjustCompanyCash {
        target: RuntimeCompanyTarget,
        delta: i64,
    },
    AdjustCompanyDebt {
        target: RuntimeCompanyTarget,
        delta: i64,
    },
    SetCandidateAvailability {
        name: String,
        value: u32,
    },
    SetNamedLocomotiveAvailability {
        name: String,
        value: bool,
    },
    SetNamedLocomotiveAvailabilityValue {
        name: String,
        value: u32,
    },
    SetNamedLocomotiveCost {
        name: String,
        value: u32,
    },
    SetCargoPriceOverride {
        target: RuntimeCargoPriceTarget,
        value: u32,
    },
    SetCargoProductionOverride {
        target: RuntimeCargoProductionTarget,
        value: u32,
    },
    SetCargoProductionSlot {
        slot: u32,
        value: u32,
    },
    SetWorldVariable {
        index: u32,
        value: i64,
    },
    SetCompanyVariable {
        target: RuntimeCompanyTarget,
        index: u32,
        value: i64,
    },
    SetPlayerVariable {
        target: RuntimePlayerTarget,
        index: u32,
        value: i64,
    },
    SetTerritoryVariable {
        target: RuntimeTerritoryTarget,
        index: u32,
        value: i64,
    },
    SetWorldScalarOverride {
        key: String,
        value: i64,
    },
    SetTerritoryAccessCost {
        value: u32,
    },
    SetSpecialCondition {
        label: String,
        value: u32,
    },
    AppendEventRecord {
        record: Box<RuntimeEventRecordTemplate>,
    },
    ActivateEventRecord {
        record_id: u32,
    },
    DeactivateEventRecord {
        record_id: u32,
    },
    RemoveEventRecord {
        record_id: u32,
    },
}
