#pragma once

#include <QObject>
#include <QUdpSocket>
#include <QHostAddress>
#include <memory>

#include "DeviceInfo.h"
#include "protocol.h"

/**
 * @brief 设备信息接收器（UDP 10000）
 */
class DeviceInfoReceiver : public QObject {
    Q_OBJECT
public:
    explicit DeviceInfoReceiver(QObject* parent = nullptr, uint16_t port = 10000);
    ~DeviceInfoReceiver();

    bool start();
    void stop();

    /** 主动请求设备信息（发送 DEVICE_INFO_REQUEST） */
    void request_info(const QHostAddress& target = QHostAddress::Broadcast);

signals:
    void deviceInfoReceived(const DeviceInfo& info);
    void error(const QString& message);

private slots:
    void on_udp_read_ready();

private:
    bool parse_packet(const QByteArray& datagram, DeviceInfo& out);
    uint64_t ntohll(uint64_t v) const;
    QString fourcc_to_string(uint32_t fourcc) const;

private:
    std::unique_ptr<QUdpSocket> udp_socket_;
    uint16_t listen_port_;
};
