//
// Created by xajhu on 2021/7/5 0005.
//
#include <uv.h>
#include <unistd.h>

#include "init.h"
#include "config.h"
#include "misc.h"
#include "common.h"
#include "user_errno.h"
#include "task_manager.h"
#include "uthash/utstring.h"
#include "banner.h"
#include "inet_misc.h"
#include "crypto.h"
#include "hardware.h"
#include "http_svr.h"
#include "lib_config.h"
#include "prj_config.h"
#include "zlog_module.h"
#ifdef SQLITE_ON
#include "database.h"
#endif
#ifdef ZEROMQ_ON
#include "msg_queue.h"
#endif

#define DEFAULT_CONFIG_FILE ("vcpe.cfg")
#define DEFAULT_CONFIG_DIR  ("config")

static pid_t g_pid;
static int   g_isInited = FALSE;

static void catch_system_interupt(int sig_num) {
    if (g_pid == uv_os_getpid()) {
        printf("\n");
        LOG_MOD(warn, ZLOG_MOD_INIT, "(%d): System close, clearing system resources..........\n\n", sig_num);
        task_manager_exit();
        sleep(1);
    }
}

int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pKey, int logLevel) {
    int        ret;
    UT_string *pPath;
    char       bufCfgFile[MAX_PATH];
    char       bufCfgDir[MAX_PATH];
    char       pidfile[MAX_PATH];

    g_pid = uv_os_getpid();

    signal(SIGINT, catch_system_interupt);
    signal(SIGABRT, catch_system_interupt);
    signal(SIGTERM, catch_system_interupt);
    signal(SIGQUIT, catch_system_interupt);
    signal(SIGTSTP, catch_system_interupt);
    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    signal(SIGKILL, catch_system_interupt);

    // 初始化 libuv loop
    get_task_manager();

    memset(bufCfgDir, 0, MAX_PATH);
    memset(bufCfgFile, 0, MAX_PATH);

    if (pCfgDirectory == NULL) {
        sprintf(bufCfgDir, "%s/%s", get_cur_process_dir(), DEFAULT_CONFIG_DIR);
    } else {
        strcpy(bufCfgDir, pCfgDirectory);
    }

    if (pAppCfgFile == NULL || strlen(pAppCfgFile) == 0) {
        const char *pTmp = strdup(bufCfgDir);
        sprintf(bufCfgFile, "%s/%s", pTmp, DEFAULT_CONFIG_FILE);
        free((void *)pTmp);
    } else {
        strcpy(bufCfgFile, pAppCfgFile);
    }

    // 初始化zlog库,系统集成日志功能
    utstring_new(pPath);
    utstring_printf(pPath, "%s/%s", bufCfgDir, "zlog.conf");

    if (file_exists(utstring_body(pPath))) {
        if ((ret = zlog_init(utstring_body(pPath))) != ERR_SUCCESS) {
            printf("Zlog configure file [%s] init result: %d+++++\n", utstring_body(pPath), ret);
            zlog_profile();
            return -ERR_ZLOG_INIT;
        } else {
            LOG_MOD(info, ZLOG_MOD_INIT, "Zlog used configure file [%s]\n", utstring_body(pPath));
            dzlog_init(utstring_body(pPath), get_cur_process_name());
            //            zlog_level_switch(zlog_get_category(get_cur_process_name()),
            //                              logLevel > ZLOG_LEVEL_DEBUG ? logLevel : ZLOG_LEVEL_DEBUG);
        }
    } else {
        printf("Zlog configure file [%s] not found, Zlog system not work+++++\n", utstring_body(pPath));
    }

    utstring_free(pPath);

    // 处置化配置文件库,系统集成配置文件支持功能
    LOG_MOD(info, ZLOG_MOD_INIT, "System used configure file [%s]\n", bufCfgFile);
    if ((ret = init_config_system(bufCfgFile, pKey)) != ERR_SUCCESS) {
        LOG_MOD(error, ZLOG_MOD_INIT, "Load system configuration error: %d\n", ret);
        return -ERR_CONFIG_INIT;
    }

    LOG_MOD(info, ZLOG_MOD_INIT, "%s library version %s information: %s (Build: %s %s  GCC Ver:%s) With %lu(bits) OS\n",
            VCPE_LIB_NAME, VCPE_LIB_VER, VCPE_GIT_VERSION, __DATE__, __TIME__, __VERSION__, sizeof(int *) * 8);

    LOG_MOD(info, ZLOG_MOD_INIT, "Application build configure: [%s]\n", VCPE_BUILD_CONFIG);

#ifdef USERVNI_ON
    sprintf(pidfile, "/tmp/%d_vcpe.pid", cfg_get_user_vni_id());
#else
    sprintf(pidfile, "/tmp/vcpe.pid");
#endif

    if (process_lock_pidfile(pidfile) == -ERR_FILE_LOCKED) {
        LOG_MOD(error, ZLOG_MOD_INIT, "!!!Another same process is running!!!, system exit......\n");
        exit(-ERR_FILE_LOCKED);
    }

    if (cfg_get_banner_enable()) {
        banner_show();
    }

    evp_system_init();
    inet_api_init();

    if (cfg_get_hardware_watch_enable()) {
        init_hardware();
    }
#ifdef SQLITE_ON
    db_sqlite3_init();
#endif

#ifdef ZEROMQ_ON
    if ((ret = mq_init()) != ERR_SUCCESS) {
        LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
    }

    if ((ret = mq_data_init()) != ERR_SUCCESS) {
        LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
    }
#endif
#ifdef HTTPSERVER_ON
    http_svr_init();
#endif
    g_isInited = TRUE;

    return ERR_SUCCESS;
}

void user_uninit() {
    if (g_isInited) {
        task_manager_exit();
#ifdef SQLITE_ON
        db_sqlite3_uninit();
#endif
#ifdef HTTPSERVER_ON
        http_svr_uinit();
#endif
#ifdef ZEROMQ_ON
        mq_uninit();
#endif
        zlog_fini();
        uninit_config_system();
        uv_loop_close(get_task_manager());
        process_unlock_pidfile();
    }
}