#include <TimeLib.h>
#include <DS1307RTC.h>
#include "disp.h"

int fontSize = 8;
int didPin=12;
int didPin2=10;

void drawGrid(rgba_t col)
{
  for(int y=0; y<screenWidth; y+=8)
  {
      drawVLine(screenHeight, y, 0, col);
  }
  for(int x=0; x<screenHeight; x+=8)
  {
      drawHLine(screenWidth, x, col);
  }
}


void printDebug(char* text, rgba_t color)
{
  setDisp(1);
  printText(text, color);
  setDisp(0);
}

void displayFolder(int gridX, int gridY, char* text)
{
  rgba_t black = getColor(0,0,0, 255);
  rgba_t white = getColor(255,255,255, 255);

  char debugTxt[128];
  sprintf(debugTxt, "DRAWING FOLDER \"%s\" AT %d,%d\n", text, gridX,gridY);
  printDebug(debugTxt, white);

  int folderWidth = 64;
  int folderHeight = 64;

  int folder_x = (folderWidth)*(gridX-1);
  int folder_y = (folderHeight+fontSize)*(gridY-1); 
  drawBinaryImage(folder_x, folder_y, folderWidth, folderHeight, "folder.bin");

  // Define the text and font size
  fontSize = 8;

  // Calculate the X-coordinate for the text to center it under the image
  int text_x = folder_x + (folderWidth - (fontSize * (strlen(text) - 1))) / 2;

  // Calculate the Y-coordinate for the text to place it below the image
  int text_y = folder_y + folderHeight; // Adjust the vertical spacing as needed

  // Set the cursor to the calculated position
  setCursor(text_x, text_y);
  printText(text, black);
}

void setup() {
  pinMode(didPin, OUTPUT);
  pinMode(didPin2, OUTPUT);

  delay(100);

  // setup id's
  digitalWrite(didPin, HIGH);
  digitalWrite(didPin, LOW);

  digitalWrite(didPin2, HIGH);
  digitalWrite(didPin2, LOW);
  digitalWrite(didPin2, HIGH);
  digitalWrite(didPin2, LOW);

  // put your setup code here, to run once:
  Serial.begin(115200);
  disp_init();

  // colors
  rgba_t taskBarColor = getColor(164,164,164, 255);
  rgba_t bg = getColor(0,128,128, 255);
  rgba_t white = getColor(255,255,255, 255);
  rgba_t black = getColor(0,0,0, 255);

  // draw test rect
  //drawRect(0,0, screenWidth,screenHeight, true, bg);
  setDisp(0);
  for(int y=0; y<screenHeight; y++)
  {
    drawHLine(screenWidth, y, bg);
  }

  // other disp
  setDisp(1);
  for(int y=0; y<screenHeight; y++)
  {
    drawHLine(screenWidth, y, bg);
  }
  setDisp(0);

  // draw bg.
  //drawBinaryImage(0,0, 640,480, "bigbg.bin");

  // Draw the image
  printDebug("Drawing WCWL.BIN\n", white);
  int wcwlWidth = 240;
  int wcwlHeight = 56;
  int image_x = screenWidth / 2 - wcwlWidth / 2;
  int image_y = screenHeight / 2 - wcwlHeight / 2;
  drawBinaryImage(image_x, image_y, wcwlWidth, wcwlHeight, "wcwl.bin");

  // Define the text and font size
  char* text = "This display works!!!";

  // Calculate the X-coordinate for the text to center it
  int text_x = screenWidth / 2 - (fontSize * (strlen(text) - 1)) / 2;

  // Calculate the Y-coordinate for the text, placing it below the image
  int text_y = image_y + wcwlHeight + 10; // Adjust the vertical spacing as needed

  // Set the cursor to the calculated position
  setCursor(text_x, text_y);
  printText(text, white);

  // draw taskbar
  int tbWidth = screenWidth;
  int tbHeight = 32;

  int tb_x = 0;
  int tb_y = screenHeight-tbHeight;

  printDebug("Drawing Taskbar\n", white);
  drawRect(tb_x,tb_y, tbWidth,tbHeight, true, taskBarColor);

  // draw rainbow dash
  printDebug("Drawing Rainbow Dash )RD.BIN(\n", white);
  drawBinaryImage(0,(screenHeight-tbHeight)-128, 128,128, "rd.bin");

  // draw twilight sparkle
  printDebug("Drawing Twilight Sparkle )TS.BIN(\n", white);
  drawBinaryImage(screenWidth-128,(screenHeight-tbHeight)-128, 128,128, "ts.bin");

  tmElements_t tm;
  if (RTC.read(tm)) {
    char time[64];
    sprintf(time, "%02d:%02d %d/%d/%d", tm.Hour, tm.Minute, tm.Day, tm.Month, tmYearToCalendar(tm.Year));

    char *dateNTimeText = "DATE/TIME";
    char *taskBarText = "THIS IS TASKBAR";

    text_x = 0;
    text_y = screenHeight-(fontSize);
    setCursor(text_x, text_y);
    printText(taskBarText, black);

    text_x = screenWidth - (fontSize * (strlen(dateNTimeText)));
    text_y = screenHeight-(fontSize*2);
    setCursor(text_x, text_y);
    printText(dateNTimeText, black);

    text_x = screenWidth - (fontSize * (strlen(time)));
    text_y = screenHeight-fontSize;
    setCursor(text_x, text_y);
    printText(time, black);
  }

  // draw desktop icons
  displayFolder(1,1, "YOUR PC");

  // the MLP folder
  displayFolder(1,2, "MLP");

  // draw active border
  int folderWidth = 64;
  int folderHeight = 64;
  int gridX=1;
  int gridY=2;
  int folder_x = (folderWidth)*(gridX-1);
  int folder_y = (folderHeight+fontSize)*(gridY-1);
  drawRect(folder_x, folder_y, folderWidth, folderHeight, false, white);


  displayFolder(2,1, "MEMES");
  displayFolder(2,2, "YEET");

  // put a pattern onto the uart data monitor chip
  //Serial1.write(0xaa);

  // tone
  tone(11, 1000, 100);
  delay(100);
  tone(11, 2000, 500);
  delay(500);
}

void loop() {
  // put your main code here, to run repeatedly:

}
uart-data-monBreakout
GND5VSDASCLSQWRTCDS1307+
interfacetestBreakout
disp2Breakout