/*
* Copyright (c), Huawei Technologies Co., Ltd. 2025-2025.All rights reserved.
 */

#ifndef WEBSOCKETPLUGINEXAMPLEPROTOCOL_H
#define WEBSOCKETPLUGINEXAMPLEPROTOCOL_H

#include "ProtocolUtil.h"
using namespace Dic::Protocol;
using namespace Dic;

struct WebSocketPluginExampleResponse : public Response
{
    WebSocketPluginExampleResponse() : Response("WebSocketExampleHandler")
    {
    }

    std::string message;
};

struct WebSocketPluginExampleRequest : public Request
{
    WebSocketPluginExampleRequest(): Request(std::string_view("WebSocketExample/test"))
    {
    }

    std::string message;
};

inline std::optional<document_t> ToWebSocketPluginExampleResponse(const Response& res)
{
    const auto& response = dynamic_cast<const WebSocketPluginExampleResponse&>(res);
    document_t json(rapidjson::kObjectType);
    auto& allocator = json.GetAllocator();
    Protocol::ProtocolUtil::SetResponseJsonBaseInfo(response, json);
    json.AddMember(rapidjson::StringRef("message"),
                   json_t().SetString(response.message.c_str(), response.message.length(), allocator),
                   allocator);
    return std::optional<document_t>(std::move(json));
}

inline std::unique_ptr<Request> ToWebSocketPluginExampleRequest(const json_t& req, std::string& err)
{
    auto request = std::make_unique<WebSocketPluginExampleRequest>();
    request->message = req.GetString();
    return request;
}

inline std::optional<document_t> ToWebSocketPluginExampleEvent(const Event& baseEvent)
{
    document_t json(rapidjson::kObjectType);
    json.AddMember(rapidjson::StringRef("eventStr"),
                   json_t().SetString(baseEvent.event.c_str(), baseEvent.event.length(), json.GetAllocator()),
                   json.GetAllocator());
    return std::optional<document_t>(std::move(json));
}

class WebSocketPluginExampleProtocol : public ProtocolUtil
{
public:
    WebSocketPluginExampleProtocol() = default;
    ~WebSocketPluginExampleProtocol() override = default;

private:
    void RegisterEventToJsonFuncs() override;
    void RegisterJsonToRequestFuncs() override;
    void RegisterResponseToJsonFuncs() override;
};
#endif //WEBSOCKETPLUGINEXAMPLEPROTOCOL_H