package thb.jeanluc.adventure.command.impl;

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

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

/**
 * Drops an item from the inventory back into the current room.
 * Usage: {@code drop <item-id>}.
 */
public class DropCommand implements Command {

    @Override
    public void execute(GameContext ctx, List<String> args) {
        if (args.isEmpty()) {
            ctx.getIo().write("Drop what?");
            return;
        }
        String itemId = args.getFirst();
        Optional<Item> dropped = ctx.getPlayer().removeItem(itemId);
        if (dropped.isEmpty()) {
            ctx.getIo().write("You are not carrying '" + itemId + "'.");
            return;
        }
        ctx.getPlayer().getCurrentRoom().addItem(dropped.get());
        ctx.getIo().write("You drop the " + dropped.get().getName() + ".");
    }

    @Override
    public String help() {
        return "drop <item>  -  put an item from your inventory into the room";
    }
}
