#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H

#include <QtMqtt/qmqttclient.h>
#include <QtMqtt/qmqttmessage.h>

#include <QObject>

class MqttClient : public QObject {
    Q_OBJECT

   public:
    static MqttClient *getInstance();  // 静态成员函数，用于获取单例实例
    static MqttClient *createNewInstance();
    explicit MqttClient(QObject *parent = nullptr);
    void subscribeToTopic(const QString &topic);
    void sendMessage(const QString &topic, const QByteArray &message, quint8 qos,
                     bool isRetainedMsg);
    void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
    void connectToMqttBroker(const QString &hostname, quint16 port, const QString &username,
                             const QString &password, const QString &clientId,
                             const QStringList &topicsToSubscribe);
    void subscribeToTopics(const QStringList &topics);
    void connectToMqttBroker();
   signals:
    void connected();
    void messageAndTopicReceived(const QByteArray &message, const QMqttTopicName &topic);
    void proMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
   private slots:

    void onConnected();
    // void onDisconnected();
    void onStateChanged(QMqttClient::ClientState newState);
    void onError(QMqttClient::ClientError error);
    // void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);

   private:
    static MqttClient *instance;  // 静态成员变量，用于保存单例实例
    QMqttClient *mqClient;
    QStringList m_subscribeTopics;
};
#endif  // MQTTCLIENT_H
