#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

//Wifi connection 
char ssid[] = "Wokwi-GUEST";     //SSID (Wifi)
char password[] = ""; //PASSWORD (WIFI)

//Telegram BOT 
#define BOTtoken "5540487735:AAHYC7Y-8YjxmWQz8ch91n_sq9d0lTuUl-A"  //BOT Token 

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime;   //last time messages' scan has been done
bool Start = false;

const int led1Pin = 26;
const int led2Pin = 27;
int ledStatus = 0;

bool set_led = false;

void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {
    String chat_id = String(bot.messages[i].chat_id);
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;
    if (from_name == "") from_name = "Guest";

    if (text == "/led1on") {
      digitalWrite(led1Pin, HIGH);   // turn the LED on (HIGH is the voltage level)
      bot.sendSimpleMessage(chat_id, "Led1 is ON", "");
    }
    else if (text == "/led1off") {
      digitalWrite(led1Pin, LOW);    // turn the LED off (LOW is the voltage level)
      bot.sendSimpleMessage(chat_id, "Led1 is OFF", "");
    }
    else if (text == "/led2on") {
      digitalWrite(led2Pin, HIGH);   // turn the LED on (HIGH is the voltage level
      bot.sendSimpleMessage(chat_id, "Led2 is ON", "");
    }
    else if (text == "/led2off") {
      digitalWrite(led2Pin, LOW);    // turn the LED off (LOW is the voltage level)
      bot.sendSimpleMessage(chat_id, "Led2 is OFF", "");
    }
    else if (text == "/status") {
      if(ledStatus){
        bot.sendSimpleMessage(chat_id, "Led is ON", "");
      } else {
        bot.sendSimpleMessage(chat_id, "Led is OFF", "");
      }
    }
    else if (text == "/start") {
      String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".%0A%0A";
      welcome += "This is Flash Led Bot example.";
      welcome += "/led1on : to switch the Led ON";
      welcome += "/led1off : to switch the Led OFF";
      welcome += "/led2on : to switch the Led ON";
      welcome += "/led2off : to switch the Led OFF%0A";
      welcome += "/setled : to set the Led 0 - 1023%0A";
      welcome += "/status : Returns current status of LED";
      bot.sendSimpleMessage(chat_id, welcome, "");
    }
  }
}


void setup() {
  Serial.begin(9600);

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

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

  Serial.println("");
  Serial.println("WiFi connected");

  client.setInsecure();
  
  pinMode(led1Pin, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(led1Pin, LOW); // initialize pin as off
  pinMode(led2Pin, OUTPUT);
  delay(10);
  digitalWrite(led2Pin, LOW); // initialize pin as off
}

void loop() {
  if (millis() > Bot_lasttime + Bot_mtbs)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    Bot_lasttime = millis();
  }
}