#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
#include <math.h>

// Define the TFT pins for the ESP32-S2
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 12
#define TFT_LED 21 // (optional for backlight control)

// SPI Pins for ESP32-S2
#define TFT_MOSI 35 // Data Out
#define TFT_SCLK 36 // Clock
#define TFT_MISO -1 // Not used, can be any available pin

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

// TFT display resolution
const int TFT_WIDTH = 240;
const int TFT_HEIGHT = 320;

// Center of the clock
const int centerX = TFT_WIDTH / 2;
const int centerY = TFT_HEIGHT / 2-30;
const int clockRadius = 100; // Radius of the clock

// Variables to store the previous hand positions
int prevSecX = centerX, prevSecY = centerY;

void setup() {
  Serial.begin(115200); // Start serial communication for debugging

  // Initialize the display
  tft.begin();
  tft.setRotation(1); // Set the rotation if needed

  // Turn on the backlight if applicable
  pinMode(TFT_LED, OUTPUT);
  digitalWrite(TFT_LED, HIGH);

  // Clear the screen with a black color
  tft.fillScreen(ILI9341_BLACK);

  // Draw the clock face once
  drawClockFace();
}

void loop() {
  // Get the current time in milliseconds
  unsigned long currentMillis = millis();
  unsigned long seconds = (currentMillis / 1000) % 60;
  unsigned long minutes = (currentMillis / (1000 * 60)) % 60;
  unsigned long hours = (currentMillis / (1000 * 60 * 60)) % 24;

  // Clear the previous second hand
  clearHand(prevSecX, prevSecY);

  // Draw the hour, minute, and second hands
  drawHand(hours % 12 * 30 + minutes * 0.5, clockRadius - 40, ILI9341_RED);
  drawHand(minutes * 6, clockRadius - 20, ILI9341_BLUE);

  // Calculate the new second hand position
  int secX = centerX + (clockRadius - 10) * cos((seconds * 6 - 90) * DEG_TO_RAD);
  int secY = centerY + (clockRadius - 10) * sin((seconds * 6 - 90) * DEG_TO_RAD);

  // Draw the second hand
  tft.drawLine(centerX, centerY, secX, secY, ILI9341_GREEN);

  // Save the current second hand position
  prevSecX = secX;
  prevSecY = secY;

  delay(1000); // Update every second
}

void drawClockFace() {
  // Draw the clock circle
  tft.drawCircle(centerX, centerY, clockRadius, ILI9341_WHITE);

  // Draw the clock numbers
  for (int i = 0; i < 12; i++) {
    float angle = i * 30 * DEG_TO_RAD;
    int x = centerX + (clockRadius - 10) * cos(angle);
    int y = centerY + (clockRadius - 10) * sin(angle);
    tft.setCursor(x - 5, y - 5);
    tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
    tft.setTextSize(1);
    tft.print(i == 0 ? 12 : i);
  }
}

void drawHand(float angle, int length, uint16_t color) {
  angle -= 90; // Adjust for 0 degrees being at the top
  float angleRad = angle * DEG_TO_RAD;
  int x = centerX + length * cos(angleRad);
  int y = centerY + length * sin(angleRad);
  tft.drawLine(centerX, centerY, x, y, color);
}

void clearHand(int x, int y) {
  // Draw over the previous second hand with the background color
  tft.drawLine(centerX, centerY, x, y, ILI9341_BLACK);
}