#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <TimeLib.h>  // Biblioteca para manejar tiempo
#include "Button.h"

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0; // Saved H, M, S x & y multipliers
float sdeg = 0, mdeg = 0, hdeg = 0;
uint16_t osx = 120, osy = 120, omx = 120, omy = 120, ohx = 120, ohy = 120; // Saved H, M, S x & y coords
uint16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0;
uint32_t targetTime = 0; // for next 1 second timeout

byte omm = 99;
byte xcolon = 0;

unsigned long cm;         

static uint8_t conv2d(const char *p);                                                // Forward declaration needed for IDE 1.6.x
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); // Get H, M, S from compile time

bool initial = 1;
bool showDigital = false; // Variable to switch between digital and analog display
bool updateDigital = false;


Button BtnSwitchMode(25);

void setup(void)
{
    hh = hh >= 5 ? hh - 5 : hh + 19;  // Subtract 5 hours from hh//Esto depende de la hora del sistema
    tft.init();
    tft.setRotation(0);

    // Draw clock face
    tft.fillCircle(120, 120, 118, TFT_GREEN);
    tft.fillCircle(120, 120, 110, TFT_BLACK);

    // Draw 12 lines
    for (int i = 0; i < 360; i += 30)
    {
        sx = cos((i - 90) * 0.0174532925);
        sy = sin((i - 90) * 0.0174532925);
        x0 = sx * 114 + 120;
        yy0 = sy * 114 + 120;
        x1 = sx * 100 + 120;
        yy1 = sy * 100 + 120;

        tft.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
    }

    // Draw 60 dots
    for (int i = 0; i < 360; i += 6)
    {
        sx = cos((i - 90) * 0.0174532925);
        sy = sin((i - 90) * 0.0174532925);
        x0 = sx * 102 + 120;
        yy0 = sy * 102 + 120;
        // Draw minute markers
        tft.drawPixel(x0, yy0, TFT_WHITE);

        // Draw main quadrant dots
        if (i == 0 || i == 180)
            tft.fillCircle(x0, yy0, 2, TFT_WHITE);
        if (i == 90 || i == 270)
            tft.fillCircle(x0, yy0, 2, TFT_WHITE);
    }

    targetTime = millis() + 1000;
}

void loop()
{
    cm=millis();
    BtnSwitchMode.update(cm);
    if(BtnSwitchMode.wasPressed()){
       tft.fillCircle(120, 120, 90, TFT_GREEN);
       if(showDigital){
        tft.fillCircle(120, 120, 90, TFT_BLACK);
       }
       else{
        tft.fillCircle(120, 120, 90, TFT_GREEN);
       }
       showDigital = !showDigital;
       updateDigital = true;
    }
    if (targetTime < millis())
    {
        targetTime += 1000;
        ss++; // Advance second
        if (ss == 60)
        {
            ss = 0;
            mm++; // Advance minute
            if (mm > 59)
            {
                mm = 0;
                hh++; // Advance hour
                if (hh > 23)
                {
                    hh = 0;
                }
            }
        }

        if (showDigital)
        {
            digitalClockDisplay();
        }
        else
        {
            analogClockDisplay();
        }
    }
}

void analogClockDisplay()
{
    // Pre-compute hand degrees, x & y coords for a fast screen update
    sdeg = ss * 6;                     // 0-59 -> 0-354
    mdeg = mm * 6 + sdeg * 0.01666667; // 0-59 -> 0-360 - includes seconds
    hdeg = hh * 30 + mdeg * 0.0833333; // 0-11 -> 0-360 - includes minutes and seconds
    hx = cos((hdeg - 90) * 0.0174532925);
    hy = sin((hdeg - 90) * 0.0174532925);
    mx = cos((mdeg - 90) * 0.0174532925);
    my = sin((mdeg - 90) * 0.0174532925);
    sx = cos((sdeg - 90) * 0.0174532925);
    sy = sin((sdeg - 90) * 0.0174532925);

    if (ss == 0 || initial)
    {
        initial = 0;
        // Erase hour and minute hand positions every minute
        tft.drawLine(ohx, ohy, 120, 121, TFT_BLACK);
        ohx = hx * 62 + 121;
        ohy = hy * 62 + 121;
        tft.drawLine(omx, omy, 120, 121, TFT_BLACK);
        omx = mx * 84 + 120;
        omy = my * 84 + 121;
    }

    // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
    tft.drawLine(osx, osy, 120, 121, TFT_BLACK);
    osx = sx * 90 + 121;
    osy = sy * 90 + 121;
    tft.drawLine(osx, osy, 120, 121, TFT_RED);
    tft.drawLine(ohx, ohy, 120, 121, TFT_WHITE);
    tft.drawLine(omx, omy, 120, 121, TFT_WHITE);
    tft.drawLine(osx, osy, 120, 121, TFT_RED);

    tft.fillCircle(120, 121, 3, TFT_RED);
}

void digitalClockDisplay()
{
    // Update digital time
    byte xpos = 50;
    byte ypos = 90;
    tft.setTextColor(0xF81F);   
    tft.drawString("Lunes", 85, 60, 4);
    tft.drawString(String(year()) + "/" + String(month()) + "/" + String(day()), 75, 150,4);

    if (omm != mm && !updateDigital)
    {                                           // Only redraw every minute to minimise flicker
        tft.setTextColor(TFT_GREEN, TFT_GREEN); // Set font colour to black to wipe image
        // Font 7 is to show a pseudo 7 segment display.
        // Font 7 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 0 : .
        tft.drawString("88:88", xpos, ypos, 7); // Overwrite the text to clear it
        tft.setTextColor(0xF81F);               // Orange
        omm = mm;

        if (hh < 10)
            xpos += tft.drawChar('0', xpos, ypos, 7);
        xpos += tft.drawNumber(hh, xpos, ypos, 7);
        xcolon = xpos;
        xpos += tft.drawChar(':', xpos, ypos, 7);
        if (mm < 10)
            xpos += tft.drawChar('0', xpos, ypos, 7);
        tft.drawNumber(mm, xpos, ypos, 7);
    }

    if(updateDigital){
          // Dibujar las líneas que conectan los círculos
          
/// /*
    for (int i = 0; i < 360; i += 1) {
      if (45 < i && i < 135){
        sx = cos((i - 90) * 0.0174532925);
        sy = sin((i - 90) * 0.0174532925);
        x0 = sx * 90 + 120; // Radio del círculo exterior
        yy0 = sy * 90 + 120;
        //x1 = sx * 60 + 120; // Radio del círculo interior
        //yy1 = sy * 60 + 160;
        x1 = 135;
        yy1 = 140;

        tft.drawLine(x0, yy0, x1, yy1, TFT_BLACK);
        }
      if (225 < i && i < 315){
        sx = cos((i - 90) * 0.0174532925);
        sy = sin((i - 90) * 0.0174532925);
        x0 = sx * 90 + 120; // Radio del círculo exterior
        yy0 = sy * 90 + 120;
        //x1 = sx * 60 + 120; // Radio del círculo interior
        //yy1 = sy * 60 + 160;
        x1 = 95;
        yy1 = 140;

       //tft.fillCircle(120, 120, 90, TFT_GREEN);
        tft.drawLine(x0, yy0, x1, yy1, TFT_BLACK);
        }
    }
    /// */

      if (hh < 10)
          xpos += tft.drawChar('0', xpos, ypos, 7);
      xpos += tft.drawNumber(hh, xpos, ypos, 7);
      xcolon = xpos;
      xpos += tft.drawChar(':', xpos, ypos, 7);
      if (mm < 10)
          xpos += tft.drawChar('0', xpos, ypos, 7);
      tft.drawNumber(mm, xpos, ypos, 7);
      updateDigital = false;
    }


    if (ss % 2)
    { // Flash the colon
        tft.setTextColor(0x39C4, TFT_GREEN);
        xpos += tft.drawChar(':', xcolon, ypos, 7);
        tft.setTextColor(0xF81F, TFT_GREEN);
    }
    else
    {
        tft.drawChar(':', xcolon, ypos, 7);
    }
}

static uint8_t conv2d(const char *p)
{
    uint8_t v = 0;
    if ('0' <= *p && *p <= '9')
        v = *p - '0';
    return 10 * v + *++p - '0';
}