/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-06 08:46:28
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-06-27 09:52:01
 */
#include "UserApp.hpp"
#include <drogon/drogon.h>

using namespace drogon;

namespace ins{

    static shared_ptr<userApp> app = nullptr;

    /**
     * @description: 初始化
     * @param {string} Name
     * @return {*}
     */    
    int8_t userApp::Init(std::string Name){
        this->appName = Name;
        InfoL << "Version:" << VersionString() << endl;
        if(this->Init() != OK) return ERR;
        return OK;
    }

    /**
     * @description: 初始化函数
     * @return {*}
     */    
    int8_t userApp::Init(){
        auto task = [&](std::string Name){
            ErrorL << Name << " Module initialization failed" << endl;
        };
        if(this->m_taskCenter == nullptr) m_taskCenter = TaskCenter::CreateNew();
        if(m_taskCenter->Init() != OK){
            task("taskCenter");
            return ERR;
        }
        DebugL << "TaskCenter initialization successfully" << endl;
        if(this->m_getCenter == nullptr) m_getCenter = GetCenter::CreateNew();
        if(m_getCenter->Init() != OK){
            task("getCenter");
            return ERR;
        }
        DebugL << "GetCenter initialization successfully" << endl;
        Sectorpower::getPtr()->setEnable(true);
        return OK;
    }

    /**
     * @description: 释放资源
     * @return {*}
     */    
    void userApp::Destroy() {
    }

    /**
     * @description: 创建新的实例
     * @param {*}
     * @return {*}
     */        
    std::shared_ptr<userApp> userApp::CreateNew(){
        if(app == nullptr) app = std::shared_ptr<userApp>(new userApp());
        return app;
    }

    /**
     * @description: 获取ptr
     * @param {*}
     * @return {*}
     */        
    std::shared_ptr<userApp> userApp::getPtr(){
        return app;
    }

    /**
     * @description: 启动任务
     * @return {*}
     */    
    int8_t userApp::StartTask(){
        // drogon::app().loadConfigFile("../source/config/config.json");
        drogon::app().addListener("0.0.0.0",8006);
        drogon::app().loadConfigFile("../source/config/config.json");
        drogon::app().run();
        return OK;
    }
};