use crate::engine::metrics::{cargo_production_total_for_class, compare_condition_value};
use crate::event::conditions::RuntimeConditionComparator;
use crate::event::targets::RuntimeCargoClass;
use crate::state::RuntimeState;

pub(super) fn candidate_availability_threshold_matches(
    state: &RuntimeState,
    name: &str,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .candidate_availability
        .get(name)
        .copied()
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn named_locomotive_availability_threshold_matches(
    state: &RuntimeState,
    name: &str,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .named_locomotive_availability
        .get(name)
        .copied()
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn named_locomotive_cost_threshold_matches(
    state: &RuntimeState,
    name: &str,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .named_locomotive_cost
        .get(name)
        .copied()
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn cargo_production_slot_threshold_matches(
    state: &RuntimeState,
    slot: u32,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .cargo_production_overrides
        .get(&slot)
        .copied()
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn cargo_production_total_threshold_matches(
    state: &RuntimeState,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .cargo_production_overrides
        .values()
        .copied()
        .map(i64::from)
        .sum::<i64>();
    compare_condition_value(actual, comparator, value)
}

pub(super) fn cargo_production_class_threshold_matches(
    state: &RuntimeState,
    cargo_class: RuntimeCargoClass,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = cargo_production_total_for_class(state, cargo_class);
    compare_condition_value(actual, comparator, value)
}
