//
// Created by Ht on 2022/10/18.
//

#ifndef IOT_EDGE_VDALNEW_SERIALUTIL_H
#define IOT_EDGE_VDALNEW_SERIALUTIL_H

#include "JAExObjectV.h"

namespace jedge {

    class JE_API SerialUtil {
    public:
        static void loadInnerClasses(QJAMgServer &server) {
            auto cls = server.Class(sval_class_name_jedp_);
            bindJAClassCreator(cls, QJAJedpTranser);
            JAUDS_DEFINE_CLASS_METHOD_WITH_STACK(server, cls, QJAJedpTranser, makeMsg);
            JAUDS_DEFINE_CLASS_METHOD_WITH_STACK(server, cls, QJAJedpTranser, loadMsg);
            JAUDS_DEFINE_CLASS_METHOD_WITH_STACK(server, cls, QJAJedpTranser, loadRule);

            //创建一个Jedp对象
            auto stack_ = server.findClass("stack");

            //查看当前可用的rule情况
            stack_->Method("rules", [&server] JAMethodConst(s,o,qm,sp) {
                FLogR("Run rule load command as : loadJedp <file>[.json]. Available belows:");
                QStringList fl;
                auto base = server.getSubCacheDirFullPath("vdal/rule",nullptr);
                auto bsize= base.size()+1;
                FileUtils::listFiles(base , "json", fl);
                auto re = s.findObject("rsp");
                for (const auto &f :fl) // NOLINT(altera-unroll-loops)
                    FLogR("\t->%s%s%s", qlog_HighLight, f.substr(bsize).c_str(), qlog_LOG);
                FLogR("==END");
                if(re!=nullptr) {
                    Json::Value files;
                    for (const auto &f :fl) // NOLINT(altera-unroll-loops)
                        files.append(f.substr(bsize));
                    OkRsp(*re);
                    re->put("files", files);
                }
                return true;
            });

            //loadJedp ruleName [objName, [ruleFilePath]]
            //创建一个rule对象，并加载某路径
            stack_->Method("loadJedp", [&server] JAMethodConst(s,o,qm,sp) {
                PREPARE_PARAM(param, sp);
                auto ruleName = param.getParam(0);
                auto objName = param.getParam(1, ruleName);
                param.putString("ruleName", ruleName);
                //相对路径
                auto rulePath = param.getParam(2);
                param.clearDataStartWith("@");
                if(rulePath.empty()) {
                    rulePath = server.getSubCacheDirFullPath("vdal/rule", (objName + ".json").c_str(), nullptr);
                    param.insertParamAt(0, rulePath.c_str(), nullptr);
                }
                qlibc::QData config;
                config.copyDataNotStartWith(param, '@');
                auto* jedp = new QJAJedpTranser(s.getContext(), config);
                jedp->setName(objName);
                if(!jedp->loadRule(s, param, nullptr)) {
                    delete jedp;
                    QWarn("Fail to load jedp %s : %s", objName.c_str(), rulePath.c_str());
                    return false;
                }
                s.getContext().Object(objName, jedp);    //首先占用缓存。如果http服务已经存在，则覆盖之、前序http服务器被释放
                QLog("create a default jedp server ok named %s as Rule : %s", objName.c_str(), ruleName.c_str());
                return true;
            });
        }

        static void releaseClasses(QJAMgServer &server) {
            server.releaseClass(sval_class_name_jedp_);
            server.removeClassMethod("stack", "loadJedp");
            server.removeClassMethod("stack", "rules");
        }
    };


}

#endif //IOT_EDGE_VDALNEW_SERIALUTIL_H
