//
// Created by Ht on 2021/1/19.
//

#ifndef LIBDEVCPP_MGCHANNELCONFIGSERVICE_H
#define LIBDEVCPP_MGCHANNELCONFIGSERVICE_H

#include <mgbus/MgBusService.h>
#include <qlibc/cmd/JCCmdRunner.h>
#include <qlibc/efflog.h>
#include "MgOperatorAccessor.h"

namespace jedge {

    using onDirectMqttMessageHandler = std::function<void(const qlibc::QData &msg)>;
    using onDirectMqttMessageHandlerR = std::shared_ptr<onDirectMqttMessageHandler>;

    class JE_API MgConfigService :
            public MgOperatorAccessor {

    protected:
        std::string logkey_;
        std::string* rm_logger_key_ = nullptr;
        onDirectMqttMessageHandlerR mDirectMqttHandler;
    public:
        explicit MgConfigService(jedge::ChannelOperator& opr, const std::string& name, const qlibc::QData& config);

        ~MgConfigService();

        void preparePattern() override;

        bool ping(qlibc::QData &msg, qlibc::QData *rsp);

        bool rlog(qlibc::QData &msg, qlibc::QData *rsp);

        bool push(qlibc::QData &msg, qlibc::QData *rsp);

        bool pull(qlibc::QData &msg, qlibc::QData *rsp);

        bool restart(qlibc::QData &msg, qlibc::QData *rsp);

        bool serverConfig(qlibc::QData &msg, qlibc::QData *rsp);

        bool exec(qlibc::QData &msg, qlibc::QData *rsp);

        bool qs(qlibc::QData &msg, qlibc::QData *rsp);  //查询当前module的service数量

        void openRemoteLogger();

        void closeRemoteLogger();

        MgTriggerManager *getTrigerService() override;

        virtual const std::string &getLocalAddressInfo();

        virtual void getServiceList(QStringList &sl);

        bool checkFunction(qlibc::QData &msg, qlibc::QData *rsp);

        bool registerFunction(const string &func);

        bool directMqttMessage(qlibc::QData &msg, qlibc::QData *rsp);

        void setDirectMqttMessageHandler(const onDirectMqttMessageHandler& handler);
    };

}
#endif //LIBDEVCPP_MGCHANNELCONFIGSERVICE_H
