#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

const int rows[4] = { 6, 7, 8, 9 };         // Digital pins for rows
const int columns[4] = { A0, A1, A2, A3 };  // Analog pins for columns

int previousPotValue = 0;  // Variable to hold previous pot value
int ldrThreshold = 0;      // Variable to hold LDR threshold value

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
// #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
// Defining size, and output pins
#define MAX_DEVICES 4
#define CS_PIN 3

// Create a new instance of the MD_Parola class with hardware SPI connection
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);


#define CLK_PIN 13
#define DATA_PIN 11

#define pushButtonPin 2
#define ledPin 4
#define potentiometerPin A4
#define inactivityTimeout 120000  // 2 minutes standby

const int ldrPins[] = { A0, A1, A2, A3 };
const int ldrThresholdDefault = 75;  // Default LDR sensitivity threshold


unsigned long startTime, endTime, reactionTime;
unsigned long recentScores[5];
unsigned long lastActivity = 0;
int scoreCount = 0;
bool gameActive = false;
bool showHighScores = false;


String sc1, sc2, sc3, sc4, sc5;  // Variables for recent scores in string form

bool checkLdrArray() {
  int ldrValues[4][4];  // Array to store LDR readings

  // Iterate through each row
  for (int i = 0; i < 4; i++) {
    // Activate the current row
    digitalWrite(rows[i], HIGH);
    Serial.print("Row ");
    Serial.print(i + 1);

    // Read all columns for the current row
    for (int j = 0; j < 4; j++) {
      ldrValues[i][j] = map(analogRead(columns[j]), 0, 1023, 0, 100) > ldrThreshold ? 1 : 0;
      if (ldrValues[i][j] == 1) {
        for (int i = 0; i < 4; i++) {  // reset pins low
          digitalWrite(rows[i], LOW);
          digitalWrite(columns[j], LOW);
        }
        return true;
      }
      Serial.print(", Col ");
      Serial.print(j + 1);
      Serial.print(": ");
      Serial.print(ldrValues[i][j]);
    }
    Serial.println();


    // Deactivate the current row
    digitalWrite(rows[i], LOW);
  }
  return false;
}

void standbyMode() {
  myDisplay.displayClear();
  myDisplay.print("L.E.D. shooter");
  Serial.println("L.E.D. Shooter");
}

void startGame() {
  for (int round = 1; round <= 5; round++) {
    myDisplay.print("READY");
    Serial.println("Get Ready");
    delay(random(5000, 10000));

    digitalWrite(ledPin, HIGH);
    myDisplay.print("FIRE");
    Serial.println("Fire");
    startTime = millis();

    bool hit = false;
    while (millis() - startTime <= 5000) {
      checkForPotentiometerChange();  // keep checking if potentiometer value changes
      if (checkLdrArray()) {
        hit = true;
        break;
      }
    }

    digitalWrite(ledPin, LOW);

    if (hit) {
      endTime = millis();
      for (int i = 0; i < 3; i++) {
        digitalWrite(ledPin, HIGH);
        delay(250);
        digitalWrite(ledPin, LOW);
        delay(250);
      }
      reactionTime = endTime - startTime;
      Serial.print("Reaction Time: ");
      Serial.print(reactionTime);
      Serial.println(" ms");
      addScore(reactionTime);
      myDisplay.setTextAlignment(PA_RIGHT);
      myDisplay.print(String(reactionTime));
      delay(3000);
      updateScoreStrings();
    }
    delay(2000);
  }
}

void setup() {
  pinMode(pushButtonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // Intialize the object
  myDisplay.begin();

  // Set the intensity (brightness) of the display (0-15)
  myDisplay.setIntensity(0);

  // Clear the display
  myDisplay.displayClear();

  // Initialize LDR Array pins
  // Initialize row pins as outputs
  for (int i = 0; i < 4; i++) {
    pinMode(rows[i], OUTPUT);
    digitalWrite(rows[i], LOW);
  }

  // Initialize column pins as inputs
  for (int i = 0; i < 4; i++) {
    pinMode(columns[i], INPUT);
  }

  myDisplay.begin();
  myDisplay.setTextAlignment(PA_CENTER);
  myDisplay.print("Hello");
  delay(2000);
  myDisplay.setTextAlignment(PA_CENTER);
  myDisplay.print("Start...");


  previousPotValue = map(analogRead(potentiometerPin), 0, 1023, 0, 100);  // Read potentiometer mapped value
  ldrThreshold = previousPotValue;                                        // Set LDR threshold to potentiometer value
  resetScores();
  lastActivity = millis();
  Serial.println("Start...");
}

void checkForPotentiometerChange() {
  int potValue = map(analogRead(potentiometerPin), 0, 1023, 0, 100);
  if (abs(potValue - previousPotValue) > 2) {
    previousPotValue = potValue;
    ldrThreshold = previousPotValue;
    String sensitivityMsg = "Sensitivity: " + String(ldrThreshold);
    myDisplay.displayClear();
    myDisplay.print(ldrThreshold);
    Serial.print("Threshold:");
    Serial.println(ldrThreshold);
    delay(1000);
  }
}
void loop() {
  unsigned long currentMillis = millis();
  checkForPotentiometerChange();

  // Check button press
  if (digitalRead(pushButtonPin) == LOW) {
    Serial.println("Button Pressed");
    delay(50);  // Debounce
    unsigned long pressStart = millis();
    while (digitalRead(pushButtonPin) == LOW)
      ;
    unsigned long pressDuration = millis() - pressStart;

    if (pressDuration >= 3000) {  // Long press
      showHighScores = true;
      gameActive = false;
      myDisplay.displayClear();
      // displayHighScores();
    } else {  // Short press
      gameActive = true;
      showHighScores = false;
      myDisplay.displayClear();
      startGame();
      // Step 5: Print recent scores sorted in descending order
      printScores();
      DisplayDot();
    }
    lastActivity = currentMillis;
  }

  // Standby mode after inactivity
  if (currentMillis - lastActivity > inactivityTimeout && !gameActive && !showHighScores) {
    standbyMode();
  }
}


void resetScores() {
  for (int i = 0; i < 5; i++) {
    recentScores[i] = 0;
  }
  updateScoreStrings();
}



void addScore(unsigned long newScore) {
  if (scoreCount < 5) {
    recentScores[scoreCount++] = newScore;
  } else {
    for (int i = 1; i < 5; i++) {
      recentScores[i - 1] = recentScores[i];
    }
    recentScores[4] = newScore;
  }
  sortScores();
  updateScoreStrings();
}



void sortScores() {
  for (int i = 0; i < scoreCount - 1; i++) {
    for (int j = i + 1; j < scoreCount; j++) {
      if (recentScores[i] > recentScores[j]) {  // Sort descending
        unsigned long temp = recentScores[i];
        recentScores[i] = recentScores[j];
        recentScores[j] = temp;
      }
    }
  }
}



void printScores() {
  Serial.println("Recent Scores:");
  for (int i = 0; i < scoreCount; i++) {
    Serial.print(i + 1);
    Serial.print(". ");
    Serial.print(recentScores[i]);
    Serial.println(" ms");
  }
}



void updateScoreStrings() {
  sc1 = (scoreCount > 0) ? String(recentScores[0]) : "";  //+ " ms" : "N/A";
  sc2 = (scoreCount > 1) ? String(recentScores[1]) : "";  //+ " ms" : "N/A";
  sc3 = (scoreCount > 2) ? String(recentScores[2]) : "";  //+ " ms" : "N/A";
  sc4 = (scoreCount > 3) ? String(recentScores[3]) : "";  //+ " ms" : "N/A";
  sc5 = (scoreCount > 4) ? String(recentScores[4]) : "";  //+ " ms" : "N/A";
}
void DisplayDot() {
  myDisplay.displayClear();
  String ST = " 1st " + sc1 + " 2nd " + sc2 + " 3rd " + sc3 + " 4th " + sc4 + " 5th " + sc5;



  // Convert String to char array
  char scrollText[ST.length() + 1];
  ST.toCharArray(scrollText, ST.length() + 1);



  // Set the scrolling parameters
  myDisplay.displayScroll(scrollText, PA_CENTER, PA_SCROLL_LEFT, 100);



  // Animate the scroll until complete
  while (!myDisplay.displayAnimate()) {
    // Continuously update the animation
  }
}