import LookCommand from "./commands/look";
import UseCommand from "./commands/use";
import TakeCommand from "./commands/take";
import DropCommand from "./commands/drop";
import EchoCommand from "./commands/echo";
import SaveCommand from "./commands/save";
import LoadCommand from "./commands/load";
import VolumeCommand from "./commands/volume";
import InventoryCommand from "./commands/inventory";

const defaultCommands = [
    [["look", "l"], LookCommand],
    [["use", "interact"], UseCommand],
    [["take", "get"], TakeCommand],
    [["drop", "put"], DropCommand],
    ["echo", EchoCommand],
    ["save", SaveCommand],
    ["load", LoadCommand],
    ["volume", VolumeCommand],
    [["i", "inv", "inventory"], InventoryCommand]
];

const directionMap = [
    ["n", "north"],
    ["ne", "northeast"],
    ["e", "east"],
    ["se", "southeast"],
    ["s", "south"],
    ["sw", "southwest"],
    ["w", "west"],
    ["nw", "northwest"],
    ["u", "up"],
    ["d", "down"]
];

export default class Commands {
    constructor(context, commands) {
        this.context = context;
        this.commands = commands || new Map();
        this.enabled = true;
        this.addDefaultCommands();
    }

    doCommand(str) {
        if (!this.enabled) {
            this.context.print(`You can't seem to do anything at the moment.`);
            return;
        }
        const room = this.context.getRoom(this.context.player.currentRoom);
        const split = str.split(" ");
        if (this.commands.get(split[0])) {
            this.commands.get(split[0])(split, this.context);
        }
        
        const direction = this.matchDirection(split[0]);

        if (room.getExit(direction)) {
            this.context.move(room.getExit(direction));
        }
    }

    addCommand(name, func) {
        if (Array.isArray(name)) {
            name.forEach((command) => this.commands.set(command, func));
        } else {
            this.commands.set(name, func);
        }
    }

    addCommands(commands) {
        commands.forEach((command) => {
            this.addCommand(command[0], command[1]);
        });
    }

    addDefaultCommands() {
        this.addCommands(defaultCommands);
    }

    matchDirection(str) {
        for (let dir of directionMap) {
            if (dir[0] == str) return dir[1];
        }
    }
}