/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-03-28 09:03:02
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-07-25 11:17:07
 */
#ifndef __BASICECONFIG_H_
#define __BASICECONFIG_H_

#include <iostream>
#include <vector>
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>

using namespace std;

class BasicConfig
{
public:
    // usr
    std::string usr;
    std::string pwd;
    
    // NettyIp;
    std::string NettyIP;
    int NettyPort;

    std::string mysql_IP;
    int mysql_port;

    double DiskUsageThreshold;
    int HardDiskCleanupTime;
    int InferDataRetentionTime;
    int interval;
    int sensitivity;
    int TimeOut;
    int debug;
    int getHistoryVideoFlag;
    int ClearPastRecords;
    
    int LowVersion = 0;
    
    double bboxSize = 0.0;
    int ExpelInterval = 0;
    std::string com_port = "";

    int ExpelPort = 19002;

    int InferInterval = 2000;
    
public:
    BasicConfig(){}
    ~BasicConfig(){}

    /**
     * @description: json转换为object
     * @param {string&} json
     * @return {*}
     */    
    bool JsonToObject(std::string& json){
        rapidjson::Document doc;
        if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
            return false;
        }
        // get members
        const auto end = doc.MemberEnd();
        // json_type
        if(end == doc.FindMember("usr") || !doc["usr"].IsString()) {
            return false;
        }else{
            usr = doc["usr"].GetString();
        }

        if(end == doc.FindMember("pwd") || !doc["pwd"].IsString()) {
            return false;
        }else{
            pwd = doc["pwd"].GetString();
        }

        if(end == doc.FindMember("NettyIP") || !doc["NettyIP"].IsString()) {
            return false;
        }else{
            NettyIP = doc["NettyIP"].GetString();
        }

        if(end == doc.FindMember("NettyPort") || !doc["NettyPort"].IsString()) {
            return false;
        }else{
            NettyPort = std::atoi(doc["NettyPort"].GetString());
        }

        if(end == doc.FindMember("DiskUsageThreshold") || !doc["DiskUsageThreshold"].IsString()) {
            return false;
        }else{
            DiskUsageThreshold = std::atof(doc["DiskUsageThreshold"].GetString());
        }

        if(end == doc.FindMember("HardDiskCleanupTime") || !doc["HardDiskCleanupTime"].IsString()) {
            return false;
        }else{
            HardDiskCleanupTime = std::atoi(doc["HardDiskCleanupTime"].GetString());
        }

        if(end == doc.FindMember("InferDataRetentionTime") || !doc["InferDataRetentionTime"].IsString()) {
            return false;
        }else{
            InferDataRetentionTime = std::atoi(doc["InferDataRetentionTime"].GetString());
        }

        if(end == doc.FindMember("interval") || !doc["interval"].IsString()) {
            return false;
        }else{
            interval = std::atoi(doc["interval"].GetString());
        }

        if(end == doc.FindMember("sensitivity") || !doc["sensitivity"].IsString()) {
            return false;
        }else{
            sensitivity = std::atoi(doc["sensitivity"].GetString());
        }

        if(end == doc.FindMember("TimeOut") || !doc["TimeOut"].IsString()) {
            return false;
        }else{
            TimeOut = std::atoi(doc["TimeOut"].GetString());
        }

        if(end == doc.FindMember("mysql_IP") || !doc["mysql_IP"].IsString()) {
            return false;
        }else{
            mysql_IP = doc["mysql_IP"].GetString();
        }

        if(end == doc.FindMember("mysql_port") || !doc["mysql_port"].IsString()) {
            return false;
        }else{
            mysql_port = std::atoi(doc["mysql_port"].GetString());
        }
        
        if(end != doc.FindMember("com_port") && doc["com_port"].IsString()) {
            com_port = doc["com_port"].GetString();
        }
        
        if(end != doc.FindMember("Debug") && doc["Debug"].IsString()) {
            debug = std::atoi(doc["Debug"].GetString());
        }

        if(end != doc.FindMember("getHistoryVideoFlag") && doc["getHistoryVideoFlag"].IsString()) {
            getHistoryVideoFlag = std::atoi(doc["getHistoryVideoFlag"].GetString());
        }

        if(end != doc.FindMember("ClearPastRecords") && doc["ClearPastRecords"].IsString()) {
            ClearPastRecords = std::atoi(doc["ClearPastRecords"].GetString());
        }

        if(end != doc.FindMember("bboxSize") && doc["bboxSize"].IsString()) {
            bboxSize = std::atof(doc["bboxSize"].GetString());
        }
        
        if(end != doc.FindMember("ExpelInterval") && doc["ExpelInterval"].IsString()) {
            ExpelInterval = std::atoi(doc["ExpelInterval"].GetString());
        }

        if(end != doc.FindMember("LowVersion") && doc["LowVersion"].IsString()){
            LowVersion = std::atoi(doc["LowVersion"].GetString());
        }

        if(end != doc.FindMember("ExpelPort") && doc["ExpelPort"].IsString()){
            ExpelPort = std::atoi(doc["ExpelPort"].GetString());
        }

        if(end != doc.FindMember("InferInterval") && doc["InferInterval"].IsString()){
            InferInterval = std::atoi(doc["InferInterval"].GetString());
        }
        
        return true;
    }
};





#endif