#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;

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

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;
  }
}

void displayHighScores() {
  String scores = "Scores: ";
  for (int i = 0; i < scoreCount; i++) {
    scores += String(recentScores[i]) + "ms ";
  }
  myDisplay.displayScroll(scores.c_str(), PA_CENTER, PA_SCROLL_LEFT, 100);
  delay(60000);  // Display for 1 minute
}

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();
      reactionTime = endTime - startTime;
      addScore(reactionTime);
      myDisplay.print(String(reactionTime) + "ms");
      Serial.print("Hit Time:");
      Serial.println(String(reactionTime) + "ms");
    } else {
      myDisplay.print("MISS");
      Serial.println("Miss");
    }
    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 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 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
  }
}