#include "ArduinoJson.h"
#include <WiFi.h>
#include "SimpleTimer.h"
#include <FastBot.h>
#include <PubSubClient.h>


const char* ssid = "Wokwi-GUEST";
const char* password = "";

const char* mqtt_server = "test.mosquitto.org";
const int mqtt_port = 1884;
const char* mqtt_topic = "TOIP/lab4/boiler/temperature";
const char* mqtt_username = "ro";
const char* mqtt_password = "readonly";

const char* bot_token = "6780587077:AAGfuAjlpo__OTIEb-kCSRe8dQCd1jEZPis";
const char* chat_id = "460540516";

int RELAY_PIN = 19;

FastBot bot(bot_token);
WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  delay(10);
  Serial.println("Connecting to WiFi...");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP Address: ");
  Serial.println(WiFi.localIP());
}

float currentTemperature = 0.0;
float targetTemperature = 27.0;
const float hysteresis = 0.5;
String currentTime;

void helpCommand() {
  String helpText = "/help - List all commands\n";
  helpText += "/get_temp - Get the current room temperature\n";
  helpText += "/set_temp <value> - Set the desired temperature for the boiler\n";
  helpText += "/get_settings - Get current thermostat settings\n";
  bot.sendMessage(helpText);
}

void setTempCommand(String text) {
  targetTemperature = text.toFloat();
  bot.sendMessage("Target temperature set to: " + String(targetTemperature) + "°C");
  checkBoiler();
}

void getSettingsCommand() {
  String settings = "Target temperature: " + String(targetTemperature) + "°C\n";
  settings += "Hysteresis: " + String(hysteresis) + "°C";
  bot.sendMessage(settings);
}

void commander(FB_msg& msg) {
  Serial.println("Received command: " + msg.text);
  if (msg.text.startsWith("/help")) {
    helpCommand();
  }
  else if(msg.text.startsWith("/set_temp")) {
    setTempCommand(msg.text.substring(10));
  }
  else if (msg.text.startsWith("/get_settings")) {
    getSettingsCommand();
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);

  bot.setChatID(chat_id);

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  bot.attach(commander);
}

void loop() {
  bot.tick();
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
} 

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESPClient-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("connected");
      client.subscribe(mqtt_topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}


void callback(char* topic, byte* message, unsigned int length) {
  String msg;
  for (int i = 0; i < length; i++) {
    msg += (char)message[i];
  }
  if (String(topic) == mqtt_topic) {
    int pos = msg.indexOf(' ');
    currentTemperature = msg.substring(0, pos).toFloat();
    currentTime = msg.substring(pos + 1);
  }

  Serial.print("Temperature: ");
  Serial.println(currentTemperature);
  Serial.print("Time: ");
  Serial.println(currentTime);

  checkBoiler();
}

void checkBoiler() {
  if (currentTemperature < (targetTemperature - hysteresis)) {
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("Current temperature < " + String(targetTemperature - hysteresis) + "°C -> relay ON");
  } else if (currentTemperature > (targetTemperature + hysteresis)) {
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Current temperature > " + String(targetTemperature - hysteresis) + "°C -> relay OFF");
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module