/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-06 11:07:53
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-27 17:06:57
 */
#ifndef __INS_INSPACK_HPP_
#define __INS_INSPACK_HPP_

#include "private/ins_module_private.hpp"
#include "ins_Task.hpp"
#include <vector>

namespace ins
{
    /**
     * @description: 数据包类型
     */    
    enum class InsPacketTypes{
        INS_ROUTINE_PACKET = 1 << 0,
        INS_IN_PACKET = 1 << 1,
        INS_NETTY_PACKET = 1 << 2,
        INS_K8SSERVER_PACKET = 1 << 3,
        INS_APOLLO_PACKET = 1 << 4,
        INS_MYSQL_PACKET = 1 << 5,
        INS_KUBESPHERE_PACKET = 1 << 6
    };
    
    /**
     * @description: 数据格式
     */    
    enum class InsDataFormat{
        INS_DATA_FORMAT_JSON = 1 << 0,
        INS_DATA_FORMAT_XML = 1 << 1,
        INS_DATA_FORMAT_PROTOBUF = 1 << 2,
        INS_DATA_FORMAT_SQL = 1 << 3,
    };
    
    class InsUniversalData
    {
    public:
        int Id;
        InsDataFormat format;
        std::string data;
        
        // 附属信息
        std::string attr1;
        std::string attr2;
        std::string attr3;

    public:
        using Ptr = std::shared_ptr<InsUniversalData>;
        InsUniversalData(){}
        ~InsUniversalData(){}
    };
    
    /**
     * @description: 数据包类
     */    
    class InsPacket:private NonCopyable
    {
    public:
        using Ptr = std::shared_ptr<InsPacket>;
        using PackTaskPtr = std::shared_ptr<std::function<void(InsPacket::Ptr)>>;

    private:        
        InsPacket(InsPacketTypes sourceType, InsPacketTypes bournType, InsUniversalData::Ptr data, InsPacket::PackTaskPtr packtask): sourceType_(sourceType), \
            bournType_(bournType), insPackTask_(packtask){
            this->CreateTime = getDataTime();
            data_ = data;
        }

    public:
        ~InsPacket(){}

        /**
         * @description: 创建实例
         * @param {InsPacketTypes} sourceType  数据来源
         * @param {InsPacketTypes} bournType   数据去向
         * @param {InsUniversalData} data      起始数据
         * @param {PackTaskPtr} packtask       包的任务，工作流中解析成功时调用,默认为nullptr, 如需使用MakePackTaskPtr生成
         * @return {*}
         */        
        static std::shared_ptr<InsPacket> CreateNew(InsPacketTypes sourceType, InsPacketTypes bournType, InsUniversalData::Ptr data, InsPacket::PackTaskPtr packtask = nullptr);

        /**
         * @description: 获取sourceType
         * @param {*}
         * @return {*}
         */        
        InsPacketTypes& getSourceType(){
            return sourceType_;
        }
        
        /**
         * @description: 获取bournType
         * @param {*}
         * @return {*}
         */        
        InsPacketTypes& getBournType(){
            return bournType_;
        }

        /**
         * @description: 
         * @param {function<void(InsPacket::Ptr)>} t
         * @return {*}
         */        
        static PackTaskPtr MakePackTaskPtr(std::function<void(InsPacket::Ptr)> t);

        /**
         * @description: 获取原始数据
         * @param {*}
         * @return {*}
         */    
        InsUniversalData::Ptr getData();

        /**
         * @description: 获得最终数据
         * @param {*}
         * @return {*}
         */        
        InsUniversalData::Ptr getToData();

        /**
         * @description: 设置最终的数据
         * @param {InsUniversalData} Data
         * @return {*}
         */         
        bool setToData(InsUniversalData::Ptr Data);

        /**
         * @description: 获取创建时间
         * @param {*}
         * @return {*}
         */        
        std::string getCreateTime(){
            return this->CreateTime;
        }

        /**
         * @description: 获取更新时间
         * @param {*}
         * @return {*}
         */        
        std::string getUpdateTime(){
            return this->UpdateTime;
        }

        /**
         * @description: 设置sql结果
         * @param {vector<vector<std::string>>} ret
         * @return {*}
         */        
        bool setSqlResult(vector<vector<std::string>> ret);

        /**
         * @description: 获取结果
         * @param {*}
         * @return {*}
         */        
        vector<vector<std::string>> getSqlResult();
        
        /**
         * @description: 获取任务
         * @param {*}
         * @return {*}
         */        
        PackTaskPtr getInsPackTask();

        /**
         * @description: 判断是否有任务
         * @param {*}
         * @return {*}
         */        
        bool IsInsPackTask();

    private:
        std::string CreateTime;
        std::string UpdateTime;
        vector<vector<std::string>> result_;
        InsPacketTypes sourceType_;
        InsPacketTypes bournType_;
        PackTaskPtr insPackTask_ = nullptr;
        InsUniversalData::Ptr data_ = nullptr;
        InsUniversalData::Ptr toData_ = nullptr;
    };
} // namespace ins






#endif