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

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <TimeLib.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

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

BlynkTimer timer;
WidgetTerminal terminal(V3);
String daysofWeek[] = {
  "......",
  "Ahad  ",
  "Senin ",
  "Selasa",
  "Rabu  ",
  "Kamis ",
  "Jum'at",
  "Sabtu "
};
String hariNTP;

//*********declarate for pray calc*********
struct struct_param // loaded to EEPROM
{
  uint8_t state;         // 1 byte  add 0
  float   L_Latitude = -5.90513600;  // 4 byte  add 1
  float   L_Longitude = 105.3349090;  // 4 byte  add 5
  float   L_Altitude = 53;    // 4 byte  add 9
  float   L_TimeZone = 7 ;   // 4 byte  add 13
  float   Ihtiyati = 2;    // 4 byte  add 17
};

// Variable by Structure
struct_param    Prm;
//DateTime        now;
float           floatnow = 0;

//Variable Hijriah date
uint8_t   hD = 0;
uint8_t   hM = 0;
uint16_t  hY = 0;

//Other Variable
float sholatT[7] = {0, 0, 0, 0, 0, 0, 0};

// Variable Box Time
uint8_t dotSt = 0;


int Subuh = 0, Syuruq = 1, Zuhur = 3, Ashar = 4, Magrib = 5, Isya = 6;
int  subuhJam, subuhMenit, SyuruqJam, SyuruqMenit,
     zuhurJam, zuhurMenit, asharJam, asharMenit,
     maghribJam, maghribMenit, isyaJam, isyaMenit;

//************************************************
byte minutesPray, hoursPray, secondpray;
byte jamUpd, menitUpd, detikUpd, tglUpd, blnUpd;
byte tglNTP, blnNTP, jamNTP, menitNTP, detikNTP;
int tahunUpd, indexHariNTP, tahunNTP;
int addHourRTC, addMinuteRTC, subHourRTC, subMinuteRTC;
boolean f_corPrayTime, f_corRtcTime;
byte jamIn, menitIn;
byte jamSenin[30];
byte menitSenin[30];

void setup()
{
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Connecting to Blynk ");
  lcd.setCursor(0, 1);
  lcd.print("WiFi ");
  Blynk.begin(auth, ssid, pass, "iot.serangkota.go.id", 8080);
  timer.setInterval(100L, myTimerEvent);
  lcd.setCursor(0, 1);
  lcd.print("Success");
  delay(200);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Connected!");
  lcd.setCursor(0, 1);
  lcd.print("Blynk");
  delay(2000);
  lcd.clear();

}

void loop()
{
  Blynk.run();
  timer.run();
  requestTime();
  getpraytime();
  converttToSh();
  editRTC();
  banding();
  display();

}

void banding() {
  if (jamUpd == isyaJam && menitUpd == isyaMenit) {
    Serial.println("waktu zuhur");
  }

}


//experiment
void editRTC() {
  tglUpd = tglNTP;
  blnUpd = blnNTP;
  tahunUpd = tahunNTP;
  detikUpd = detikNTP;

  jamUpd = jamNTP + addHourRTC;
  menitUpd = menitNTP + addMinuteRTC;

  // Periksa dan sesuaikan nilai jam jika melebihi 23 atau kurang dari 0
  if (jamUpd > 23) {
    jamUpd = jamUpd % 24; // Kembali ke 0 jika melebihi 23
  } else if (jamUpd < 0) {
    jamUpd = 23; // Kembali ke 23 jika kurang dari 0
  }

  // Periksa dan sesuaikan nilai menit jika melebihi 59 atau kurang dari 0
  if (menitUpd > 59) {
    menitUpd = menitUpd % 60; // Kembali ke 0 jika melebihi 59
  } else if (menitUpd < 0) {
    menitUpd = 59; // Kembali ke 59 jika kurang dari 0
  }
}


void display() {
  lcd.setCursor(0, 0);
  lcd.print(jamUpd / 10);
  lcd.setCursor(1, 0);
  lcd.print(jamUpd % 10);
  lcd.setCursor(2, 0);
  lcd.print(":");
  lcd.setCursor(3, 0);
  lcd.print(menitUpd / 10);
  lcd.setCursor(4, 0);
  lcd.print(menitUpd % 10);
  lcd.setCursor(5, 0);
  lcd.print(":");
  lcd.setCursor(6, 0);
  lcd.print(detikUpd / 10);
  lcd.setCursor(7, 0);
  lcd.print(detikUpd % 10);
  lcd.print("<>");
  //--------------
  lcd.setCursor(10, 0);
  lcd.print(tglUpd / 10);
  lcd.setCursor(11, 0);
  lcd.print(tglUpd % 10);
  lcd.setCursor(12, 0);
  lcd.print("/");
  lcd.setCursor(13, 0);
  lcd.print(blnUpd / 10);
  lcd.setCursor(14, 0);
  lcd.print(blnUpd % 10);
  lcd.setCursor(15, 0);
  lcd.print("/");
  lcd.setCursor(16, 0);
  lcd.print(tahunUpd / 1000);
  lcd.setCursor(17, 0);
  lcd.print((tahunUpd % 1000) / 100);
  lcd.setCursor(18, 0);
  lcd.print((tahunUpd % 100) / 10);
  lcd.setCursor(19, 0);
  lcd.print(tahunUpd % 10);

  lcd.setCursor(0, 1);
  lcd.print("SB/");
  lcd.print(subuhJam / 10);
  lcd.print(subuhJam % 10);
  lcd.print(":");
  lcd.print(subuhMenit / 10);
  lcd.print(subuhMenit % 10);

  lcd.setCursor(0, 2);
  lcd.print("SQ/");
  lcd.print(SyuruqJam / 10);
  lcd.print(SyuruqJam % 10);
  lcd.print(":");
  lcd.print(SyuruqMenit / 10);
  lcd.print(SyuruqMenit % 10);

  lcd.setCursor(0, 3);
  lcd.print("ZH/");
  lcd.print( zuhurJam / 10);
  lcd.print( zuhurJam % 10);
  lcd.print(":");
  lcd.print(zuhurMenit / 10);
  lcd.print(zuhurMenit % 10);

  lcd.setCursor(12, 1);
  lcd.print("AR/");
  lcd.print( asharJam / 10);
  lcd.print( asharJam % 10);
  lcd.print(":");
  lcd.print(asharMenit / 10);
  lcd.print(asharMenit % 10);

  lcd.setCursor(12, 2);
  lcd.print("MG/");
  lcd.print( maghribJam / 10);
  lcd.print( maghribJam % 10);
  lcd.print(":");
  lcd.print(maghribMenit / 10);
  lcd.print(maghribMenit % 10);

  lcd.setCursor(12, 3);
  lcd.print("IS/");
  lcd.print(isyaJam / 10);
  lcd.print(isyaJam % 10);
  lcd.print(":");
  lcd.print(isyaMenit / 10);
  lcd.print(isyaMenit % 10);

}