#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL3XvWT_aYr"
#define BLYNK_TEMPLATE_NAME "poject3"
#define BLYNK_AUTH_TOKEN "O6zkM7ErXBvIgU5jg5ztdcCtUCvDbuXA"


#include <WiFi.h>  // Ganti ESP8266WiFi.h dengan WiFi.h untuk ESP32
#include <BlynkSimpleEsp32.h>  // Ganti BlynkSimpleEsp8266.h dengan BlynkSimpleEsp32.h

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

// Array data provinsi
String provinces[] = {"Aceh", "Sumatera Utara", "Sumatera Barat", "Riau", "Kepulauan Riau", "Jambi", 
                      "Sumatera Selatan", "Bangka Belitung", "Bengkulu", "Lampung", "DKI Jakarta", 
                      "Banten", "Jawa Barat", "Jawa Tengah", "DI Yogyakarta", "Jawa Timur", 
                      "Bali", "Nusa Tenggara Barat", "Nusa Tenggara Timur", "Kalimantan Barat", 
                      "Kalimantan Tengah", "Kalimantan Selatan", "Kalimantan Timur", "Kalimantan Utara", 
                      "Sulawesi Utara", "Gorontalo", "Sulawesi Tengah", "Sulawesi Selatan", 
                      "Sulawesi Tenggara", "Sulawesi Barat", "Maluku", "Maluku Utara", 
                      "Papua Barat", "Papua"};

float latitudes[] = {5.5483, 3.5970, -0.9471, 0.5333, 1.1500, -1.4852, -3.3194, -2.7410, -3.7928, -5.4286, -6.2088, 
                     -6.4058, -6.9175, -7.1500, -7.7956, -7.2504, -8.4095, -8.6529, -10.1772, -0.0333, 
                     -2.2150, -3.3186, -1.2379, 2.0000, 1.4930, 0.5473, -0.8989, -5.1477, -4.1299, 
                     -2.6786, -3.2385, 0.6342, -1.3375, -4.2699};

float longitudes[] = {95.3238, 98.6785, 100.4172, 101.4500, 104.0200, 103.7381, 104.9147, 106.4406, 102.2665, 105.2000, 
                      106.8456, 106.0640, 107.6191, 110.1403, 110.3695, 112.7688, 115.1889, 116.3249, 
                      123.6070, 109.3333, 113.9133, 114.5908, 116.8526, 117.5000, 124.8413, 123.0596, 
                      119.8707, 119.4327, 122.0874, 118.9047, 130.1453, 127.9716, 133.1747, 138.0804};

float altitudes[] = {125, 33, 5, 15, 10, 17, 12, 23, 9, 29, 8, 
                     85, 738, 327, 113, 21, 87, 102, 625, 50, 
                     27, 48, 134, 183, 12, 7, 91, 18, 25, 6, 
                     59, 55, 1372, 483};

// Timezones
String timezones[] = {"WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", 
                      "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", 
                      "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WIB (UTC+7)", "WITA (UTC+8)", 
                      "WITA (UTC+8)", "WITA (UTC+8)", "WIB (UTC+7)", "WIB (UTC+7)", "WITA (UTC+8)", 
                      "WITA (UTC+8)", "WITA (UTC+8)", "WITA (UTC+8)", "WITA (UTC+8)", "WITA (UTC+8)", 
                      "WITA (UTC+8)", "WITA (UTC+8)", "WITA (UTC+8)", "WIT (UTC+9)", "WIT (UTC+9)", 
                      "WIT (UTC+9)", "WIT (UTC+9)"};

// Virtual pins
#define VPIN_NUMERIC_INPUT V1
#define VPIN_PROVINSI V2
#define VPIN_LATITUDE V3
#define VPIN_LONGITUDE V4
#define VPIN_TIMEZONE V5
#define VPIN_ALTITUDE V6  // Tambahkan virtual pin untuk altitude

BlynkTimer timer;

BLYNK_WRITE(VPIN_NUMERIC_INPUT) {
  int selection = param.asInt();  // Get the numeric input

  if (selection >= 0 && selection < 34) {
    // Send the selected province's name, latitude, longitude, timezone, and altitude to corresponding widgets
    Blynk.virtualWrite(VPIN_PROVINSI, provinces[selection]);
    Blynk.virtualWrite(VPIN_LATITUDE, String(latitudes[selection]));
    Blynk.virtualWrite(VPIN_LONGITUDE, String(longitudes[selection]));
    Blynk.virtualWrite(VPIN_TIMEZONE, timezones[selection]);
    Blynk.virtualWrite(VPIN_ALTITUDE, String(altitudes[selection]));  // Kirim data altitude
  } else {
    // Out of bounds, clear the labels
    Blynk.virtualWrite(VPIN_PROVINSI, "Invalid selection");
    Blynk.virtualWrite(VPIN_LATITUDE, "");
    Blynk.virtualWrite(VPIN_LONGITUDE, "");
    Blynk.virtualWrite(VPIN_TIMEZONE, "");
    Blynk.virtualWrite(VPIN_ALTITUDE, "");  // Hapus data altitude jika pilihan tidak valid
  }
}

void setup() {
  // Debug console
  Serial.begin(115200);

  // Connect to Wi-Fi and Blynk
  Blynk.begin(auth, ssid, pass, "iot.serangkota.go.id", 8080);

  // Setup a timer to keep the connection alive
  timer.setInterval(1000L, []() { Blynk.run(); });
}

void loop() {
  Blynk.run();
  timer.run();  // Run timer to keep connection alive
}