#pragma once

#include <QString>
#include <cstddef>
#include <memory>
#include <vector>

#include "devices/DeviceTypes.h"
#include "devices/base/IPhysicalDevice.h"
namespace softbus::hardware::drivers::serial { class SerialQtDriver; }
namespace softbus::message_bus::ingress { class IIngressPort; }
namespace softbus::core::memory { class MemoryPool; }

namespace softbus::devices::physical
{

class SerialDevice : public base::IPhysicalDevice
{
public:
    struct Config
    {
        QString endpoint;
        int     baudRate;
        int     dataBits;
        QString parity;
        int     stopBits;
        QString flowControl;
    };

    SerialDevice(std::unique_ptr<softbus::hardware::drivers::serial::SerialQtDriver> driver,
                 const Config&                                                       config,
                 const softbus::devices::DeviceInfo&                                 info);

    ~SerialDevice() override;

    bool start() override;
    void stop() override;

    softbus::devices::DeviceInfo deviceInfo() const override;

    QString                        endpoint() const;
    softbus::devices::DeviceKind   kind() const;
    void bindIngress(softbus::message_bus::ingress::IIngressPort* ingress,
                     std::shared_ptr<softbus::core::memory::MemoryPool> pool);
    bool writePayload(const std::vector<std::uint8_t>& data);
    bool writePayload(const std::uint8_t* data, std::size_t size);

private:
    void onRead(const std::vector<std::uint8_t>& data);
    void onError(const std::string& error);

private:
    std::unique_ptr<softbus::hardware::drivers::serial::SerialQtDriver> m_driver;
    Config                                                               m_config;
    softbus::devices::DeviceInfo                                         m_info;
    softbus::message_bus::ingress::IIngressPort*                         m_ingress{nullptr};
    std::shared_ptr<softbus::core::memory::MemoryPool>                   m_pool;
};

} // namespace softbus::devices::physical

