use std::collections::BTreeSet;

use crate::event::conditions::RuntimeCondition;

use super::{
    validate_chairman_target, validate_company_target, validate_player_target,
    validate_territory_target,
};

pub(crate) fn validate_runtime_condition(
    condition: &RuntimeCondition,
    valid_company_ids: &BTreeSet<u32>,
    valid_player_ids: &BTreeSet<u32>,
    valid_chairman_profile_ids: &BTreeSet<u32>,
    valid_territory_ids: &BTreeSet<u32>,
) -> Result<(), String> {
    match condition {
        RuntimeCondition::WorldVariableThreshold { index, .. } => {
            if !(1..=4).contains(index) {
                Err("index must be in 1..=4".to_string())
            } else {
                Ok(())
            }
        }
        RuntimeCondition::CompanyNumericThreshold { target, .. } => {
            validate_company_target(target, valid_company_ids)
        }
        RuntimeCondition::CompanyVariableThreshold { target, index, .. } => {
            validate_company_target(target, valid_company_ids)?;
            if !(1..=4).contains(index) {
                Err("index must be in 1..=4".to_string())
            } else {
                Ok(())
            }
        }
        RuntimeCondition::ChairmanNumericThreshold { target, .. } => {
            validate_chairman_target(target, valid_chairman_profile_ids)
        }
        RuntimeCondition::PlayerVariableThreshold { target, index, .. } => {
            validate_player_target(target, valid_player_ids)?;
            if !(1..=4).contains(index) {
                Err("index must be in 1..=4".to_string())
            } else {
                Ok(())
            }
        }
        RuntimeCondition::TerritoryNumericThreshold { target, .. } => {
            validate_territory_target(target, valid_territory_ids)
        }
        RuntimeCondition::TerritoryVariableThreshold { target, index, .. } => {
            validate_territory_target(target, valid_territory_ids)?;
            if !(1..=4).contains(index) {
                Err("index must be in 1..=4".to_string())
            } else {
                Ok(())
            }
        }
        RuntimeCondition::CompanyTerritoryNumericThreshold {
            target, territory, ..
        } => {
            validate_company_target(target, valid_company_ids)?;
            validate_territory_target(territory, valid_territory_ids)
        }
        RuntimeCondition::SpecialConditionThreshold { label, .. }
        | RuntimeCondition::CandidateAvailabilityThreshold { name: label, .. }
        | RuntimeCondition::NamedLocomotiveAvailabilityThreshold { name: label, .. }
        | RuntimeCondition::NamedLocomotiveCostThreshold { name: label, .. } => {
            if label.trim().is_empty() {
                Err("label must not be empty".to_string())
            } else {
                Ok(())
            }
        }
        RuntimeCondition::CargoProductionSlotThreshold { slot, label, .. } => {
            if !(1..=11).contains(slot) {
                Err("slot must be in 1..=11".to_string())
            } else if label.trim().is_empty() {
                Err("label must not be empty".to_string())
            } else {
                Ok(())
            }
        }
        RuntimeCondition::CargoProductionTotalThreshold { .. }
        | RuntimeCondition::FactoryProductionTotalThreshold { .. }
        | RuntimeCondition::FarmMineProductionTotalThreshold { .. }
        | RuntimeCondition::OtherCargoProductionTotalThreshold { .. }
        | RuntimeCondition::LimitedTrackBuildingAmountThreshold { .. }
        | RuntimeCondition::TerritoryAccessCostThreshold { .. }
        | RuntimeCondition::EconomicStatusCodeThreshold { .. } => Ok(()),
        RuntimeCondition::WorldFlagEquals { key, .. } => {
            if key.trim().is_empty() {
                Err("key must not be empty".to_string())
            } else {
                Ok(())
            }
        }
    }
}
