package thb.jeanluc.adventure.game;

import thb.jeanluc.adventure.model.Condition;

import java.util.List;

/** Evaluates {@link Condition}s against a {@link GameContext}. */
public final class Conditions {

    private Conditions() {
    }

    public static boolean holds(Condition c, GameContext ctx) {
        return switch (c.type()) {
            case FLAG -> ctx.getState().isSet(c.arg());
            case NOT_FLAG -> !ctx.getState().isSet(c.arg());
            case HAS_ITEM -> ctx.getPlayer().hasItem(c.arg());
        };
    }

    /** True iff every condition holds. A null or empty list is vacuously true. */
    public static boolean all(List<Condition> conditions, GameContext ctx) {
        if (conditions == null) {
            return true;
        }
        for (Condition c : conditions) {
            if (!holds(c, ctx)) {
                return false;
            }
        }
        return true;
    }
}
