//
// Created by Ht on 2019/8/10.
//

#ifndef JSONPROTOCOL_JRCOMMONDECORDER_H
#define JSONPROTOCOL_JRCOMMONDECORDER_H

#include "JRDpCommandDecoder.h"
#include "JRPackageVerifer.h"
#include "JEDPBaseMsgPacker.h"


namespace jedp {
    static const char* vkey_field_veryfy = "@verify";
    static const char* vkey_field_required = "@required";

    class JE_API JRPackageDecorder :
            public JRDpCommandDecoder {

    public:
        explicit JRPackageDecorder(QData &data, JEDPUserDecoderHandlerTable* uddh, JEDPUserEncoderHandlerTable* udeh);
        ~JRPackageDecorder() override;

        bool isValid() const override;

        JRPackage *decodePackage(const QByte *buff, size_t bufflen, JRPackage *package);

        bool encodeBuffer(qlibc::QData &msg, QByte **buff, size_t &bufflen) override ;

        bool checkPackageValid(const JRPackage& package);

        bool makeMessage(JRPackage *package, qlibc::QData& message);

        void setupTargetMessagePacker(JEDPBaseMsgPacker *packer);

        void preHandleJsonMessage(qlibc::QData &message);

        const std::string &getDataBaseKey();

    protected:
        void makeBufferFromList(std::unordered_map<string, QByte *> &bufferMap, std::unordered_map<string, size_t> &varSizeMap,
                                unsigned char **buff, size_t buffLen) override;

        JEDPBaseMsgPacker *mpPacker = nullptr;
        JRPackageVerifer  *mVerifer = nullptr;
        string mVerifyFieldKey;
        string mRequiredBaseKey;

        void buildVerifer();

    };
}


#endif //JSONPROTOCOL_JRCOMMONDECORDER_H
