#define NUM_LEDS_GROUP1 2
#define NUM_LEDS_GROUP2 8
#define NUM_LEDS_GROUP3 10

#define LED_PIN_GROUP1 22
#define LED_PIN_GROUP2 24
#define LED_PIN_GROUP3 32
const int LONG_PRESS_DURATION = 5000; // 5 seconds (in milliseconds)
unsigned long buttonPressStartTime = 0;

int oldSequence = 10;
bool done_playing = false;
int row = 16;
bool apressed = false;
bool pause_play = true;
bool playImmediate = false;
byte currentSelection = 1; // Tracks the current selection (1st, 2nd, 3rd)
const byte ROWS = 4;       // four rows
const byte COLS = 4;       // three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
const byte busyPin = 10;
const byte buttonPin = 8;
const byte pinLed = 5;
const byte ssRXPin = 11;
const byte ssTXPin = 12;
byte playIndex = 0;
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the
byte keyBufferIndex = 0;
byte colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the
bool cancel = false;

char key;
char keyBuffer[21];
bool newEntry = false;
byte track = 0;
byte trackList[3] = {0, 0, 0};
byte trackIndex = 0;
byte numTracks = 0;
byte mode = 0;
bool playList = false;
static bool lastBusyPinState = 0;
byte currentDisplayLine = 1;

#define MAX_SEQUENCE_LENGTH 10 // Maximum length of the sequence list
bool numAlpha = false;

int sequenceList[MAX_SEQUENCE_LENGTH]; // Array to store the sequence list
int sequenceLength = 0;                // Current length of the sequence list
// Function to add a track to the sequence list
bool selectionBlinkState = false;
unsigned long lastBlinkTime = 0;
unsigned long blinkInterval = 500; // Blink interval in milliseconds
bool trackBlinkState = false;
unsigned long lastTrackBlinkTime = 0;
unsigned long trackBlinkInterval = 500; // Blink interval in milliseconds
char collect1 = ' ';
char collect2 = ' ';
// Define the pin numbers for buttons
const int letterButtonPins[] = {15, 16, 17, 18, 19, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, A0, A1, A2}; // Pins for characters 'a' to 'v'
const int digitButtonPins[] = {A3, A4, A5, A6, A7, A8, A9, A10, A11, A12};                                       // Pins for digits '0' to '9'
int combine = 0;
bool checking = true;
bool buttonEntry = false;
String displayNum[10] = {};
int numCounter = 0;
bool lastState[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
void buttonPressed()
{
  for (int i = 0; i < sizeof(digitButtonPins) / sizeof(digitButtonPins[0]); i++)
  {
    if (digitalRead(digitButtonPins[i]) == LOW && lastState[i])
    {
      lastState[i] = false;

      Serial.println(" ");
      Serial.print(("button pressed :"));
      Serial.println(i);
      buttonPressStartTime = millis(); // Record button press start time
      delay((200));
      while (1)
      {
        if (digitalRead(digitButtonPins[i]))
        {
          Serial.println("Short press detected");
          handleShortPress(i);
          delay(400);
          break;
        }
        // Wait for button release or timeout
        else if (millis() - buttonPressStartTime >= LONG_PRESS_DURATION)
        {
          buttonPressStartTime = millis();
          Serial.println("Long press detected");
          handleLongPress();
          delay(800);
          break;
        }
      }
    }
    if (digitalRead(digitButtonPins[i]))
    {
      lastState[i] = true;
    }
  }
}

char mapPinToDigit(int pin)
{
  // Map digit button pin to corresponding character ('0' to '9')
  const char digits[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
  int index = pin - A3; // Calculate index based on pin number (A3 corresponds to '0')
  return digits[index];
}
void handleShortPress(int buttonIndex)
{
  delay(600); // Debounce delay
  char pressedChar = mapPinToDigit(digitButtonPins[buttonIndex]);
  Serial.print("Entered num: ");
  Serial.println(pressedChar);
  // getEntry(pressedChar);
}
void splitInteger(int number, char &hundreds, char &tens, char &units)
{
  units = (number % 10) + '0';
  tens = ((number / 10) % 10) + '0';
  hundreds = (number / 100) + '0';
}

void handleLongPress()
{
  // Perform actions for long press
  Serial.println("Long press detected!");
  int number = random(201); // Generates a random number between 0 and 200
  char hundreds, tens, units;
  splitInteger(number, hundreds, tens, units);

  // Print the results
  Serial.print("Hundreds: ");
  Serial.println(hundreds);
  Serial.print("Tens: ");
  Serial.println(tens);
  Serial.print("Units: ");
  Serial.println(units);
  // getEntry(units);
  //getEntry(tens);
  //getEntry(hundreds);
  //getEntry('*');
  // Add your code to handle the long press here
}
void setup() { // Set button pins as inputs with internal pull-up resistors
  Serial.begin(9600);
  Serial.println("started");
  for (int i = 0; i < sizeof(letterButtonPins) / sizeof(letterButtonPins[0]); i++)
  { Serial.println("started");
    pinMode(letterButtonPins[i], INPUT_PULLUP);
  }
  for (int i = 0; i < sizeof(digitButtonPins) / sizeof(digitButtonPins[0]); i++)
  { Serial.println("started");
    pinMode(digitButtonPins[i], INPUT_PULLUP);
  }
}
void loop () {
  buttonPressed();
}