Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var fs_extra_1 = require("fs-extra");
var os_1 = require("os");
var path = require("path");
var os = require("os");
var bnj = require("bonjour");
var FileHandler_1 = require("./FileHandler");
var WebServer_1 = require("./WebServer");
var SocketServer_1 = require("./SocketServer");
var DeckManager_1 = require("./Decks/DeckManager");
var PageManager_1 = require("./Pages/PageManager");
var IntegrationsManager_1 = require("./Integrations/IntegrationsManager");
var ConnectionManager_1 = require("./ConnectionManager");
var Icons_1 = require("./Icons");
var Logger_1 = require("./Logger");
var bonjour = bnj();
Undecked = {
    DEVMODE: process.argv.includes('--dev'),
    quality: 96,
    dataPath: path.join((0, os_1.homedir)(), 'MorphixProductions', 'Undecked'),
    FileHandler: new FileHandler_1.FileHandler(),
    WebServer: new WebServer_1.WebServer(),
    SocketServer: new SocketServer_1.SocketServer(),
    Decks: new DeckManager_1.DeckManager(),
    Pages: new PageManager_1.PageManager(),
    Integrations: new IntegrationsManager_1.IntegrationsManager(),
    Connections: new ConnectionManager_1.ConnectionManager(),
    Icons: new Icons_1.Icons(),
    getName: function () {
        if (Config.name != null)
            return Config.name;
        if (os.userInfo().username.length > 0)
            return os.userInfo().username;
        return os.hostname();
    }
};
Undecked.start = function () {
    (0, Logger_1.Log)('info', 'Starting Undecked');
    var start = function () {
        return Undecked.FileHandler.load(function () {
            Undecked.Decks.load(function () {
                Undecked.Icons.load(function () {
                    Undecked.Pages.load(function () {
                        Undecked.Integrations.load(function () {
                            Undecked.Connections.load(function () {
                                Undecked.WebServer.start(function (server) {
                                    Undecked.SocketServer.start(server, function () {
                                        (0, Logger_1.Log)('info', 'Undecked started');
                                        try {
                                            bonjour.publish({
                                                name: Undecked.getName(),
                                                type: 'undecked',
                                                port: Config.ports.http
                                            });
                                        }
                                        catch (error) { }
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    };
    (0, fs_extra_1.pathExists)(path.join((0, os_1.homedir)(), 'GetiyoSoftware', 'Undecked'), function (err, exists) {
        if (err)
            throw err;
        if (exists)
            (0, fs_extra_1.move)(path.join((0, os_1.homedir)(), 'GetiyoSoftware', 'Undecked'), Undecked.dataPath, function (err) {
                if (err)
                    throw err;
                start();
            });
        else
            start();
    });
};
Undecked.generateRandom = function (length, checksum, chars) {
    if (chars === void 0) { chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; }
    for (var i = 0; i < 1000; i++) {
        var str = '';
        for (var i_1 = 0; i_1 < length; i_1++) {
            str += chars.charAt(Math.floor(Math.random() * chars.length));
        }
        if (checksum != undefined)
            if (checksum(str))
                return str;
            else
                return str;
    }
};
Undecked.convert = function () {
    var outputWhiteDir = path.join(__filename, '..', '..', '..', 'Static', 'materialicons', 'white');
    var outputBlackDir = path.join(__filename, '..', '..', '..', 'Static', 'materialicons', 'black');
    var inputWhiteDir = path.join("C:\\Users\\Mees\\Pictures\\materialicons\\white");
    var inputBlackDir = path.join("C:\\Users\\Mees\\Pictures\\materialicons\\black");
    (0, fs_extra_1.ensureDir)(outputWhiteDir, function (err) {
        if (err)
            throw err;
        (0, fs_extra_1.ensureDir)(inputBlackDir, function (err) {
            if (err)
                throw err;
            (0, fs_1.readdir)(path.join(inputBlackDir), function (err, files) {
                if (err)
                    throw err;
                console.log("Found ".concat(files.length, " icons"));
                (function handleCopy(i) {
                    if (i === void 0) { i = 0; }
                    if (files[i]) {
                        var inputWhite = path.join(inputWhiteDir, files[i], 'sharp.png');
                        var inputBlack = path.join(inputBlackDir, files[i], 'sharp.png');
                        var outputWhite = path.join(outputWhiteDir, "".concat(files[i], "_low.png"));
                        var outputBlack = path.join(outputBlackDir, "".concat(files[i], "_low.png"));
                        (0, fs_extra_1.copy)(inputWhite, outputWhite, function (err) {
                            if (err)
                                console.log("Error whilst copying white '".concat(files[i], "'"));
                            (0, fs_extra_1.copy)(inputBlack, outputBlack, function (err) {
                                if (err)
                                    console.log("Error whilst copying black '".concat(files[i], "'"));
                                console.log("Completed convertion on ".concat(i + 1, "/").concat(files.length));
                                handleCopy(i + 1);
                            });
                        });
                    }
                    else
                        console.log('--- Convertion completed ---');
                })();
            });
        });
    });
};
Undecked.start();
//# sourceMappingURL=Core.js.map