let { logger } = require('./logging');
let { modules } = require('./module/index');
let trie = require('trie-prefix-tree');
let { getShortestPrefix } = require('./utility');
let help = require('./module/help');
let message = require('./message');
let utility = require('./utility');

let sentinelValue = '!';

class Engine {
    constructor(config, bot, modules) {
        this.config = config;
        this.bot = bot;
        this.modules = modules;
        this.moduleMap = new Map();
        this.commandMap = new Map();
        this.commandRadixTree = trie([]);
    }

    initModules() {
        this.modules.forEach((module) => {
            logger.info("Loading module: %s", module.name);
            this.moduleMap.set(module.command, module);
            this.commandMap.set(module.command, module);
            this.commandRadixTree.addWord(module.command);
        });

        this.modules.forEach((module) => {
            let shortCommand = getShortestPrefix(this.commandRadixTree, module.command, 3);
            logger.info("Adding short command %s for module: %s", shortCommand, module.name);
            this.commandMap.set(shortCommand, module);
        });

        this.helpModule = help.create(this.moduleMap)
        this.commandMap.set('help', this.helpModule);

        logger.info("Bound modules to keywords: %o", this.moduleMap);
    }

    init() {
        logger.info("Initializing modules");
        this.initModules();

        /* Bind Message Parsing */
        let engine = this;
        let handleMessages = (event, room, toStartOfTimeline) => {
            /* Don't process messages from self */
            if (event.sender.userId !== this.config.userId) {
                /* don't process messages that aren't of type m.room.message */
                if (event.getType() !== "m.room.message") {
                    logger.debug("Recieved message of type: %s", event.getType());
                    return;
                } else {
                    let messageBody = event.event.content.body;
                    logger.debug("[%s] %s", room.name, messageBody);
                    if (messageBody.indexOf(sentinelValue) === 0) {
                        let command = messageBody.split(' ')[0].substring(1);
                        var responseMessage = null;
                        if (engine.commandMap.has(command)) {
                            responseMessage = engine.commandMap.get(command).handleMessage(event, room);
                        } else {
                            responseMessage = engine.helpModule.help();
                        }

                        logger.debug("Responding to room: %s", room.roomId);
                        if (responseMessage instanceof Object) {
                            engine.bot.client.sendMessage(room.roomId, responseMessage);
                        } else if (utility.isString(responseMessage)) {
                            engine.bot.client.sendMessage(room.roomId, message.createBasic(responseMessage));
                        } else {
                            logger.error("Unable to process response message: %s", responseMessage);
                        }
                    }
                }
            }
        }

        this.bot.init(handleMessages);

        /* Capture Exit Conditions */

        ["SIGINT", "SIGTERM"].forEach((signature) => {
            process.on(signature, async () => {
                await this.bot.sendStatusShutdown()
                    .then(() => {
                        logger.info("Gracefully stopping Matrix SDK Client")
                        this.bot.client.stopClient();
                    });
                process.exit(0);
            });
        });

        process.on('exit', () => {
            logger.info("Shutting Down");
        });

        return this;
    }

    run() {
        this.bot.connect();
        return this;
    }
}

function create(config, bot) {
    return new Engine(config, bot, modules)
}

exports.create = create;