#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include "icons.h"

// Include the header files for each function
#include "qrCodeScan.h"
#include "tirePressure.h"
#include "tireGrooves.h"
#include "settings.h"

// Pin definitions for the TFT display
#define TFT_CS 5
#define TFT_RST 16
#define TFT_DC 17
#define TFT_SCLK 18
#define TFT_MOSI 23
#define TFT_BACKLIGHT 4  // Backlight pin

// Button pin definitions
#define BUTTON_LEFT 32    // Left
#define BUTTON_RIGHT 33   // Right
#define BUTTON_SELECT 25  // Select
#define BUTTON_CANCEL 26  // Cancel/back

// Create TFT display object
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// Variables for menu navigation
int currentMenu = 0;
const int menuItems = 4;
// String menuOptions[] = {"QR Scan", "Tire Pressure", "Thread Depth", "Settings"};
const uint8_t* menuOptions[] = {
  bit_icn_qr_scan,
  bit_icn_tire_pressure,
  bit_icn_tire_groves,
  bit_icn_settings
};


void setup() {
  Serial.begin(115200);

  pinMode(TFT_BACKLIGHT, OUTPUT);
  analogWrite(TFT_BACKLIGHT, 255);  // Turn on the backlight at full brightness

  // Initialize TFT display
  tft.initR(INITR_BLACKTAB);
  tft.fillScreen(ST77XX_BLACK);
  tft.setRotation(0);

  // Set text properties
  tft.setTextSize(1);
  tft.setTextColor(ST77XX_WHITE);
  tft.write("Initializing...");

  // Initialize button pins
  pinMode(BUTTON_LEFT, INPUT_PULLUP);
  pinMode(BUTTON_RIGHT, INPUT_PULLUP);
  pinMode(BUTTON_SELECT, INPUT_PULLUP);
  pinMode(BUTTON_CANCEL, INPUT_PULLUP);


  delay(1500);
  // Display the main menu
  displayMenu();

  drawNavbar();

  Serial.print("started");
}

void loop() {
  // Check for button presses
  if (digitalRead(BUTTON_LEFT) == LOW) {
    currentMenu = (currentMenu - 1 + menuItems) % menuItems;
    displayMenu();
    tft.drawBitmap(11, 142, arrow_left_i, 18, 15, 0x58BF);
    delay(200);  // Debounce delay
  } else {
    tft.drawBitmap(11, 142, arrow_left_i, 18, 15, ST77XX_WHITE);
  }
  if (digitalRead(BUTTON_RIGHT) == LOW) {
    currentMenu = (currentMenu + 1) % menuItems;
    displayMenu();
    tft.drawBitmap(41, 142, arrow_right_i, 18, 15, 0x58BF);
    delay(200);  // Debounce delay
  } else {
    tft.drawBitmap(41, 142, arrow_right_i, 18, 15, ST77XX_WHITE);
  }
  if (digitalRead(BUTTON_SELECT) == LOW) {
    displaySelectedItem();
    tft.drawBitmap(70, 142, select_i, 15, 16, ST77XX_GREEN);
    delay(200);  // Debounce delay
  } else {
    tft.drawBitmap(70, 142, select_i, 15, 16, ST77XX_WHITE);
  }
  if (digitalRead(BUTTON_CANCEL) == LOW) {
    displayMenu();
    tft.drawBitmap(100, 142, cancel_i, 15, 16, 0xF800);
    delay(200);  // Debounce delay
  } else {
    tft.drawBitmap(100, 142, cancel_i, 15, 16, ST77XX_WHITE);
  }
}

void setBacklight(uint8_t brightness) {
  analogWrite(TFT_BACKLIGHT, brightness);  // Set the brightness (0-255)
}

void displayMenu() {
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(0, 0);
  tft.setTextSize(1);
  tft.setTextColor(ST77XX_YELLOW);
  tft.println("Main Menu");

  for (int i = 0; i < menuItems; i++) {
    int x = 8 + (i % 3) * 40;   // Calculate X position
    int y = 30 + (i / 3) * 40;  // Calculate Y position

    if (i == currentMenu) {
      // Highlight the selected icon
      // tft.drawRect(x - 1, y - 1, 37, 37, ST77XX_GREEN);
      tft.drawBitmap(x - 3, y - 3, menu_select_i, 41, 41, 0x540);
    }

    // Draw the icon
    tft.drawBitmap(x, y, menuOptions[i], 35, 35, ST77XX_WHITE);
  }
  drawNavbar();
}


void displaySelectedItem() {
  switch (currentMenu) {
    case 0:
      qrCodeScan(tft);
      break;
    case 1:
      tirePressure(tft);
      break;
    case 2:
      tireGrooves(tft);
      break;
    case 3:
      settings(tft);
      break;
    default:
      tft.fillScreen(ST77XX_BLACK);
      tft.setCursor(0, 0);
      tft.setTextSize(1);
      tft.setTextColor(ST77XX_WHITE);
      tft.println("Unknown selection");
      break;
  }
  drawNavbar();
}

void drawNavbar() {
  int y = tft.height() - 20;                          // Position the bar at the bottom of the screen
  tft.fillRect(0, y, tft.width(), 20, ST77XX_BLACK);  // Draw a black rectangle for the background

  // Draw dividing line
  tft.drawLine(0, 138, 127, 138, 0xA514);
  // Draw each icon in its place
  tft.drawBitmap(11, 142, arrow_left_i, 18, 15, ST77XX_WHITE);   // Left button icon
  tft.drawBitmap(41, 142, arrow_right_i, 18, 15, ST77XX_WHITE);  // Right button icon
  tft.drawBitmap(70, 142, select_i, 15, 16, ST77XX_WHITE);       // Select button icon
  tft.drawBitmap(100, 142, cancel_i, 15, 16, ST77XX_WHITE);      // Back button icon
}