"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const figlet_1 = require("figlet");
const blessed_1 = __importDefault(require("blessed"));
const compilation_status_1 = require("../lerna/enums/compilation.status");
const service_status_1 = require("../lerna/enums/service.status");
exports.screen = blessed_1.default.screen({ smartCSR: true, title: 'Microlambda v0.0.6-alpha' });
exports.splashScreen = () => {
    const box = blessed_1.default.box({
        top: 'center',
        left: 'center',
        content: figlet_1.textSync('MicroLambda', {
            font: 'Small',
            horizontalLayout: 'default',
            verticalLayout: 'default',
        }) + `\nv.0.0.6alpha`,
        tags: true,
        style: {
            fg: 'magenta',
        }
    });
    exports.screen.append(box);
    exports.screen.render();
    return new Promise((resolve) => {
        setTimeout(() => {
            initWidgets();
            resolve();
        }, 2000);
    });
};
const eventsLog = blessed_1.default.log({
    top: '0',
    left: '50%',
    width: 'half',
    height: '100%',
    mouse: true,
});
const packagesList = blessed_1.default.list({
    top: '0',
    left: '0',
    width: 'half',
    height: 'half',
    style: {
        item: { bg: 'blue' },
    }
});
const servicesList = blessed_1.default.list({
    top: '50',
    left: '0',
    width: 'half',
    height: '100%',
    style: {
        item: { bg: 'green' },
    }
});
const getCompilationStatus = (node) => {
    if (!node.isEnabled()) {
        return chalk_1.grey('[Disabled]');
    }
    switch (node.getCompilationStatus()) {
        case compilation_status_1.CompilationStatus.COMPILED:
            return chalk_1.green('[Compiled]');
        case compilation_status_1.CompilationStatus.COMPILING:
            return chalk_1.cyan('[Compiling]');
        case compilation_status_1.CompilationStatus.ERROR_COMPILING:
            return chalk_1.red('[Error compiling]');
        case compilation_status_1.CompilationStatus.NOT_COMPILED:
            return '[Not compiled]';
    }
};
const formatPackage = (pkg) => {
    const name = chalk_1.bold(pkg.getName());
    return `${name} ${getCompilationStatus(pkg)} ${chalk_1.grey(' @ ' + new Date().toISOString())}`;
};
const formatService = (service) => {
    const name = chalk_1.bold(service.getName());
    const getStatus = () => {
        if (!service.isEnabled()) {
            return chalk_1.grey('[Disabled]');
        }
        switch (service.getStatus()) {
            case service_status_1.ServiceStatus.CRASHED:
                return chalk_1.bgRed('[Crashed]');
            case service_status_1.ServiceStatus.RUNNING:
                return chalk_1.green('[Running]');
            case service_status_1.ServiceStatus.STARTING:
                return chalk_1.cyan('[Starting]');
            case service_status_1.ServiceStatus.STOPPED:
                return chalk_1.red('[Stopped]');
            case service_status_1.ServiceStatus.STOPPING:
                return chalk_1.yellow('[Stopping]');
        }
    };
    return `${name} ${getStatus()} ${chalk_1.grey(' @ ' + new Date().toISOString())}`;
};
exports.setPackages = (packages) => {
    packages.forEach((pkg) => {
        packagesList.add(formatPackage(pkg));
    });
};
exports.setServices = (services) => {
    services.forEach((pkg) => {
        packagesList.add(formatService(pkg));
    });
};
const initWidgets = () => {
    exports.screen.realloc();
    exports.screen.append(eventsLog);
    exports.screen.append(packagesList);
    exports.screen.append(servicesList);
    exports.screen.render();
};
exports.addLog = (txt) => {
    eventsLog.add(txt);
};
exports.pipeToEventLog = (process, _scope) => {
    process.stdout.on('data', (data) => eventsLog.add(data.toString()));
    process.stderr.on('data', (data) => eventsLog.add(data.toString()));
};
//# sourceMappingURL=blessed.js.map