#include <LedControl.h>

const int dataPin = 11;
const int clkPin = 13;
const int csPin = 10;

const int game1 = 5; 
const int game2 = 4;
const int game3 = 3; 
int currentGame = 0; 

int joyStickX = A1;
int joystickY = A0;
bool isClicked = false; 

int xLed = 0; // Initialize x-coordinate of LED cursor
int yLed = 0; // Initialize y-coordinate of LED cursor
bool cursorVisible = true; // Flag to toggle cursor visibility
bool paintMode = false; // Flag to indicate paint mode
bool hasExecuted1 = false; 

int numRows = 8; // Number of rows in LED matrix
int numCols = 8; // Number of columns in LED matrix
bool ledArray[8][8] = {
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false},
  {false, false, false, false, false, false, false, false}
};

LedControl lc = LedControl(dataPin, clkPin, csPin, 1);

void setup() {
  Serial.begin(9600);
  SetupLED(); 
  SetupPin();
}

void loop() {
  if (digitalRead(game1) == LOW) {
    //Activate Game 1
    delay(100);
    currentGame = 1; 
  } else if (digitalRead(game2) == LOW) {
    delay(100);
    currentGame = 2; 
  } else if (digitalRead(game3) == LOW) {
    delay(100);
    currentGame = 3; 
  }

  if (currentGame == 1) {
    LoadGame1();
  } else if (currentGame == 2) {
    LoadGame2();
  } else if (currentGame == 3) {
    LoadGame3();
  }
}

void SetupLED() {
  lc.shutdown(0, false);   
  lc.setIntensity(0, 8);   
  lc.clearDisplay(0);  
}

void LoadGame1() {
  if(hasExecuted1 == true){
    hasExecuted1 = false; 
  }
  Serial.println("GAME!");
  int xValue = analogRead(joyStickX);
  int yValue = analogRead(joystickY);

  float xNormalized = mapFloat(xValue, 0, 1023, -1.0, 1.0);
  float yNormalized = mapFloat(yValue, 0, 1023, -1.0, 1.0);

  // Check if the button is pressed
  bool buttonState = digitalRead(8); // Provide the correct pin for the button
  static bool prevButtonState = HIGH; // Define and initialize prevButtonState

  if (buttonState != prevButtonState) {
    if (buttonState == LOW) {
      // Button is pressed
      isClicked = true;
    } else {
      // Button is released
      if (!paintMode && isClicked) {
        // Toggle the cursor visibility
        cursorVisible = !cursorVisible;
      } else if (paintMode && isClicked) {
        // Toggle the LED in the ledArray at the current cursor position
        ledArray[yLed][xLed] = !ledArray[yLed][xLed];
      }
      isClicked = false; // Reset the click flag
    }
    prevButtonState = buttonState;
  }

  if (xNormalized < -0.5 && xLed > 0) {
    xLed--;
  } else if (xNormalized > 0.5 && xLed < 7) {
    xLed++;
  }

  if (yNormalized < -0.5 && yLed > 0) {
    yLed--;
  } else if (yNormalized > 0.5 && yLed < 7) {
    yLed++;
  }

  // Update the paint mode based on the button state
  if (buttonState == LOW) {
    paintMode = true;
  } else {
    paintMode = false;
  }

  // Update the LED matrix based on the ledArray values
  updateLedMatrix();

  delay(100);
}

void LoadGame2() {
  if(hasExecuted1 == false){
    lc.clearDisplay(0); 
    hasExecuted1 = true; 
  }
  lc.setLed(0, 4, 4, true);
  //
}

void LoadGame3() {
  if (currentGame != 3) {
    lc.clearDisplay(0);
  } 
  lc.setLed(0, 5, 4, true);
}

void SetupPin() {
  pinMode(game1, INPUT_PULLUP);
  pinMode(game2, INPUT_PULLUP);
  pinMode(game3, INPUT_PULLUP);
}

void updateLedMatrix() {
  lc.clearDisplay(0);
  for (int row = 0; row < numRows; row++) {
    for (int col = 0; col < numCols; col++) {
      if (row == yLed && col == xLed && cursorVisible) {
        lc.setLed(0, row, col, true);
      } else {
        lc.setLed(0, row, col, ledArray[row][col]);
      }
    }
  }
}

float mapFloat(float x, float inMin, float inMax, float outMin, float outMax) {
  return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
$abcdeabcde151015202530354045505560fghijfghij