#include "Arduino.h"
#include "WiFi.h"
// #include <WiFiClientSecure.h>
#include "PubSubClient.h"
#define led1 2
#define led2 4
#define led3 5
#define led4 18
//

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "202.29.228.244";
const uint16_t mqtt_server_port = 1883;
const char* mqttUser = "demo";
const char* mqttPassword = "demo1234";
const char* mqttTopicIn_1 = "Home/66302040099/LED1";
const char* mqttTopicIn_2 = "Home/66302040099/LED2";
const char* mqttTopicIn_3 = "Home/66302040099/LED3";
const char* mqttTopicIn_4 = "Home/66302040099/LED4";
//

// #define MQTT_TLS
#define MQTT_TLS_VERIFY

// WiFiClientSecure wifiClient;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

void connect(){
  while(!mqttClient.connected()){
    Serial.print("Attempting MQTT connection...");
    String mqttClientId = "";
    mqttClient.setKeepAlive(1000);
    if (mqttClient.connect(mqttClientId.c_str(), mqttUser, mqttPassword)){
      Serial.println("connected");
      mqttClient.subscribe(mqttTopicIn_1);
      mqttClient.subscribe(mqttTopicIn_2);
      mqttClient.subscribe(mqttTopicIn_3);
      mqttClient.subscribe(mqttTopicIn_4);
    }else{
      Serial.print("Fail, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" will try again in 5 seconds");
      delay(500);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  String message;
  Serial.print("Message arrived on topic: '");
  Serial.print(topic);
  Serial.print("' with payload: ");
  for (unsigned int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    message += (char)payload[i];
  }
  if(String(topic) == String(mqttTopicIn_1)){
    if(message == "1"){
      digitalWrite(led1, HIGH);
    }
    else{
      digitalWrite(led1, LOW);
    }
  }
  if(String(topic) == String(mqttTopicIn_2)){
    if(message == "1"){
      digitalWrite(led2, HIGH);
    }
    else{
      digitalWrite(led2, LOW);
    }
  }
   if(String(topic) == String(mqttTopicIn_3)){
    if(message == "1"){
      digitalWrite(led3, HIGH);
    }
    else{
      digitalWrite(led3, LOW);
    }
  }
   if(String(topic) == String(mqttTopicIn_4)){
        if(message == "1"){
      digitalWrite(led4, HIGH);
    }
    else{
      digitalWrite(led4, LOW);
    }
  }
  //5

  //6
  
  Serial.println();
}

void setup_wifi(){
  delay(100);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  // wifiClient.setInsecure();
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
  Serial.println();
}

void setup() {
  Serial.begin(115200);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  setup_wifi();
  mqttClient.setServer(mqtt_server, mqtt_server_port);
  mqttClient.setCallback(callback);
}

void loop() {
  if (!mqttClient.connected()) {
     connect();
  }
  mqttClient.loop();
  mqttClient.publish("esp8266/me","55");
  delay(2000);
}