/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-12 15:40:03
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-06-27 15:26:26
 */
#ifndef __PARSERJSON_HPP_
#define __PARSERJSON_HPP_

#include <iostream>
#include "ParserBase.hpp"

using namespace std;

namespace ins
{
    class ParserJson:public ParserBase
    {
    private:
    
    public:
        ParserJson():ParserBase(){}
        ~ParserJson(){}

        /**
         * @description: 解析配置
         * @param {shared_ptr<InsPacket>} packet
         * @return {*}
         */        
        bool ParseConfig(std::shared_ptr<InsPacket> packet){
            ApolloServerResultMsg<ApolloAppConfig> msg;
            if(msg.jsonToObject(packet->getData()->data)){
                InsUniversalData::Ptr toData = std::make_shared<InsUniversalData>();
                msg.configurations.USERID = Disposition::getPtr()->getUserId();
                toData->format = InsDataFormat::INS_DATA_FORMAT_SQL;
                msg.ObjectToSql(toData->data);
                packet->setToData(toData);
                return true;
            }
            return false;
        }

        /**
         * @description: 解析 KsTemplate
         * @param {*}
         * @return {*}
         */        
        bool ParseKsTemplate(std::shared_ptr<InsPacket> packet){
            
            return false;
        }

        /**
         * @description: 获取对应类型的解析器
         * @param {std::string} DescriptionId 描述的ID
         * @return {*}
         */        
        MsgBase::Ptr getParserType(std::string DescriptionId){
            MsgBase::Ptr msg = nullptr;
            SqlStream stream("SELECT TYPE,PARSERTYPE FROM InsServerDB.`InsDataDescription` WHERE USERID = 2 AND DESCRIPTIONID = '?'");
            stream << DescriptionId;
            vector<vector<std::string>> res;
            std::string sql = (std::string)stream;
            res = GetCenter::getPtr()->ExecuteSql(sql);
            if(res.empty()) return msg;
            if(res.size() > 1) return msg;
            for(auto &line: res){
                if(line[1] == "ApolloAppConfig") msg = std::make_shared<ApolloServerResultMsg<ApolloAppConfig>>();
                else if(line[1] == "ApolloKsTemplate") msg = std::make_shared<ApolloServerResultMsg<ApolloKsTemplate>>();
                else if(line[1] == "KubeSphereNodes") msg = std::make_shared<KubeSphereResourcesMsg<KubeSphereNodes>>();
                else if(line[1] == "KubeSpherePods") msg = std::make_shared<KubeSphereResourcesMsg<KubeSpherePods>>();
                else if(line[1] == "KubeSphereServers") msg = std::make_shared<KubeSphereResourcesMsg<KubeSphereServers>>();
                else if(line[1] == "KubeSphereDeployments") msg = std::make_shared<KubeSphereResourcesMsg<KubeSphereDeployments>>();
            }
            return msg;
        }

        /**
         * @description: 解析数据
         * @param {*}
         * @return {*}
         */        
        virtual bool ParserData(std::shared_ptr<InsPacket> packet) override{
            MsgBase::Ptr msg = nullptr;
            msg = getParserType(packet->getData()->attr1);
            if(msg == nullptr) return false;
            if(msg->jsonToObject(packet->getData()->data)){
                InsUniversalData::Ptr toData = std::make_shared<InsUniversalData>();
                msg->DATATIME = packet->getCreateTime();
                msg->USERID = Disposition::getPtr()->getUserId();
                msg->ID = std::atoi(packet->getData()->attr1.c_str());
                msg->DATATIME = packet->getCreateTime();
                if(packet->getBournType() == InsPacketTypes::INS_MYSQL_PACKET){
                    if(!msg->ObjectToSql(toData->data)) return false;
                    msg->ObjectToSqlBack(toData->attr2);
                    toData->format = InsDataFormat::INS_DATA_FORMAT_SQL;
                }
                packet->setToData(toData);
                return true;
            }
            return false;
        }
    };
} // namespace ins




#endif