#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET    -1  // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

// WiFi credentials
const char *ssid     = "Wokwi-GUEST";
const char *password = "";

// NTP Client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 60000); // Time offset in seconds (3600 for GMT+1) and update interval (60000ms)

// Button and LED GPIOs
const int hourButtonPin = 14;
const int minuteButtonPin = 37;
const int dayNightButtonPin = 36;
const int dayLedPin = 46;
const int nightLedPin = 11;

// Alarm Time Variables (initially set to 8:00 AM as an example)
int alarmHour = 8;
int alarmMinute = 0;
bool dayMode = true; // Start in day mode

void setup() {
  Serial.begin(115200);
  pinMode(hourButtonPin, INPUT_PULLUP);
  pinMode(minuteButtonPin, INPUT_PULLUP);
  pinMode(dayNightButtonPin, INPUT_PULLUP);
  pinMode(dayLedPin, OUTPUT);
  pinMode(nightLedPin, OUTPUT);
  digitalWrite(dayLedPin, HIGH); // Start in day mode
  digitalWrite(nightLedPin, LOW);

  // Initialize SSD1306 display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("coucou");

  // Connect to WiFi
  WiFi.begin(ssid, password, 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");

  // Initialize NTP Client
  timeClient.begin();
  Serial.println("Ended setup");
}

void loop() {
  Serial.println("Starting loop");
  timeClient.update();

  // Display time and alarm time
  displayTime();

  // Check for button presses to set alarm
  if(digitalRead(hourButtonPin) == LOW) {
    delay(200); // Debounce delay
    alarmHour = (alarmHour + 1) % 24;
  }

  if(digitalRead(minuteButtonPin) == LOW) {
    delay(200); // Debounce delay
    alarmMinute = (alarmMinute + 1) % 60;
  }

  if(digitalRead(dayNightButtonPin) == LOW) {
    delay(200); // Debounce delay
    toggleDayNightMode();
  }

  // Check if it's time to trigger the alarm
  if(timeClient.getHours() == alarmHour && timeClient.getMinutes() == alarmMinute) {
    dayMode = true; // Switch to day mode at alarm time
    updateLEDs();
  }

  delay(1000); // Loop delay
}

void displayTime() {
  display.clearDisplay();
  display.setCursor(0,0);
  display.print("Time: ");
  display.print(timeClient.getHours());
  display.print(":");
  if(timeClient.getMinutes() < 10) display.print('0'); // Leading zero for minutes
  display.print(timeClient.getMinutes());
  display.print("\nAlarm: ");
  display.print(alarmHour);
  display.print(":");
  if(alarmMinute < 10) display.print('0'); // Leading zero for minutes
  display.print(alarmMinute);
  display.print(dayMode ? "\nMode: Day" : "\nMode: Night");
  display.display();
}

void toggleDayNightMode() {
  dayMode = !dayMode;
  updateLEDs();
}

void updateLEDs() {
  digitalWrite(dayLedPin, dayMode ? HIGH : LOW);
  digitalWrite(nightLedPin, !dayMode ? HIGH : LOW);
}