import CommandBuilder from "../builders/command";

function takeHandler(args, context) {
    const room = context.getRoom(context.player.currentRoom);
    const items = room.getItems();
    let item = null;
    for (let i of items) {
        if (i.name.includes(args[1])) {
            item = i;
            break;
        }
    }
    if (!item) {
        context.print(`You can't find any ${args[1]}.`);
    } else {
        if (!item.takeable) {
            context.print(`You can't take ${item.name}.`);
        } else {
            room.removeItem(item.id);
            context.player.addItem(item.id);
            context.print(`You take ${item.name}.`);
            item.onTake();
            
            // Refresh mobile commands when items change
            context.commandHandler.refreshMobileCommands();
        }
    }
}

export default new CommandBuilder()
    .withName("take")
    .withAliases(["get"])
    .withCategory("Actions")
    .withDescription("Take an object")
    .withArgument("item", "target", false, "Object to take")
    .withHandler(takeHandler)
    .create();