#pragma once

#include <softbus_transport/UplinkWire.h>

#include <asio.hpp>

#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

namespace softbus::transport {

using RawPacketHandler = std::function<void(const softbus::core::RawPacket&)>;
using WireMessageHandler = std::function<void(const WireMessage&)>;
using CommandSender = std::function<bool(const softbus::api::ExecuteCommand&)>;

class TcpUplinkServer {
public:
    TcpUplinkServer(asio::io_context& io, std::string host, uint16_t port);
    ~TcpUplinkServer();

    void setRawPacketHandler(RawPacketHandler handler);
    void setWireMessageHandler(WireMessageHandler handler);
    void start();
    void stop();
    bool sendCommand(const softbus::api::ExecuteCommand& command);

private:
    void doAccept();
    void doRead(std::shared_ptr<asio::ip::tcp::socket> socket);
    void handleLine(const std::string& line);

    asio::io_context& io_;
    asio::ip::tcp::acceptor acceptor_;
    RawPacketHandler rawHandler_;
    WireMessageHandler wireHandler_;
    std::shared_ptr<asio::ip::tcp::socket> clientSocket_;
    std::string readBuffer_;
    std::mutex sendMutex_;
    bool running_{false};
};

} // namespace softbus::transport
