#include "rm67162.h"
#include <TFT_eSPI.h>
#include <Arduino.h>
#include <RDA5807.h>
#include <Wire.h>
// I2C bus pin on ESP32
#define ESP32_I2C_SDA 43
#define ESP32_I2C_SCL 44
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft);

#define WIDTH 536
#define HEIGHT 240
#define DEBOUNCE_DELAY 50

// Button Definitions
#define BTN_UP 13
#define BTN_DOWN 11
#define BTN_LEFT 12
#define BTN_RIGHT 15
#define BTN_ENTER 14

// Button states
uint8_t buttonPins[5] = {BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, BTN_ENTER};
char buttonChars[5] = {'u', 'd', 'l', 'r', 'e'};
bool lastButtonState[5] = {HIGH, HIGH, HIGH, HIGH, HIGH};
unsigned long lastDebounceTime[5] = {0,0,0,0,0};

// Menu variables
int menuTopIndex = 0;
const int VISIBLE_MENU_ITEMS = 5;
int settingsPage = 0;
const int ITEMS_PER_PAGE = 4;
const int TOTAL_SETTINGS_PAGES = 2;

// UI Configuration
const uint16_t COLOR_BG = tft.color565(20, 25, 35);
const uint16_t COLOR_TEXT = tft.color565(240, 240, 240);
const uint16_t COLOR_ACCENT = tft.color565(0, 180, 255);
const uint16_t COLOR_CARD = tft.color565(40, 45, 60);
const uint16_t COLOR_SCALE = tft.color565(60, 70, 90);
const uint16_t COLOR_WARNING = tft.color565(255, 80, 80);
const uint16_t COLOR_HIGHLIGHT = tft.color565(255, 220, 100);
const uint16_t COLOR_SELECTED = tft.color565(100, 200, 255);

// Font definitions
#define TITLE_FONT &FreeSansBold12pt7b
#define LARGE_FONT &FreeSansBold24pt7b
#define VALUE_FONT &FreeSansBold18pt7b
#define LABEL_FONT &FreeSansBold9pt7b

// UI States
#define MAIN_SCREEN 0
#define SETTINGS_MENU 1
#define MANUAL_ENTRY 2
#define SEEK_VALUE 3

// Settings menu items
const char* settingsMenuItems[] = {
  "Manual Entry",
  "Seek",
  "Seek Value",
  "Restart",
  "Shutdown",
  "Mute",
  "Exit Settings"
};
const int menuItemCount = 7;

// System state
int currentState = MAIN_SCREEN;
int currentMenuItem = 0;
int manualEntryPosition = 0;
float seekStep = 0.5;
bool settingsActive = false;
RDA5807 radio;  // FM Radio module

// Radio data structure
struct RadioData {
  float frequency = 98.5;
  float volume = 75.0;
  bool muted = false;
  bool stereo = true;
  float signalStrength = 85.0;
  float batteryLevel = 65.0;
  bool charging = false;
  String stationName = "Classic FM";
};

RadioData radioData;

// Function prototypes
void syncRadioWithData();
void updateRadioData();
void handleInput(char input);
void showWelcomeScreen();
void drawRadioUI();
void drawMainScreen();
void drawTopBar();
void drawFrequencyDisplay();
void drawFMBandScale();
void drawVolumeArc();
void drawSpectrumAnalyzer();
void drawSettingsMenu();
void drawManualEntry();
void drawSeekValue();
void handleMainScreenInput(char input);
void handleSettingsMenuInput(char input);
void handleManualEntryInput(char input);
void handleSeekValueInput(char input);
void selectMenuItem();
void performSeek();
void updateDigit(int direction);

void syncRadioWithData() {
  radio.setFrequency(radioData.frequency * 100);
  radio.setVolume(radioData.volume * 15 / 100);
  radio.setMute(radioData.muted);
}

void updateRadioData() {
  radioData.frequency = radio.getFrequency() / 100.0;
  radioData.volume = map(radio.getVolume(), 0, 15, 0, 100);
  radioData.muted = radio.getMute();
  // radioData.stereo = radio.getStereo();
  // radioData.signalStrength = map(radio.getSignalLevel(), 0, 15, 0, 100);
}

void handleInput(char input) {
  Serial.print("Input: "); Serial.println(input);
  
  switch(currentState) {
    case MAIN_SCREEN:
      handleMainScreenInput(input);
      break;
      
    case SETTINGS_MENU:
      handleSettingsMenuInput(input);
      break;
      
    case MANUAL_ENTRY:
      handleManualEntryInput(input);
      break;
      
    case SEEK_VALUE:
      handleSeekValueInput(input);
      break;
  }
}

void setup() {
  Serial.begin(115200);
  menuTopIndex = 0;
  settingsPage = 0;
  currentMenuItem = 0;
  
  // Initialize buttons
  for (int i = 0; i < 5; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  
  //Initialize radio
  // Wire.begin(43, 44);  // SDA, SCL
   Wire.begin(ESP32_I2C_SDA, ESP32_I2C_SCL);
   radio.setup();
  // radio.setBand(RADIO_BAND_FM);
  radio.setFrequency(radioData.frequency * 100);
  radio.setVolume(abs(radioData.volume * 15 / 100));
  radio.setMute(radioData.muted);

  // Initialize display
  rm67162_init();
  lcd_setRotation(1);
  
  // Create sprite buffer
  spr.createSprite(WIDTH, HEIGHT);
  spr.setSwapBytes(true);

  // Initialize backlight
  pinMode(45, OUTPUT);
  digitalWrite(45, HIGH);

  Serial.println("FM Radio initialized");
  Serial.println("Controls: u=UP, d=DOWN, l=LEFT, r=RIGHT, e=ENTER");
  Serial.println("Main Screen: u/d=Volume, l/r=Frequency, e=Settings");
  
  showWelcomeScreen();
  delay(1000);
}

void loop() {
  // Handle physical buttons
  unsigned long currentTime = millis();
  for (int i = 0; i < 5; i++) {
    int reading = digitalRead(buttonPins[i]);
    
    if (reading != lastButtonState[i]) {
      lastDebounceTime[i] = currentTime;
    }
    
    if ((currentTime - lastDebounceTime[i]) > DEBOUNCE_DELAY) {
      if (reading == LOW) {
        handleInput(buttonChars[i]);
        delay(150);  // Button repeat delay
      }
    }
    lastButtonState[i] = reading;
  }
  
  // Handle serial input
  if (Serial.available() > 0) {
    char input = Serial.read();
    handleInput(input);
  }
  
  static unsigned long lastUpdate = 0;
  if (millis() - lastUpdate > 100) {
    lastUpdate = millis();
    updateRadioData();
    
    // Simulate battery changes
    if (radioData.charging) {
      radioData.batteryLevel += 0.2;
      if (radioData.batteryLevel > 100) radioData.charging = false;
    } else {
      radioData.batteryLevel -= 0.05;
      if (radioData.batteryLevel < 5) radioData.charging = true;
    }
  }

  drawRadioUI();
  delay(50);
}

void handleMainScreenInput(char input) {
  switch(input) {
    case 'u': // Volume up
      radioData.volume = min(radioData.volume + 1, 100.0f);
      Serial.print("Volume: ");
      Serial.println(radioData.volume);
      syncRadioWithData();
      break;
      
    case 'd': // Volume down
      radioData.volume = max(radioData.volume - 1, 0.0f);
      Serial.print("Volume: ");
      Serial.println(radioData.volume);
      syncRadioWithData();
      break;
      
    case 'r': // Frequency up
      radioData.frequency = min(radioData.frequency + 0.1f, 108.0f);
      Serial.print("Frequency: ");
      Serial.println(radioData.frequency);
      syncRadioWithData();
      break;
      
    case 'l': // Frequency down
      radioData.frequency = max(radioData.frequency - 0.1f, 88.0f);
      Serial.print("Frequency: ");
      Serial.println(radioData.frequency);
      syncRadioWithData();
      break;
      
    case 'e': // Enter settings
      currentState = SETTINGS_MENU;
      currentMenuItem = 0;
      settingsActive = true;
      Serial.println("Entering Settings Menu");
      break;
  }
}

void performSeek() {
  Serial.println("Seeking...");
  //radio.seekUp(true);
  delay(500);
  updateRadioData();
  Serial.print("Tuned to: ");
  Serial.println(radioData.frequency);
  currentState = MAIN_SCREEN;
}

void selectMenuItem() {
  switch(currentMenuItem) {
    case 0: // Manual Entry
      currentState = MANUAL_ENTRY;
      manualEntryPosition = 0;
      Serial.println("Manual Entry: Use l/r to select digit, u/d to change value");
      break;
      
    case 1: // Seek
      performSeek();
      break;
      
    case 2: // Seek Value
      currentState = SEEK_VALUE;
      Serial.print("Set Seek Step: ");
      Serial.println(seekStep);
      break;
      
    case 3: // Restart
      Serial.println("Restarting...");
      delay(1000);
      ESP.restart();
      break;
      
    case 4: // Shutdown
      Serial.println("Shutting down... Press RESET to wake");
      esp_deep_sleep_start();
      break;
      
    case 5: // Mute
      radioData.muted = !radioData.muted;
      Serial.println(radioData.muted ? "Muted" : "Unmuted");
      syncRadioWithData();
      break;
      
    case 6: // Exit Settings
      currentState = MAIN_SCREEN;
      Serial.println("Exiting Settings");
      break;
  }
}

void handleSettingsMenuInput(char input) {
  switch(input) {
    case 'u': // Navigate up
    case 'l': // Previous item
      currentMenuItem = (currentMenuItem - 1 + menuItemCount) % menuItemCount;
      Serial.print("Menu: ");
      Serial.println(settingsMenuItems[currentMenuItem]);
      break;
      
    case 'd': // Navigate down
    case 'r': // Next item
      currentMenuItem = (currentMenuItem + 1) % menuItemCount;
      Serial.print("Menu: ");
      Serial.println(settingsMenuItems[currentMenuItem]);
      break;
      
    case 'e': // Select item
      selectMenuItem();
      break;
  }
}

void handleManualEntryInput(char input) {
  int tens = (int)radioData.frequency / 10;
  int units = (int)radioData.frequency % 10;
  int tenths = (int)(radioData.frequency * 10) % 10;
  
  switch(input) {
    case 'l': // Move left
      manualEntryPosition = (manualEntryPosition - 1 + 3) % 3;
      Serial.print("Editing digit: ");
      Serial.println(manualEntryPosition);
      break;
      
    case 'r': // Move right
      manualEntryPosition = (manualEntryPosition + 1) % 3;
      Serial.print("Editing digit: ");
      Serial.println(manualEntryPosition);
      break;
      
    case 'u': // Increase digit
      updateDigit(1);
      syncRadioWithData();
      break;
      
    case 'd': // Decrease digit
      updateDigit(-1);
      syncRadioWithData();
      break;
      
    case 'e': // Exit manual entry
      currentState = SETTINGS_MENU;
      Serial.println("Exiting Manual Entry");
      break;
  }
}

void updateDigit(int direction) {
  int tens = (int)radioData.frequency / 10;
  int units = (int)radioData.frequency % 10;
  int tenths = (int)(radioData.frequency * 10) % 10;
  
  switch(manualEntryPosition) {
    case 0: // Tens
      tens = constrain(tens + direction, 8, 10);
      break;
    case 1: // Units
      if (tens == 10) {
        units = constrain(units + direction, 0, 8);
      } else {
        units = constrain(units + direction, 8, 9);
      }
      break;
    case 2: // Tenths
      tenths = constrain(tenths + direction, 0, 9);
      break;
  }
  
  radioData.frequency = tens * 10 + units + tenths / 10.0;
  radioData.frequency = constrain(radioData.frequency, 88.0, 108.0);
  Serial.print("Frequency set to: ");
  Serial.println(radioData.frequency);
}

void handleSeekValueInput(char input) {
  switch(input) {
    case 'u': // Increase seek step
      seekStep = min(seekStep + 0.1, 2.0);
      Serial.print("Seek Step: ");
      Serial.println(seekStep);
      break;
      
    case 'd': // Decrease seek step
      seekStep = max(seekStep - 0.1, 0.1);
      Serial.print("Seek Step: ");
      Serial.println(seekStep);
      break;
      
    case 'e': // Exit seek value
      currentState = SETTINGS_MENU;
      Serial.println("Exiting Seek Value");
      break;
      
    case 'l': // Exit without change
    case 'r':
      currentState = SETTINGS_MENU;
      Serial.println("Exiting Seek Value");
      break;
  }
}

void showWelcomeScreen() {
  spr.fillSprite(COLOR_BG);
  
  for (int y = 0; y < HEIGHT; y++) {
    uint16_t color = tft.color565(10, 15, 25 + y/15);
    spr.drawFastHLine(0, y, WIDTH, color);
  }
  
  spr.setFreeFont(LARGE_FONT);
  spr.setTextColor(COLOR_ACCENT);
  spr.setTextDatum(MC_DATUM);
  spr.drawString("FM RADIO", WIDTH/2, HEIGHT/2 - 30);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.drawString("Professional Receiver", WIDTH/2, HEIGHT/2 + 30);
  
  lcd_PushColors(0, 0, WIDTH, HEIGHT, (uint16_t *)spr.getPointer());
}

void drawRadioUI() {
  spr.fillSprite(COLOR_BG);

  if (currentState == MAIN_SCREEN) {
    drawMainScreen();
  } 
  else if (currentState == SETTINGS_MENU) {
    drawMainScreen();
    drawSettingsMenu();
  } 
  else if (currentState == MANUAL_ENTRY) {
    drawMainScreen();
    drawManualEntry();
  } 
  else if (currentState == SEEK_VALUE) {
    drawMainScreen();
    drawSeekValue();
  }

  lcd_PushColors(0, 0, WIDTH, HEIGHT, (uint16_t *)spr.getPointer());
}

void drawMainScreen() {
  drawTopBar();
  drawFrequencyDisplay();
  drawFMBandScale();
  drawVolumeArc();
  drawSpectrumAnalyzer();
}

void drawTopBar() {
  spr.fillRect(0, 0, WIDTH, 30, COLOR_CARD);
  
  spr.setFreeFont(TITLE_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.setTextDatum(TL_DATUM);
  spr.drawString(radioData.stationName, 170, 8);
  
  if (radioData.muted) {
    spr.fillRoundRect(10, 5, 60, 20, 5, COLOR_WARNING);
    spr.setTextColor(TFT_WHITE);
    spr.drawString("MUTE", 40, 8);
  }
 
  int batX = WIDTH - 50;
  int batY = 5;
  int batW = 40;
  
  spr.drawRect(batX, batY, batW, 20, COLOR_TEXT);
  spr.fillRect(batX + batW, batY + 5, 3, 10, COLOR_TEXT);
  
  uint16_t batColor;
  if (radioData.batteryLevel > 60) batColor = tft.color565(85, 200, 100);
  else if (radioData.batteryLevel > 30) batColor = tft.color565(50, 150, 255);
  else batColor = COLOR_WARNING;
  
  spr.fillRect(batX + 2, batY + 2, (batW - 4) * radioData.batteryLevel / 100, 16, batColor);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.drawString(String((int)radioData.batteryLevel) + "%", 430, batY + 2);
}

void drawFrequencyDisplay() {
  spr.setFreeFont(LARGE_FONT);
  spr.setTextColor(COLOR_ACCENT);
  spr.setTextDatum(MC_DATUM);
  
  char freqStr[10];
  sprintf(freqStr, "%.1f", radioData.frequency);
  spr.drawString(freqStr, WIDTH/2, 70);
  
  spr.setFreeFont(VALUE_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.drawString("MHz", WIDTH/2 + 100, 75);
}

void drawFMBandScale() {
  int startX = 20;
  int startY = 140;
  int width = WIDTH - 30;
  int height = 25;
  
  spr.fillRoundRect(startX, startY, width, height, 3, COLOR_SCALE);
  spr.drawRoundRect(startX, startY, width, height, 3, COLOR_ACCENT);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.setTextDatum(BC_DATUM);
  
  for (int f = 88; f <= 108; f++) {
    int markerX = startX + map(f, 88, 108, 0, width);
    
    if (f % 5 == 0) {
      spr.drawLine(markerX, startY, markerX, startY + height, COLOR_TEXT);
      spr.drawNumber(f, markerX, startY - 8);
    } 
    else {
      spr.drawLine(markerX, startY + height - 8, markerX, startY + height, tft.color565(180, 180, 200));
    }
  }
  
  int freqX = startX + map(radioData.frequency * 10, 880, 1080, 0, width);
  
  spr.drawLine(freqX, startY, freqX, startY + height, COLOR_WARNING);
  spr.drawLine(freqX-1, startY, freqX-1, startY + height, COLOR_WARNING);
  
  spr.fillTriangle(
    freqX, startY - 5,
    freqX - 6, startY,
    freqX + 6, startY,
    COLOR_WARNING
  );
  
  spr.fillTriangle(
    freqX, startY + height + 5,
    freqX - 6, startY + height,
    freqX + 6, startY + height,
    COLOR_WARNING
  );
  
  int signalWidth = width * radioData.signalStrength / 100;
  spr.fillRect(startX, startY + height - 3, signalWidth, 3, COLOR_ACCENT);
}

void drawVolumeArc() {
  int centerX = WIDTH - 480;
  int centerY = HEIGHT - 170;
  int radius = 35;
  int startAngle = 10;
  int endAngle = 390;
  
  spr.drawArc(centerX, centerY, radius, 25, startAngle, endAngle, COLOR_CARD, COLOR_BG);
  
  int filledAngle = map(radioData.volume, 0, 100, startAngle, endAngle);
  spr.drawArc(centerX, centerY, radius, 25, startAngle, filledAngle, COLOR_ACCENT, COLOR_BG);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.setTextDatum(BC_DATUM);
  spr.drawString("VOLUME", centerX+3, centerY + radius - 79);
  
  spr.setFreeFont(LABEL_FONT);
  spr.drawString(String((int)radioData.volume), centerX, centerY+8);
}

void drawSpectrumAnalyzer() {
  int startX = 20;
  int startY = HEIGHT - 60;
  int width = WIDTH - 30;
  int height = 55;
  
  spr.fillRoundRect(startX, startY, width, height, 5, COLOR_SCALE);
  spr.drawRoundRect(startX, startY, width, height, 5, COLOR_ACCENT);
  
  static uint8_t spectrum[24];
  for (int i = 0; i < 24; i++) {
    spectrum[i] = random(5, height - 10);
    if (i > 0) spectrum[i] = constrain(spectrum[i], spectrum[i-1] - 5, spectrum[i-1] + 5);
  }
  
  int barWidth = (width - 10) / 24;
  for (int i = 0; i < 24; i++) {
    int x = startX + 5 + i * barWidth;
    int barHeight = spectrum[i];
    
    uint8_t green = map(i, 0, 24, 100, 255);
    uint16_t color = tft.color565(0, green, 255);
    
    spr.fillRect(x, startY + height - barHeight, barWidth - 2, barHeight, color);
    spr.drawRect(x, startY + height - barHeight, barWidth - 2, barHeight, 
                tft.color565(150, 230, 255));
    
    spr.drawFastHLine(x, startY + height - barHeight, barWidth - 2, 
                     tft.color565(200, 240, 255));
  }
}

void drawSettingsMenu() {
  uint16_t overlayColor = tft.color565(0, 0, 0);
  for (int y = 0; y < HEIGHT; y += 2) {
    for (int x = y % 4; x < WIDTH; x += 4) {
      spr.drawPixel(x, y, overlayColor);
      spr.drawPixel(x+1, y, overlayColor);
      spr.drawPixel(x, y+1, overlayColor);
    }
  }
  
  int boxX = 40;
  int boxY = 40;
  int boxWidth = WIDTH - 80;
  int boxHeight = HEIGHT - 120;
  
  spr.fillRoundRect(boxX, boxY, boxWidth, boxHeight, 15, COLOR_CARD);
  spr.drawRoundRect(boxX, boxY, boxWidth, boxHeight, 15, COLOR_ACCENT);
  
  spr.setFreeFont(TITLE_FONT);
  spr.setTextColor(COLOR_ACCENT);
  spr.setTextDatum(TC_DATUM);
  spr.drawString("SETTINGS", WIDTH/2, boxY + 10);
  spr.drawLine(boxX + 20, boxY + 35, boxX + boxWidth - 20, boxY + 35, COLOR_ACCENT);
  
  spr.setFreeFont(TITLE_FONT);
  spr.setTextColor(COLOR_HIGHLIGHT);
  spr.drawString(settingsMenuItems[currentMenuItem], WIDTH/2, boxY + boxHeight/2 - 15);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  
  const char* description = "";
  switch(currentMenuItem) {
    case 0: description = "Set frequency digit-by-digit"; break;
    case 1: description = "Scan for next station"; break;
    case 2: description = "Set seek step size"; break;
    case 3: description = "Restart the device"; break;
    case 4: description = "Enter deep sleep mode"; break;
    case 5: description = "Toggle audio output"; break;
    case 6: description = "Return to main screen"; break;
  }
  spr.drawString(description, WIDTH/2, boxY + boxHeight/2 + 20);
  
  spr.fillRoundRect(boxX + 20, boxY + boxHeight - 40, boxWidth - 40, 35, 8, COLOR_CARD);
  spr.drawRoundRect(boxX + 20, boxY + boxHeight - 40, boxWidth - 40, 35, 8, COLOR_ACCENT);
  
  spr.fillTriangle(boxX + 40, boxY + boxHeight - 25, 
                  boxX + 60, boxY + boxHeight - 35, 
                  boxX + 60, boxY + boxHeight - 15, 
                  COLOR_ACCENT);
  
  spr.fillTriangle(boxX + boxWidth - 40, boxY + boxHeight - 25, 
                  boxX + boxWidth - 60, boxY + boxHeight - 35, 
                  boxX + boxWidth - 60, boxY + boxHeight - 15, 
                  COLOR_ACCENT);
  
  char itemStr[20];
  sprintf(itemStr, "%d/%d", currentMenuItem + 1, menuItemCount);
  spr.drawString(itemStr, WIDTH/2, boxY + boxHeight - 25);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.drawString("Press ENTER to select", WIDTH/2, boxY + boxHeight + 40);
}

void drawManualEntry() {
  uint16_t overlayColor = tft.color565(0, 0, 0);
  for (int y = 0; y < HEIGHT; y += 2) {
    for (int x = y % 4; x < WIDTH; x += 4) {
      spr.drawPixel(x, y, overlayColor);
      spr.drawPixel(x+1, y, overlayColor);
      spr.drawPixel(x, y+1, overlayColor);
    }
  }
  
  int boxX = 80;
  int boxY = 80;
  int boxWidth = WIDTH - 160;
  int boxHeight = 100;
  
  spr.fillRoundRect(boxX, boxY, boxWidth, boxHeight, 10, COLOR_CARD);
  spr.drawRoundRect(boxX, boxY, boxWidth, boxHeight, 10, COLOR_ACCENT);
  
  spr.setFreeFont(TITLE_FONT);
  spr.setTextColor(COLOR_ACCENT);
  spr.setTextDatum(TC_DATUM);
  spr.drawString("MANUAL FREQUENCY", WIDTH/2, boxY + 20);
  
  char freqStr[10];
  sprintf(freqStr, "%.1f", radioData.frequency);
  
  spr.setFreeFont(VALUE_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.setTextDatum(TC_DATUM);
  spr.drawString(freqStr, WIDTH/2, boxY + 50);
  
  int cursorX = WIDTH/2 - 35;
  cursorX += manualEntryPosition * 35;
  
  spr.drawLine(cursorX, boxY + 70, cursorX + 20, boxY + 70, COLOR_HIGHLIGHT);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.drawString("l/r: Move  u/d: Change  e: Exit", WIDTH/2, boxY + 85);
}

void drawSeekValue() {
  uint16_t overlayColor = tft.color565(0, 0, 0);
  for (int y = 0; y < HEIGHT; y += 2) {
    for (int x = y % 4; x < WIDTH; x += 4) {
      spr.drawPixel(x, y, overlayColor);
      spr.drawPixel(x+1, y, overlayColor);
      spr.drawPixel(x, y+1, overlayColor);
    }
  }
  
  int boxX = 100;
  int boxY = 100;
  int boxWidth = WIDTH - 200;
  int boxHeight = 80;
  
  spr.fillRoundRect(boxX, boxY, boxWidth, boxHeight, 10, COLOR_CARD);
  spr.drawRoundRect(boxX, boxY, boxWidth, boxHeight, 10, COLOR_ACCENT);
  
  spr.setFreeFont(TITLE_FONT);
  spr.setTextColor(COLOR_ACCENT);
  spr.setTextDatum(TC_DATUM);
  spr.drawString("SEEK STEP VALUE", WIDTH/2, boxY + 15);
  
  spr.setFreeFont(VALUE_FONT);
  spr.setTextColor(COLOR_HIGHLIGHT);
  spr.drawString(String(seekStep, 1) + " MHz", WIDTH/2, boxY + 45);
  
  spr.setFreeFont(LABEL_FONT);
  spr.setTextColor(COLOR_TEXT);
  spr.drawString("u/d: Change  e: Exit", WIDTH/2, boxY + 75);
}