"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
var _ = require('lodash'), express = require('express'), compression = require('compression'), cons = require('consolidate'), fs = require('fs'), http = require('http'), util = require('util'), app;
// var SystemJS = require('systemjs');
var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    if ('OPTIONS' == req.method) {
        res.send(200);
    }
    else {
        next();
    }
};
exports.init = function (options) {
    app = express();
    app.engine('html', cons.underscore);
    app.set('views', options.views);
    app.set('view engine', 'underscore');
    app.set("jsonp callback", true);
    app.use(compression());
    app.use(express.static(options.baseDir + options.server_static_dir));
    app.use(allowCrossDomain);
    //app.use(express.methodOverride());
    //app.use(express.bodyParser());
    //app.use(app.router);
    app.listen(options.port);
    app.appSettings = {};
    console.log('app_server: listening on port ' + options.port);
    process.on('uncaughtException', function (e) {
        console.log('Uncaught exception:' + e);
    });
    app.proxy = function (req, res) {
        var file = options.baseDir + '/' + req.query.file;
        fs.readFile(file, 'utf8', function (err, data) {
            if (err) {
                console.log('Error: ' + err);
                res.json(err);
                return;
            }
            res.send(data);
        });
    };
    app.serveTemplate = function (req, res) {
        fs.readFile(app.get('views') + '/' + req.params.name, function (err, html) {
            if (err) {
                res.json(err);
                return;
            }
            res.writeHeader(200, { "Content-Type": "text/html" });
            res.write(html);
            res.end();
        });
    };
    app.serveCss = function (req, res) {
        fs.readFile(options.baseDir + '/css/' + req.params.name, function (err, css) {
            if (err) {
                res.json(err);
                return;
            }
            res.writeHeader(200, { "Content-Type": "text/css" });
            res.write(css);
            res.end();
        });
    };
    app.loadAppSettings = function (file, callback) {
        module.exports.loadAppSettings(file, function (settings) {
            for (var k in settings) {
                app.appSettings[k] = settings[k];
            }
            callback.call(this, app.appSettings);
        });
    };
    if (options.dbUri && options.dbUri != '') {
        this.dbConnect.call(this, options.dbUri, options.callback);
    }
    else {
        options.callback.call(this, app);
    }
    // todo:
    //    if(options.config_file && options.config_file != ""){
    //
    //    }
};
// utilities
exports.loadAppSettings = function (file, callback) {
    var settings = {};
    fs.readFile(file, function (err, data) {
        var obj = JSON.parse(data);
        for (var k in obj) {
            settings[k] = castToType(obj[k]);
        }
        callback.call(this, settings);
    });
};
var castToType = function (o) {
    var value = o;
    if (Number(value) && value !== "true" && value !== "false") {
        value = Number(value);
    }
    if (value === "0") {
        value = 0;
    }
    if (value === "true") {
        value = true;
    }
    if (value === "false") {
        value = false;
    }
    return value;
};
//# sourceMappingURL=at-server-boot.js.map