package thb.jeanluc.adventure.command.impl;

import thb.jeanluc.adventure.command.Command;
import thb.jeanluc.adventure.game.GameContext;
import thb.jeanluc.adventure.game.Light;
import thb.jeanluc.adventure.model.Npc;
import thb.jeanluc.adventure.model.item.Item;

import java.util.List;
import java.util.Optional;

/**
 * Shows the description of an item or NPC. The target may be in the
 * player's inventory or in the current room. Usage: {@code examine <id>}.
 */
public class ExamineCommand implements Command {

    @Override
    public void execute(GameContext ctx, List<String> args) {
        if (args.isEmpty()) {
            ctx.getIo().write("Examine what?");
            return;
        }
        if (!Light.isLit(ctx, ctx.getPlayer().getCurrentRoom())) {
            ctx.getIo().write("It's too dark to see that.");
            return;
        }
        String id = args.getFirst();
        Optional<Item> item = ctx.getPlayer().findItem(id);
        if (item.isEmpty()) {
            item = ctx.getPlayer().getCurrentRoom().findItem(id);
        }
        if (item.isPresent()) {
            ctx.getIo().write(item.get().getDescription());
            return;
        }
        Optional<Npc> npc = ctx.getPlayer().getCurrentRoom().findNpc(id);
        if (npc.isPresent()) {
            ctx.getIo().write(npc.get().getDescription());
            return;
        }
        ctx.getIo().write("There is no '" + id + "' here.");
    }

    @Override
    public String help() {
        return "examine <target>  -  inspect an item or NPC (alias: x)";
    }
}
