/**
 * Help module
 */

let { AbstractModule } = require('./abstract');
let { logger } = require('../logging');

class HelpModule extends AbstractModule {
    constructor(commandMap) {
        super(
            "Help",
            "Provide helpful information about other modules.",
            "help"
        );
        this._commandMap = commandMap;
        this._commandList = Array.from(commandMap.keys()).sort();
        this.defaultCommand = 'help';
    }

    _default_help_message() {
        let help = `!help <command>`;
        for (let command of this._commandList) {
            help += "\n!help " + command + " : " + this._commandMap.get(command).description;
        }
        return help;
    }

    cmd_help(...args) {
        logger.debug("%o", args)
        if (args.length < 1) {
            return this._default_help_message();
        } else {
            let command = args[0];
            logger.debug("Looking up help for %s from %o", command, this._commandMap);
            if (this._commandList.includes(command)) {
                return this._commandMap.get(command).cmd_help();
            } else {
                let help = command + " is an unrecognized module\n";
                help += this._default_help_message();
                return help;
            }
        }
    }
}

function create(commandMap) {
    return new HelpModule(commandMap);
}

exports.create = create;