#ifndef HOMEKIT_LIB_MQTT_MODULE_H
#define HOMEKIT_LIB_MQTT_MODULE_H

#include "./mqtt.h"
#include "./payload.h"
#include <homekit/stopwatch.h>


namespace homekit::mqtt {

class Mqtt;

class MqttModule {
protected:
    bool initialized;
    StopWatch tickSw;
    short tickInterval;

    bool receiveOnPublish;
    bool receiveOnDisconnect;

    bool tickElapsed();

public:
    MqttModule(short _tickInterval, bool _receiveOnPublish = false, bool _receiveOnDisconnect = false)
        : initialized(false)
        , tickInterval(_tickInterval)
        , receiveOnPublish(_receiveOnPublish)
        , receiveOnDisconnect(_receiveOnDisconnect) {}

    virtual void init(Mqtt& mqtt) = 0;
    virtual void tick(Mqtt& mqtt) = 0;

    virtual void handlePayload(Mqtt& mqtt, String& topic, uint16_t packetId, const uint8_t *payload, size_t length, size_t index, size_t total);
    virtual void handleOnPublish(uint16_t packetId);
    virtual void handleOnDisconnect(espMqttClientTypes::DisconnectReason reason);

    inline void setInitialized() {
        initialized = true;
    }

	inline short getTickInterval() {
		return tickInterval;
	}

    friend class Mqtt;
};

}

#endif //HOMEKIT_LIB_MQTT_MODULE_H
