//
// Created by zx on 23-2-22.
//

#ifndef PAHOC_SAMPLE_SAMPLES_PAHO_CLIENT_H_
#define PAHOC_SAMPLE_SAMPLES_PAHO_CLIENT_H_
#include "MQTTAsync.h"
#include <string>
#include "mqttmsg.h"

typedef void (*ArrivedCallback)(std::string topic,int QOS,MqttMsg& msg,void* context);

class Paho_client
{
 public:
    Paho_client(std::string clientid);
    ~Paho_client();
    void set_maxbuf(int size);
    bool connect(std::string address,int port);
    bool disconnect();
    void subcribe(std::string topic,int QOS,ArrivedCallback callback,void* context);
    void publish(const std::string& topic,int QOS,const MqttMsg& msg);

    bool isconnected(){return connected_;}

 protected:
    static int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message);
    static void connlost(void *context, char *cause);
    static void onConnectFailure(void* context, MQTTAsync_failureData* response);
    static void onSubscribe(void* context, MQTTAsync_successData* response);
    static void onSubscribeFailure(void* context, MQTTAsync_failureData* response);
    static void onConnect(void* context, MQTTAsync_successData* response);
    static void onSendFailure(void* context, MQTTAsync_failureData* response);
    static void onSend(void* context, MQTTAsync_successData* response);
    static void onDisconnectFailure(void* context, MQTTAsync_failureData* response);
    static void onDisconnect(void* context, MQTTAsync_successData* response);

 protected:

    bool connected_;
    std::string clientid_;
    std::string address_;
    int port_;

    MQTTAsync client_;
    MQTTAsync_connectOptions conn_opts_ = MQTTAsync_connectOptions_initializer;

    MQTTAsync_message pubmsg_ = MQTTAsync_message_initializer;
    MQTTAsync_responseOptions pub_opts_ = MQTTAsync_responseOptions_initializer;
    MQTTAsync_createOptions create_opts_=MQTTAsync_createOptions_initializer;

    ArrivedCallback arrivedCallback_= nullptr;
    void* context_;

};


#endif //PAHOC_SAMPLE_SAMPLES_PAHO_CLIENT_H_
