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

async function useHandler(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) {
        const items = context.player.getInventory();
        for (let i of items) {
            if (i.name.includes(args[1])) {
                item = i;
                break;
            }
        }
    }
    if (!item) {
        context.output.say(`I could not find a ${args[1]}.`);
    } else {
        await item.onUse();
    }
}

export default new CommandBuilder()
    .withName("use")
    .withAliases(["interact"])
    .withCategory("Actions")
    .withDescription("Use an object")
    .withArgument("item", "target", false, "Object to use")
    .withHandler(useHandler)
    .isAsync(true)
    .create();