use super::context_targets::{
    lower_condition_true_company_target_in_company_target,
    lower_condition_true_player_target_in_player_target,
};
use super::territory::lower_territory_target_in_condition;
use crate::documents::{RuntimeImportBlocker, RuntimeImportContext};
use crate::event::conditions::RuntimeCondition;
use crate::event::targets::{RuntimeCompanyTarget, RuntimePlayerTarget};
use crate::inspect::smp::events::SmpLoadedPackedEventConditionRowSummary;

pub(crate) fn lower_condition_targets_in_condition(
    condition: &RuntimeCondition,
    row: &SmpLoadedPackedEventConditionRowSummary,
    lowered_company_target: Option<&RuntimeCompanyTarget>,
    lowered_player_target: Option<&RuntimePlayerTarget>,
    runtime_context: &RuntimeImportContext,
) -> Result<RuntimeCondition, RuntimeImportBlocker> {
    Ok(match condition {
        RuntimeCondition::WorldVariableThreshold {
            index,
            comparator,
            value,
        } => RuntimeCondition::WorldVariableThreshold {
            index: *index,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::CompanyNumericThreshold {
            target,
            metric,
            comparator,
            value,
        } => RuntimeCondition::CompanyNumericThreshold {
            target: lower_condition_true_company_target_in_company_target(
                target,
                lowered_company_target,
            )?,
            metric: *metric,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::CompanyVariableThreshold {
            target,
            index,
            comparator,
            value,
        } => RuntimeCondition::CompanyVariableThreshold {
            target: lower_condition_true_company_target_in_company_target(
                target,
                lowered_company_target,
            )?,
            index: *index,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::ChairmanNumericThreshold {
            target,
            metric,
            comparator,
            value,
        } => RuntimeCondition::ChairmanNumericThreshold {
            target: target.clone(),
            metric: *metric,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::PlayerVariableThreshold {
            target,
            index,
            comparator,
            value,
        } => RuntimeCondition::PlayerVariableThreshold {
            target: lower_condition_true_player_target_in_player_target(
                target,
                lowered_player_target,
            )?,
            index: *index,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::TerritoryNumericThreshold {
            target,
            metric,
            comparator,
            value,
        } => RuntimeCondition::TerritoryNumericThreshold {
            target: lower_territory_target_in_condition(target, row, runtime_context)?,
            metric: *metric,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::TerritoryVariableThreshold {
            target,
            index,
            comparator,
            value,
        } => RuntimeCondition::TerritoryVariableThreshold {
            target: lower_territory_target_in_condition(target, row, runtime_context)?,
            index: *index,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::CompanyTerritoryNumericThreshold {
            target,
            territory,
            metric,
            comparator,
            value,
        } => RuntimeCondition::CompanyTerritoryNumericThreshold {
            target: lower_condition_true_company_target_in_company_target(
                target,
                lowered_company_target,
            )?,
            territory: lower_territory_target_in_condition(territory, row, runtime_context)?,
            metric: *metric,
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::SpecialConditionThreshold {
            label,
            comparator,
            value,
        } => RuntimeCondition::SpecialConditionThreshold {
            label: label.clone(),
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::CandidateAvailabilityThreshold {
            name,
            comparator,
            value,
        } => RuntimeCondition::CandidateAvailabilityThreshold {
            name: name.clone(),
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::NamedLocomotiveAvailabilityThreshold {
            name,
            comparator,
            value,
        } => RuntimeCondition::NamedLocomotiveAvailabilityThreshold {
            name: name.clone(),
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::NamedLocomotiveCostThreshold {
            name,
            comparator,
            value,
        } => RuntimeCondition::NamedLocomotiveCostThreshold {
            name: name.clone(),
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::CargoProductionSlotThreshold {
            slot,
            label,
            comparator,
            value,
        } => RuntimeCondition::CargoProductionSlotThreshold {
            slot: *slot,
            label: label.clone(),
            comparator: *comparator,
            value: *value,
        },
        RuntimeCondition::CargoProductionTotalThreshold { comparator, value } => {
            RuntimeCondition::CargoProductionTotalThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::FactoryProductionTotalThreshold { comparator, value } => {
            RuntimeCondition::FactoryProductionTotalThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::FarmMineProductionTotalThreshold { comparator, value } => {
            RuntimeCondition::FarmMineProductionTotalThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::OtherCargoProductionTotalThreshold { comparator, value } => {
            RuntimeCondition::OtherCargoProductionTotalThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::LimitedTrackBuildingAmountThreshold { comparator, value } => {
            RuntimeCondition::LimitedTrackBuildingAmountThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::TerritoryAccessCostThreshold { comparator, value } => {
            RuntimeCondition::TerritoryAccessCostThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::EconomicStatusCodeThreshold { comparator, value } => {
            RuntimeCondition::EconomicStatusCodeThreshold {
                comparator: *comparator,
                value: *value,
            }
        }
        RuntimeCondition::WorldFlagEquals { key, value } => RuntimeCondition::WorldFlagEquals {
            key: key.clone(),
            value: *value,
        },
    })
}
