#include <Wire.h>
#include "RTClib.h"
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <ESP32Servo.h>

/************************* WiFi Access Point ***************************/
#define WLAN_SSID "Wokwi-GUEST"
#define WLAN_PASS ""

/************************* Adafruit.io Setup ***************************/
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "r9drigo"
#define AIO_KEY         "aio_fYfZ06Ip8TeMijRVfAPWO12h4OlD"
#define AIO_FEED_1      "/feeds/pet-smart-feed.feed"
#define AIO_FEED_2      "/feeds/pet-smart-feed.interval"
#define AIO_FEED_3      "/feeds/pet-smart-feed.auto"

/****************************** Global State ***********************************/
#define FEED_SERVO 2
#define AUTO_LED 12

Servo servo;
int pos = 0;

RTC_DS1307 rtc;

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);

int interval = 1;
bool isAuto = false;

int nextHour = 0;
int nextMinute = 0;

#define CHECK_INTERVAL 10000 // 10s in ms

unsigned long lastCheck = 0;

/****************************** Feeds ***************************************/

// Publishers
Adafruit_MQTT_Publish feedPublish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME AIO_FEED_1);

// Subscribers
Adafruit_MQTT_Subscribe feedSubscriber = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME AIO_FEED_1, MQTT_QOS_1);
Adafruit_MQTT_Subscribe intervalSubscriber = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME AIO_FEED_2, MQTT_QOS_1);
Adafruit_MQTT_Subscribe autoSubscriber = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME AIO_FEED_3, MQTT_QOS_1);

void feedCallback(char *data, uint16_t len) {
  if (strcmp(data, "1") == 0) {
    Serial.println("Feeding... 🥩");

    moveServo();

    feedPublish.publish(0);
    if (isAuto) setNextTime(rtc.now());
  } else {
    Serial.println("Feeding concluded. 👍");
  }
}

void intervalCallback(char *data, uint16_t len) {
  interval = atoi(data);
  Serial.print("Interval between feeds is now ");
  Serial.print(interval);
  Serial.println(" minute(s). ⏱");

  if (isAuto) setNextTime(rtc.now());
}

void autoCallback(char *data, uint16_t len) {
  if (strcmp(data, "ON") == 0) {
    isAuto = true;
    digitalWrite(AUTO_LED, HIGH);
    Serial.println("Auto Feed is now on. ✅");

    setNextTime(rtc.now());
  } else {
    isAuto = false;
    digitalWrite(AUTO_LED, LOW);
    Serial.println("Auto Feed is now off. 🚫");
  }
}

void moveServo() {
  for (pos = 0; pos <= 90; pos += 1) {
    servo.write(pos);
    delay(15);
  }
  for (pos = 90; pos >= 0; pos -= 1) {
    servo.write(pos);
    delay(15);
  }
}

void setNextTime(DateTime now) {
  nextHour = now.hour();
  nextMinute = now.minute() + interval;

  while (nextMinute >= 60) {
    nextMinute -= 60;
    nextHour++;
  }

  while (nextHour >= 24) {
    nextHour -= 24;
  }

  Serial.print("Next feeding will occur at ");
  if (nextHour < 10) Serial.print("0");
  Serial.print(nextHour);
  Serial.print(":");
  if (nextMinute < 10) Serial.print("0");
  Serial.print(nextMinute);
  Serial.println(" ⏱");
}

void wifiSetup() {
  // Connect to WiFi access point.
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS, 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi Connected!");
}

void mqttSetup() {
  int8_t ret;
  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 10 seconds...");
    mqtt.disconnect();
    delay(10000);  // wait 10 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
}

void rtcSetup() {
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void setup() {
  Serial.begin(115200);
  
  rtcSetup();

  wifiSetup();

  servo.attach(FEED_SERVO, 500, 2400);
  pinMode(AUTO_LED, OUTPUT);

  // Set the callback function to be called when feed's message arrives
  feedSubscriber.setCallback(feedCallback);
  intervalSubscriber.setCallback(intervalCallback);
  autoSubscriber.setCallback(autoCallback);
  
  // Setup MQTT subscription for time feed.
  mqtt.subscribe(&feedSubscriber);
  mqtt.subscribe(&intervalSubscriber);
  mqtt.subscribe(&autoSubscriber);
}

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the mqttSetup
  // function definition further below.
  mqttSetup();

  // this is our 'wait for incoming subscription packets and callback em' busy subloop
  // try to spend your time here:
  mqtt.processPackets(10000);
  
  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  if(!mqtt.ping()) {
    mqtt.disconnect();
  }

  if (isAuto) {
    unsigned long nowInMillis = millis();
    if (nowInMillis - lastCheck > CHECK_INTERVAL) {
      lastCheck = nowInMillis;

      DateTime now = rtc.now();

      if (nextHour == now.hour() && nextMinute == now.minute()) {
        feedPublish.publish(1);
      }
    }
  }
}
GND5VSDASCLSQWRTCDS1307+