package thb.jeanluc.adventure.game;

import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.item.Item;
import thb.jeanluc.adventure.model.item.SwitchableItem;

import java.util.Collection;

/** Decides whether a room is lit, given dark rooms and active light sources. */
public final class Light {

    private Light() {
    }

    /** A room is lit if it is not dark, or an active light source is carried or present. */
    public static boolean isLit(GameContext ctx, Room room) {
        if (!room.isDark()) {
            return true;
        }
        return hasActiveLight(ctx.getPlayer().getInventory().values())
                || hasActiveLight(room.getItems().values());
    }

    /** True if the player carries an active light source (for the HUD). */
    public static boolean carryingLight(GameContext ctx) {
        return hasActiveLight(ctx.getPlayer().getInventory().values());
    }

    private static boolean hasActiveLight(Collection<Item> items) {
        for (Item it : items) {
            if (isActiveLight(it)) {
                return true;
            }
        }
        return false;
    }

    private static boolean isActiveLight(Item it) {
        if (!it.isLight()) {
            return false;
        }
        return !(it instanceof SwitchableItem s) || s.isOn();
    }
}
