#include "RTClib.h"
#include<LiquidCrystal.h>
#include <EEPROM.h>
//contruct lcd
const int rs=8,en=7,D4=6,D5=5,D6=4,D7=3;
LiquidCrystal lcd(rs,en,D4,D5,D6,D7);

//rtc button
RTC_DS1307 rtc;
const int ButtonA=13;
const int ButtonB=12;
const int Buzzer=11;

bool alarmSetupMode = false; 
bool haveArlam = false;
void setup() {
  //check Arlam state
  if(readIntFromEEPROM(4)==1){
    haveArlam=true;
    Serial.println("have arlam");
  }
  
  Serial.begin(9600);
  pinMode(Buzzer, OUTPUT);
  pinMode(ButtonA,INPUT);
  pinMode(ButtonB,INPUT);

  lcd.begin(16,2);
  if (!rtc.begin()) {
    Serial.println("not found RTC");
    while(1);
  }
  // DateTime newTime(2024, 7, 14, 9, 59, 50);
  // rtc.adjust(newTime); 
}

void loop() {
  DateTime now = rtc.now();
  //check Arlam state
  if(readIntFromEEPROM(4)==1){
    haveArlam=true;
    Serial.print("have arlam:");
    Serial.print(readIntFromEEPROM(0));
    Serial.print(":");
    Serial.println(readIntFromEEPROM(2));
  }else{
    haveArlam=false;
  }

  //press A enter setupmode
  if (digitalRead(ButtonA) == HIGH) {
    alarmSetupMode = true;
  }
  //press B delete arlam
  if (digitalRead(ButtonB) == HIGH) {
    writeIntToEEPROM(4, 0);
    lcd.clear();
    lcd.write("removed arlam");
    delay(2000);
    lcd.clear();
  }


  if (alarmSetupMode) {
    setupAlarm();
  } else {
    displayCurrentTime(now);

  }
  //arlam trigger
  if (haveArlam && now.hour() == readIntFromEEPROM(0) && now.minute() == readIntFromEEPROM(2) && now.second()==0) {
    Serial.println("Alarm triggered!");
    tone(Buzzer, 400, 5000);
  }
  
  delay(1000);
}


void displayCurrentTime(DateTime now) {


  int hour = now.hour();
  int minute = now.minute();
  int second = now.second();
  lcd.setCursor(4, 0);
  if (hour < 10) {
    lcd.print("0");
  }
  lcd.print(hour);
  lcd.print(":");

  if (minute < 10) {
    lcd.print("0");
  }
  lcd.print(minute);
  lcd.print(":");

  if (second < 10) {
    lcd.print("0");
  }

  lcd.print(second);
  lcd.setCursor(0,0);


  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.print(second);
  Serial.println();
  
}


void writeIntToEEPROM(int address, int number)
{ 
  byte byte1 = number >> 8;
  //1111 1111
  byte byte2 = number & 0xFF;
  EEPROM.write(address, byte1);
  EEPROM.write(address + 1, byte2);
}

int readIntFromEEPROM(int address)
{
  byte byte1 = EEPROM.read(address);
  byte byte2 = EEPROM.read(address + 1);
  return (byte1 << 8) + byte2;
}


void setupAlarm() {
  lcd.clear();
  lcd.print("Set Alarm");
  lcd.setCursor(0, 1);
  lcd.print("00:00");

  int alarmHour = 0;
  int alarmMinute = 0;
  bool setHour = true;
  delay(500);
  while (alarmSetupMode) {
    //setup hour first
    if (setHour) {
      if (digitalRead(ButtonB) == HIGH) {
        delay(200);

        alarmHour++;
        if (alarmHour > 23) {
          alarmHour = 0;
        }

        //display hours
        lcd.setCursor(0, 1);
        if (alarmHour < 10) {
          lcd.print("0");
        }
        lcd.print(alarmHour);
      }

      // go setup minus mode if A press
      if (digitalRead(ButtonA) == HIGH) {
        delay(200);
        setHour = false;
      }

    //not in setup hour state
    } else {
      // setup minus
      if (digitalRead(ButtonB) == HIGH) {
        delay(200);

        alarmMinute++;
        if (alarmMinute > 59) {
          alarmMinute = 0;
        }

        //display minus
        lcd.setCursor(3, 1);
        if (alarmMinute < 10) {
          lcd.print("0");
        }
        lcd.print(alarmMinute);
      }

      // ButtonA press setup arlam done 
      if (digitalRead(ButtonA) == HIGH) {
        delay(200); 
        alarmSetupMode = false;

        writeIntToEEPROM(0, alarmHour);
        writeIntToEEPROM(2, alarmMinute);
        //save have arlam state
        writeIntToEEPROM(4, 1);
        lcd.clear();
      }
    }
  }
}
$abcdeabcde151015202530354045505560fghijfghij
GND5VSDASCLSQWRTCDS1307+