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.Room;
import thb.jeanluc.adventure.model.item.Item;

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

/**
 * Picks an item up from the current room and places it in the inventory.
 * Usage: {@code take <item-id>}.
 */
public class TakeCommand implements Command {

    @Override
    public void execute(GameContext ctx, List<String> args) {
        if (args.isEmpty()) {
            ctx.getIo().write("Take what?");
            return;
        }
        if (!Light.isLit(ctx, ctx.getPlayer().getCurrentRoom())) {
            ctx.getIo().write("It's too dark to see that.");
            return;
        }
        String itemId = args.getFirst();
        Room room = ctx.getPlayer().getCurrentRoom();
        Optional<Item> taken = room.removeItem(itemId);
        if (taken.isEmpty()) {
            ctx.getIo().write("There is no '" + itemId + "' here.");
            return;
        }
        ctx.getPlayer().addItem(taken.get());
        ctx.getIo().write("You take the " + taken.get().getName() + ".");
    }

    @Override
    public String help() {
        return "take <item>  -  pick up an item from the room (alias: pick, get)";
    }
}
