package thb.jeanluc.adventure.command.impl;

import thb.jeanluc.adventure.command.Command;
import thb.jeanluc.adventure.game.Conditions;
import thb.jeanluc.adventure.game.GameContext;
import thb.jeanluc.adventure.game.Light;
import thb.jeanluc.adventure.io.text.RoomView;
import thb.jeanluc.adventure.model.DescriptionState;
import thb.jeanluc.adventure.model.Direction;
import thb.jeanluc.adventure.model.Npc;
import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.item.Item;

import java.util.List;

/**
 * Describes the current room by emitting a {@link RoomView}; the active
 * frontend decides how to render it. Usage: {@code look}.
 */
public class LookCommand implements Command {

    @Override
    public void execute(GameContext ctx, List<String> args) {
        Room room = ctx.getPlayer().getCurrentRoom();
        if (!Light.isLit(ctx, room)) {
            ctx.getIo().write("It's pitch black; you can't make anything out. You need a light source.");
            return;
        }
        List<String> items = room.getItems().values().stream().map(Item::getName).toList();
        List<String> npcs = room.getNpcs().values().stream().map(Npc::getName).toList();
        List<String> exits = room.getExits().keySet().stream().map(Direction::getLabel).toList();
        String description = room.getDescription();
        for (DescriptionState ds : room.getDescriptionStates()) {
            if (Conditions.all(ds.when(), ctx)) {
                description = ds.text();
                break;
            }
        }
        ctx.getIo().showRoom(new RoomView(room.getName(), description, items, npcs, exits));
    }

    @Override
    public String help() {
        return "look  -  describe the current room";
    }
}
