use crate::engine::metrics::compare_condition_value;
use crate::event::conditions::RuntimeConditionComparator;
use crate::state::RuntimeState;

pub(super) fn world_variable_threshold_matches(
    state: &RuntimeState,
    index: u32,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .world_runtime_variables
        .get(&index)
        .copied()
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

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

pub(super) fn limited_track_building_amount_threshold_matches(
    state: &RuntimeState,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .world_restore
        .limited_track_building_amount
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn territory_access_cost_threshold_matches(
    state: &RuntimeState,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .world_restore
        .territory_access_cost
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn economic_status_code_threshold_matches(
    state: &RuntimeState,
    comparator: RuntimeConditionComparator,
    value: i64,
) -> bool {
    let actual = state
        .world_restore
        .economic_status_code
        .map(i64::from)
        .unwrap_or(0);
    compare_condition_value(actual, comparator, value)
}

pub(super) fn world_flag_equals(state: &RuntimeState, key: &str, value: bool) -> bool {
    state.world_flags.get(key).copied().unwrap_or(false) == value
}
