var node = require('./lib/node');
var express = require('./lib/express');
var mysql = require('./lib/mysql');
var redis = require('./lib/redis');
var comm = require('./lib/comm/index');

var g_app = {
    v_upload: {},
    v_singleCoreProcess: false,
    m_apparatus: comm.apparatus,
    m_convert: comm.convert,
    m_cryptogram: comm.cryptogram,
    m_encode: comm.encode,
    m_obj: comm.obj,
    m_obtain: comm.obtain,
    m_request: comm.request,
    m_verify: comm.verify,
    m_ucpaas: require('./lib/ucpaas-sms/index.js'),
    m_wechat : require('./lib/wechat.js')
};

if (process.argv) {
    if (process.argv.length) {
        g_app.v_singleCoreProcess = process.argv[process.argv.length - 1].indexOf('-d') == 0 ? true : false
    }
    else {
        g_app.v_singleCoreProcess = process.argv[0].indexOf('-d') == 0 ? true : false
    }
}

var app = function () {
    return g_app;
};
exports.app = g_app;

//运行Express
var runExpress = function (conf, middleware) {
    express.runExpress(conf, middleware);
}

//运行Node
var runNode = function (conf, all, middleware) {
    node.runNode(conf, all, middleware);
}
exports.runNode = runNode;

//运行Mysql
var runMysql = function (conf) {
    g_app.m_mysql = mysql.runMysql(conf);
    return g_app.mysql;
}
exports.runMysql = runMysql;

//运行Redis
var runRedis = function (conf) {
    g_app.m_redis = redis.runRedis(conf);
    return g_app.m_redis;
}
exports.runRedis = runRedis;

//运行所有服务
var runAllServer = function (conf, middleware) {
    runNode(conf, true, middleware);
}
exports.runAllServer = runAllServer;

//重写console的log、info、warn、error方法
(function () {
    setTimeout(function () {
        var ismain = false;
        var c_log = console.log;
        console.log = function () {
            var date = (new Date()).Format();
            c_log(['------[LOG PID:' + process.pid.toString() + (ismain ? '(MAIN)' : '') + ' TIME:' + date + ']------']);
            for (var key in arguments) {
                c_log(arguments[key]);
            }
            c_log(['------[LOG PID:' + process.pid.toString()+ (ismain ? '(MAIN)' : '') + ' TIME:' + date + ']------']);
            c_log();
        };
        var c_info = console.info;
        console.info = function () {
            var date = (new Date()).Format();
            c_info(['------[INFO PID:' + process.pid.toString() + ' TIME:' + date + ']------']);
            for (var key in arguments) {
                c_info(arguments[key]);
            }
            c_info(['------[INFO PID:' + process.pid.toString() + ' TIME:' + date + ']------']);
            c_log();
        };
        var c_warn = console.warn;
        console.warn = function () {
            var date = (new Date()).Format();
            c_warn(['------[WARN PID:' + process.pid.toString() + ' TIME:' + date + ']------']);
            for (var key in arguments) {
                c_warn(arguments[key]);
            }
            c_warn(['------[WARN PID:' + process.pid.toString() + ' TIME:' + date + ']------']);
            c_log();
        };
        var c_error = console.error;
        console.error = function () {
            var date = (new Date()).Format();
            c_error(['------[ERROR PID:' + process.pid.toString() + ' TIME:' + date + ']------']);
            for (var key in arguments) {
                c_error(arguments[key]);
            }
            c_error(['------[ERROR PID:' + process.pid.toString() + ' TIME:' + date + ']------']);
            c_log();
        };
    }, 5000);
})();