/*
 * @Description: 
 * @Version: 1.0
 * @Autor: lishengyin
 * @Date: 2022-04-02 14:19:07
 * @LastEditors: lishengyin
 * @LastEditTime: 2022-04-24 11:08:01
 */
#include "ProcessingIt.hpp"
#include "dataTypeList.hpp"
#include "ParserJson.hpp"

namespace ins
{
    /**
     * @description: 获取ptr
     * @param {*}
     * @return {*}
     */    
    std::shared_ptr<ProcessingIt> ProcessingIt::getPtr(){
        static std::shared_ptr<ProcessingIt> m_ProcessingIt = nullptr;
        if(m_ProcessingIt == nullptr) m_ProcessingIt = std::shared_ptr<ProcessingIt>(new ProcessingIt);
        return m_ProcessingIt;
    }

    /**
     * @description: 初始化
     * @param {*}
     * @return {*}
     */    
    int8_t ProcessingIt::Init(){
        if(this->valid) return OK;
        this->valid = true;
        return OK;
    }

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

    /**
     * @description: 解析数据
     * @param {*}
     * @return {*}
     */    
    bool ProcessingIt::ParserData(std::shared_ptr<InsPacket> packet){
        if(packet == nullptr) return false;
        ParserBase::ptr m_parser = getParser(packet->getData());
        if(m_parser == nullptr){
            // 未找到解析器
            WarnL << "No parser found" << endl;
            return false;
        } 
        return m_parser->ParserData(packet);
    }

    /**
     * @description: 获取解析器
     * @param {InsUniversalData} data
     * @return {*}
     */    
    std::shared_ptr<ParserBase> ProcessingIt::getParser(InsUniversalData::Ptr data){
        std::shared_ptr<ParserBase> parserBase = nullptr;
        // 获取解析器
        switch (data->format)
        {
        case InsDataFormat::INS_DATA_FORMAT_JSON:
            parserBase = std::make_shared<ParserJson>();
            break;
        case InsDataFormat::INS_DATA_FORMAT_XML:
            break;
        case InsDataFormat::INS_DATA_FORMAT_PROTOBUF:
            break;
        case InsDataFormat::INS_DATA_FORMAT_SQL:
            break;
        default:
            break;
        }
        return parserBase;
    }
} // namespace ins
