#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H

#include <QMqttClient>
#include <QObject>
#include <QJsonObject>
#include <map>

class MqttClient: public QObject
{
	Q_OBJECT
public:
	struct Subscription
	{
		int ref;
		QMqttSubscription* subscription;
	};

private:
	QString baseTopicName;
	std::shared_ptr<QMqttClient> client;
	std::map<QString, Subscription*> subscriptions;

private slots:
	void onClientStateChanged(QMqttClient::ClientState state);
	void onClientError(QMqttClient::ClientError error);

public:
	explicit MqttClient();
	~MqttClient();
	void start(const QJsonObject& settings);
	void store(QJsonObject& json);
	std::shared_ptr<QMqttClient> getClient();
	Subscription* subscribe(QString topic);
	void unsubscribe(Subscription* subscription);
	void unsubscribe(QString topic);
	QString getBaseTopic();
};

#endif // MQTTCLIENT_H
