//
// Created by Ht on 2021/10/14.
//

#ifndef INC_0_LIBDEVCPP_CPAD_MGTRIGGERSERVICE_H
#define INC_0_LIBDEVCPP_CPAD_MGTRIGGERSERVICE_H

#include <mgbus/MgBusService.h>
#include <mgbus/MgTriggerManager.h>
#include "../MgBusServer.h"

namespace jedge {

    class JE_API MgbusServerTriggerService :
            public MgService, public MgTriggerManager {

    public:
        explicit MgbusServerTriggerService(jedge::MgBusServer &opr, const std::string &name, const qlibc::QData &config);
        ~MgbusServerTriggerService();

        void appendEventAppendixInfo(qlibc::QData &event_data) override;

        void onServiceStart(const qlibc::QData &config) override;

        void onServiceStop() override;

        void preparePattern() override;

        template<typename T>
        inline std::shared_ptr<T> findServerService(const std::string& svcKey) {
            auto org = getServer().findServiceRef(svcKey);
            auto ref = std::dynamic_pointer_cast<T>(org);
            return ref;
        }

    protected:
        MgBusServer &getServer() {
            return *dynamic_cast<jedge::MgBusServer*>(&operator_);
        }

        void prepareEvents() override;

        bool afterOnlineWatched(MgTriggerWatcher &watcher, const std::string &keyVal, const qlibc::QData &data);

        bool beforeOnlineUnwatching(MgTriggerWatcher &watcher, const std::string &keyVal, const qlibc::QData &data);
    };

}
#endif //INC_0_LIBDEVCPP_CPAD_MGTRIGGERSERVICE_H
