// #include <SPI.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_ILI9341.h>
// #include <TimeLib.h>
// #include <WiFi.h>
// #include <NTPClient.h>
// #include <WiFiUdp.h>



// // #define TFT_CS    15     // TFT CS  pin is connected to NodeMCU pin D2
// // #define TFT_RST   4     // TFT RST pin is connected to NodeMCU pin D3
// // #define TFT_DC    2     // TFT DC  pin is connected to NodeMCU pin D4
// // // initialize ILI9341 TFT library with hardware SPI module
// // // SCK (CLK) ---> NodeMCU pin D5 (GPIO14)
// // // MOSI(DIN) ---> NodeMCU pin D7 (GPIO13)
// // Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);


// // Pin definitions for ESP32 and ILI9341
// #define TFT_CS    15
// #define TFT_RST   4  // Reset pin not used
// #define TFT_DC    2
// #define TFT_MOSI  23
// #define TFT_CLK   18
// #define TFT_MISO  19

// // Initialize TFT display
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TimeLib.h>
#include <ctime>

// Pin definitions for ESP32 and ILI9341
#define TFT_CS    15
#define TFT_RST   -1  // Reset pin not used
#define TFT_DC    23
#define TFT_MOSI  23
#define TFT_CLK   18
#define TFT_MISO  19

// Future date
#define FUTURE_YEAR   2024
#define FUTURE_MONTH  4
#define FUTURE_DAY    20

// Initialize TFT display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

void setup() {
  Serial.begin(9600);
  
  // Initialize TFT display
  tft.begin();
  tft.setRotation(1); // Adjust as needed for display orientation
  
  // Initialize time with current time
  time_t currentTime = now();
  setTime(hour(currentTime), minute(currentTime), second(currentTime), day(currentTime), month(currentTime), year(currentTime));
  
  // Update time display
  updateDisplay();
}

void loop() {
  // Update time every hour
  if (hour() == 0 && minute() == 0 && second() == 0) {
    updateTime();
    updateDisplay();
  }
}

void updateTime() {
  // Increment time by an hour
  adjustTime(3600);
}

void updateDisplay() {
  // Clear previous display
  tft.fillScreen(ILI9341_BLACK);

  // Create a tm struct representing the future date
  tm futureDate;
  futureDate.tm_year = FUTURE_YEAR - 1900; // Years since 1900
  futureDate.tm_mon = FUTURE_MONTH - 1;     // Month (0-11)
  futureDate.tm_mday = FUTURE_DAY;          // Day of the month
  futureDate.tm_hour = 0;                   // Hour (0-23)
  futureDate.tm_min = 0;                    // Minute (0-59)
  futureDate.tm_sec = 0;                    // Second (0-59)

  // Convert tm struct to time_t variable
  time_t futureTime = mktime(&futureDate);

  // Calculate time difference
  time_t currentTime = now();
  time_t timeDifference = futureTime - currentTime;

  // Calculate days and hours
  int days = timeDifference / 86400;
  int hours = (timeDifference % 86400) / 3600;

  // Print time difference to TFT display
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(20, 50);
  tft.print("Time Remaining:");
  tft.setCursor(20, 80);
  tft.print(days);
  tft.print(" days ");
  tft.print(hours);
  tft.println(" hours");
}