#include "plugins/protocols/modbus_rtu/parser/ModbusRtuProtocolPlugin.h"

#include "core/plugin_system/IProtocolPlugin.h"
#include "core/models/RawBusMessage.h"
#include "message_bus/pipeline/protocol/ProtocolEnvelope.h"
#include "plugins/protocols/modbus_rtu/codec/ModbusRtuCodec.h"
#include "plugins/protocols/modbus_rtu/framer/ModbusRtuFraming.h"

#include <memory>
#include <utility>
#include <variant>

namespace softbus::core::plugin_system
{

namespace
{

using softbus::message_bus::pipeline::protocol::ModbusExceptionPdu;
using softbus::message_bus::pipeline::protocol::ModbusRtuPdu;
using softbus::message_bus::pipeline::protocol::ModbusRtuUnsupportedPdu;
using softbus::message_bus::pipeline::protocol::ProtocolFamily;
using softbus::message_bus::pipeline::protocol::ReadCoilsRequest;
using softbus::message_bus::pipeline::protocol::ReadCoilsResponse;
using softbus::message_bus::pipeline::protocol::ReadDiscreteInputsRequest;
using softbus::message_bus::pipeline::protocol::ReadDiscreteInputsResponse;
using softbus::message_bus::pipeline::protocol::ReadHoldingRegistersRequest;
using softbus::message_bus::pipeline::protocol::ReadHoldingRegistersResponse;
using softbus::message_bus::pipeline::protocol::ReadInputRegistersRequest;
using softbus::message_bus::pipeline::protocol::ReadInputRegistersResponse;
using softbus::message_bus::pipeline::protocol::WriteSingleCoilRequest;
using softbus::message_bus::pipeline::protocol::WriteSingleCoilResponse;
using softbus::message_bus::pipeline::protocol::WriteSingleRegisterRequest;
using softbus::message_bus::pipeline::protocol::WriteSingleRegisterResponse;
using softbus::message_bus::pipeline::protocol::WriteMultipleCoilsRequest;
using softbus::message_bus::pipeline::protocol::WriteMultipleCoilsResponse;
using softbus::message_bus::pipeline::protocol::WriteMultipleRegistersRequest;
using softbus::message_bus::pipeline::protocol::WriteMultipleRegistersResponse;
using softbus::message_bus::pipeline::protocol::decodeModbusRtu;

template <class... Ts>
struct overloaded : Ts...
{
    using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

class ModbusRtuSession final : public IProtocolSession
{
public:
    bool parse(const softbus::core::models::RawBusMessage& ctx,
               softbus::message_bus::pipeline::protocol::ProtocolEnvelope& out) override
    {
        if (!ctx.payload.valid()) {
            return false;
        }
        const std::size_t n = ctx.payload.length;
        const std::uint8_t* p = ctx.payload.bytes();
        if (!p || n < 4) {
            return false;
        }

        ModbusRtuPdu pdu;
        if (!decodeModbusRtu(ctx.direction,
                             p,
                             n,
                             pdu,
                             nullptr)) {
            out.family = ProtocolFamily::None;
            out.pdu = {};
            return false;
        }
        out.family = ProtocolFamily::ModbusRtu;
        out.pdu = pdu;
        return true;
    }

    void reset() override {}
};

} // namespace

class ModbusRtuProtocolPlugin final : public IProtocolPlugin
{
public:
    QString pluginId() const override { return QStringLiteral("modbus_rtu"); }

    softbus::core::models::ProtocolType protocolType() const override
    {
        return softbus::core::models::ProtocolType::MODBUS_RTU;
    }

    std::unique_ptr<IProtocolSession> createSession() override
    {
        return std::make_unique<ModbusRtuSession>();
    }
};

std::shared_ptr<IProtocolPlugin> makeModbusRtuProtocolPlugin()
{
    return std::make_shared<ModbusRtuProtocolPlugin>();
}

IProtocolPlugin* createModbusRtuProtocolPluginRaw()
{
    return new ModbusRtuProtocolPlugin();
}

} // namespace softbus::core::plugin_system
