#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6igkO5fK6"
#define BLYNK_TEMPLATE_NAME "RFID"
#define BLYNK_AUTH_TOKEN "YblsssubUKz4zUCJVKS_XLNQBA8UfMDc"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;

#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 20, 4);

#include <ezTime.h>
Timezone local;

int const pb1=14, pb2=12;

BLYNK_CONNECTED() 
{ Blynk.sendInternal("utc", "time");      
  Blynk.sendInternal("utc", "tz_rule");
  Blynk.sendInternal("rtc", "sync");
  Blynk.syncAll();
}
BLYNK_WRITE(InternalPinUTC) 
{ String cmd = param[0].asStr();
  if (cmd == "time") 
  { const uint64_t utc_time = param[1].asLongLong();
    UTC.setTime(utc_time / 1000, utc_time % 1000);
  } 
  else if (cmd == "tz_rule") 
  { String tz_rule = param[1].asStr();
    local.setPosix(tz_rule);
  }
}
long ts;
BLYNK_WRITE(InternalPinRTC) { ts = param.asLong(); }

char tx2buf[100];
void localTime() 
{ sprintf(tx2buf,"Time:%d:%d:%d Day:%d/%d/%d",
  local.hour(),local.minute(),local.second(),
  local.day(),local.month(),local.year());
  
  Blynk.virtualWrite(V5,tx2buf);
  Serial.println(tx2buf);
}

int cnt=0,kira=0,flag=0;
int toggle1=0,toggle2=0,toggle3=0,toggle4=0,toggle5=0,toggle6=0;

void menu()
{ if(digitalRead(pb1)==0 && flag==0)
  { cnt++;
    while(digitalRead(pb1)==0);
    if(cnt==4) cnt=0;
    delay(200);
    lcd.clear();
  }
 
  if(cnt==0)
  { lcd.setCursor(0,0);   lcd.print("Summary: ");
  
    lcd.setCursor(0,2);   sprintf(tx2buf,"Time: %d:%d:%d",
                          local.hour(),local.minute(),local.second());
                          lcd.print(tx2buf);

    lcd.setCursor(0,3);   sprintf(tx2buf,"Day:%d/%d/%d",
                          local.day(),local.month(),local.year());
                          lcd.print(tx2buf);
    
    lcd.setCursor(19,3);  lcd.print(">");
  }
  if(cnt==1)
  { lcd.setCursor(0,0);   lcd.print("Service: pg1");
    lcd.setCursor(0,2);   if(toggle1==0) lcd.print("1.Chg Filter    X");
                          if(toggle1==1) lcd.print("1.Chg Filter    /");
    lcd.setCursor(0,3);   if(toggle2==0) lcd.print("2.Svc Drainage  X"); 
                          if(toggle2==1) lcd.print("2.Svc Drainage  /");   
    lcd.setCursor(19,3);  lcd.print(">"); 
  }
  if(cnt==2)
  { lcd.setCursor(0,0);   lcd.print("Service: pg2");
    lcd.setCursor(0,2);   if(toggle3==0) lcd.print("3.Belting Chk   X");
                          if(toggle3==1) lcd.print("3.Belting Chk   /");
    lcd.setCursor(0,3);   if(toggle4==0) lcd.print("4.Svc Cool Coil X");
                          if(toggle4==1) lcd.print("4.Svc Cool Coil /");
    lcd.setCursor(19,3);  lcd.print(">"); 
  }
  if(cnt==3)
  { lcd.setCursor(0,0);   lcd.print("Setting");
    lcd.setCursor(0,2);   if(toggle5==0) lcd.print("1.Alarm         X");
                          if(toggle5==1) lcd.print("1.Alarm         /");
    lcd.setCursor(0,3);   if(toggle6==0) lcd.print("2.Damper        X");
                          if(toggle6==1) lcd.print("2.Damper        /");
    lcd.setCursor(19,3);  lcd.print(">"); 
  }

  if(digitalRead(pb2)==0 && cnt!=0)
  { kira++;
    flag=1;
    if(kira==3) { kira=0; flag=0; }
    while(digitalRead(pb2)==0);
    delay(200);
  }
  if(kira==0) { lcd.setCursor(15,3);  lcd.print(" "); }
  if(kira==1) { lcd.setCursor(15,2);  lcd.print("-"); 
                lcd.setCursor(15,3);  lcd.print(" "); 
              }
  if(kira==2) { lcd.setCursor(15,2);  lcd.print(" "); 
                lcd.setCursor(15,3);  lcd.print("-"); 
              }

  if(digitalRead(pb1)==0 && flag==1)
  { if(kira==1 && cnt==1) toggle1=!toggle1;
    if(kira==2 && cnt==1) toggle2=!toggle2;
    if(kira==1 && cnt==2) toggle3=!toggle3;
    if(kira==2 && cnt==2) toggle4=!toggle4;
    if(kira==1 && cnt==3) toggle5=!toggle5;
    if(kira==2 && cnt==3) toggle6=!toggle6;
    
    while(digitalRead(pb1)==0);
    delay(200);   
  }
}

void setup()
{ lcd.begin();
  lcd.backlight();

  Serial.begin(9600);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  pinMode(pb1, INPUT_PULLUP);
  pinMode(pb2, INPUT_PULLUP);

  timer.setInterval(10,menu);
}

void loop()
{ Blynk.run();
  timer.run();
}