//
// Created by Ht on 2020/8/26.
//

#ifndef LIBDEVCPP_ECSInvokeService_H
#define LIBDEVCPP_ECSInvokeService_H


#include <mgbus/MgBusService.h>

namespace jedge {

    static const char* JEDGE_MUFIS_KEY_CLIENT_NAME = "payload.name";   //mgbus 客户端命名
    static const char* je_http_invoker_sync_call_prefix = "/@t/@s"; //同步调用测试
    static const char* je_http_invoker_async_call_prefix = "/@t/@a";//异步调用测试
    static const unsigned char je_http_invoker_call_prefix_len = 6;

    //用于测试向在线的客户端反向推送消息的测试服务。

#define getTestTargetUri(req)\
    (req).getString(MG_SERVICE_URI_PATTERN).substr(je_http_invoker_call_prefix_len)

#define resetTestTargetUri(req) \
    (req).putString(MG_SERVICE_URI_PATTERN, message.getString(MG_SERVICE_URI_PATTERN).substr(je_http_invoker_call_prefix_len))

    //将所有带有mgbus直接请求标志的http请求，转发到内部服务中去
#define bindMgBusTransfer(targetObj) \
    targetObj.addPatternHandler(DATA_CHANNEL_HTTP, std::string(MG_MODULE_PATH_SIGN_PREFIX) + "*/**",  [](ChannelOperator& opr, const httplib::Request &httprequest, \
        qlibc::QData &request,qlibc::QData &response) -> bool { \
            return jedge::ECSInvokeService::httpDirectMgBusCall(opr, httprequest, request, response); \
        })

    class ECSInvokeService :
            public MgBusService {

    protected:

    public:
        explicit ECSInvokeService(jedge::MgBusHolder& opr, const std::string& name, const qlibc::QData& config = qlibc::QData()) :
            MgBusService(opr, name, config){}
        ~ECSInvokeService() override = default;

        void preparePattern() override;

    protected:
        bool handle_Sync(qlibc::QData &message, qlibc::QData *response);

        bool handle_ASync(qlibc::QData &message, qlibc::QData *response);

        bool handle_Default(qlibc::QData &message, qlibc::QData *response) override;

    public:

        /**
         *          mgcall 转发
         *
         * @param opr
         * @param httprequest
         * @param request
         * @param response
         * @return
         */

        static bool httpDirectMgBusCall(ChannelOperator& opr, const httplib::Request &httprequest,
                                        qlibc::QData& request, qlibc::QData& response);

    };

}

#endif //LIBDEVCPP_MUFISTESTSERVICE_H
