import { CreateCommand } from "../commands/create-command";
import { Command } from "../commands/command";

export class CommandManager {
    protected prefixRegex = /papoh\s*/g;
    protected optionRegex = /\-{1,2}\w+(\s*)/g
    protected commandRegex = /^papoh\s*\w+\s*/g;
    protected createCommand: CreateCommand;


    constructor(mainChannel: any) {
        this.createCommand = new CreateCommand(mainChannel);
    }
    
    public extractCommandFromMessage(msg: string): string | null {

        let command = msg.match(this.commandRegex)
        if (Array.isArray(command)) {
            return command[0].trim().replace(this.prefixRegex, '');
        }
        return null;
    }

    public extractOptionsFromMessage(msg: string): string[] | null {
        let options = msg.replace(this.commandRegex, '');
        let optionPairs = options.match(/\-{1,2}\w+(?:\s*)\"{0,1}(?:\w*\s*\d*)*\"{0,1}/g);
        let optionArray = [];
        if (Array.isArray(optionPairs)) {
            for (const optionPair of optionPairs) {
                let optionPairMatch = optionPair.match(/(\-{1,2}\w+)\s*(.*)/);
                if (Array.isArray(optionPairMatch)) {
                    optionArray.push(optionPairMatch[1]);
                    optionArray.push(optionPairMatch[2].replace(/"/g, ''));
                }
            }
        }
        return optionArray;
    }

    public getCommandClass(name: string): Command | null {
        switch (name) {
            case "create":
                return this.createCommand;
            default:
                return null;
        }
    }

}