// #include <WiFi.h>
// #include <WiFiClient.h>
 
// String ssid = "Wokwi-GUEST";    //nama ssid wifi kalian

// String pass = "";  //password wifi kalian

// String token = "6518526803:AAEe9_lemPbNfjXYz4fRxsS8AtslFXoDmoos";    //token bot baru kalian

// const int id = 5412754826;      //id telegram kalian
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// SSID dan Password WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";

// Telegram BOT
#define BOTtoken "6531867748:AAFaKosbkW4_7xQh3LNZjcUps_-nv86g88U"
#define CHAT_ID "5412754826"

#define pin_merah   23
#define pin_putih   22

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Cek Pesan Setiap 1 detik
int interval = 1000;
unsigned long waktu_terakhir;
bool status_merah, status_putih;

void setup() {
  // atur mode pin menjadi output
  pinMode(pin_merah, OUTPUT);
  pinMode(pin_putih, OUTPUT);

  // Serial monitor
  Serial.begin(115200);
  
  // Hubungkan ke Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Sedang menghubungkan ke WiFi..");
  }
  
  // IP Address
  Serial.println(WiFi.localIP());
  Serial.print("Atur Waktu: ");
  configTime(0, 0, "pool.ntp.org"); 
  time_t now = time(nullptr);
  while (now < 24 * 3600)
  {
    Serial.print(".");
    delay(100);
    now = time(nullptr);
  }
  Serial.println(now);
}

void loop() {
  
  cek_pesan();
}

// Cek Pesan Terbaru
void cek_pesan(){
  if (millis() > waktu_terakhir + interval){
    int banyakPesan = bot.getUpdates(bot.last_message_received + 1);
  
    while(banyakPesan) {
      Serial.println("got response");
      handleNewMessages(banyakPesan);
      banyakPesan = bot.getUpdates(bot.last_message_received + 1);
    }
    waktu_terakhir = millis();
  }
}

// Memproses pesan yang diterima
void handleNewMessages(int numNewMessages) {
  for (int i=0; i<numNewMessages; i++) {
    
    // Cek Chat ID
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    
    // Terima pesan dari telegram
    String text = bot.messages[i].text;
    Serial.println(text);

    String from_name = bot.messages[i].from_name;

    if (text == "/start") {
      String welcome = "Selamat Datang, " + from_name + ".\n";
      welcome += "Komen berikut untuk kontrol LED.\n\n";
      welcome += "/merah_on untuk LED Merah ON \n";
      welcome += "/merah_off untuk LED Merah OFF \n";
      welcome += "/putih_on untuk LED Putih ON \n";
      welcome += "/putih_off untuk LED Putih OFF \n";
      welcome += "/status_merah mendapatkan status LED Merah\n";
      welcome += "/status_putih mendapatkan status LED Putih\n";
      bot.sendMessage(chat_id, welcome, "");
    }

    if (text == "/merah_on") {
      bot.sendMessage(chat_id, "LED Merah ON", "");
      led_merah(true);
    }
    
    if (text == "/merah_off") {
      bot.sendMessage(chat_id, "LED Merah OFF", "");
      led_merah(false);
    }

    if (text == "/putih_on") {
      bot.sendMessage(chat_id, "LED Putih ON", "");
      led_putih(true);
    }
    
    if (text == "/putih_off") {
      bot.sendMessage(chat_id, "LED Putih OFF", "");
      led_putih(false);
    }
    
    if (text == "/status_merah") {
      if (digitalRead(pin_merah)){
        bot.sendMessage(chat_id, "LED Merah ON", "");
      }
      else{
        bot.sendMessage(chat_id, "LED Merah OFF", "");
      }
    }
    
    if (text == "/status_putih") {
      if (digitalRead(pin_putih)){
        bot.sendMessage(chat_id, "LED Putih ON", "");
      }
      else{
        bot.sendMessage(chat_id, "LED Putih OFF", "");
      }
    }
  }
}

void led_merah(bool isOn){
  if(isOn){
    digitalWrite(pin_merah, HIGH);
  }
  else{
    digitalWrite(pin_merah, LOW);
  }
}

void led_putih(bool isOn){
  if(isOn){
    digitalWrite(pin_putih, HIGH);
  }
  else{
    digitalWrite(pin_putih, LOW);
  }
}