/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-12 15:40:03
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-24 17:49:52
 */
#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: 解析 KsWorkload的
         * @param {*}
         * @return {*}
         */        
        bool ParseKsWorkloadTemplate(std::shared_ptr<InsPacket> packet){
            ApolloServerResultMsg<ApolloKsWorkloadTemplate> msg;
            if(msg.jsonToObject(packet->getData()->data)){
                InsUniversalData::Ptr toData = std::make_shared<InsUniversalData>();
                msg.configurations.WORKLOADTEMPLATEID = packet->getData()->Id;
                msg.configurations.APOLLONAMESPACEID = atoi(packet->getData()->attr2.c_str());
                msg.configurations.AIID = atoi(packet->getData()->attr1.c_str());
                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: 
         * @param {*}
         * @return {*}
         */        
        bool ParseKsServersTemplate(std::shared_ptr<InsPacket> packet){
            ApolloServerResultMsg<ApolloKsServersTemplate> msg;
            if(msg.jsonToObject(packet->getData()->data)){
                InsUniversalData::Ptr toData = std::make_shared<InsUniversalData>();
                msg.configurations.SERVERID = packet->getData()->Id;
                msg.configurations.USERID = Disposition::getPtr()->getUserId();
                msg.configurations.APOLLONAMESPACEID = atoi(packet->getData()->attr2.c_str());
                toData->format = InsDataFormat::INS_DATA_FORMAT_SQL;
                msg.ObjectToSql(toData->data);
                packet->setToData(toData);
                return true;
            }
            return false;
        }

        /**
         * @description: 解析数据
         * @param {*}
         * @return {*}
         */        
        virtual bool ParserData(std::shared_ptr<InsPacket> packet) override{
            bool result = false;
            switch ((int)packet->getData()->type)
            {
            //* 应用配置
            case (int)InsDataType::INS_DATA_TYPE_APPCONFIG:
                result = ParseConfig(packet);
                break;
            //* Ks workload template  工作负载模板
            case (int)(InsDataType::INS_DATA_TYPE_KSWORKLOADTEMPLATE):
                result = ParseKsWorkloadTemplate(packet);
                break;
            //* Ks servers template  servers模板
            case (int)InsDataType::INS_DATA_TYPE_KSSERVERSTEMPLATE:
                result = ParseKsServersTemplate(packet);
                break;
            default:
                break;
            }
            return result;
        }
    };
} // namespace ins




#endif