/*
 * Mini Arcade — Kaihatsu Lab
 * ESP32 30 broches, OLED I2C, 7 boutons
 *
 * ATTENTION : GPIO12 (BTN_U10) est un strapping pin (MTDI).
 * Pull-up interne volontairement desactivee sur ce pin pour
 * ne pas risquer de tirer le niveau haut pendant le boot --
 * a verifier physiquement avant flash definitif.
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_SDA    22
#define OLED_SCL    21
#define SCREEN_W    128
#define SCREEN_H    64

#define BTN_U4      25
#define BTN_U5      32
#define BTN_U6      26
#define BTN_U7      14
#define BTN_U8      33
#define BTN_U9      27
#define BTN_U10     12

const int BUTTON_PINS[7] = {BTN_U4, BTN_U5, BTN_U6, BTN_U7, BTN_U8, BTN_U9, BTN_U10};
bool buttonState[7] = {false,false,false,false,false,false,false};
bool buttonLast[7]  = {false,false,false,false,false,false,false};

Adafruit_SSD1306 display(SCREEN_W, SCREEN_H, &Wire, -1);

const char* GAME_NAMES[] = {"Casse-briques", "Snake", "Shoot'em up"};
const int GAME_COUNT = 3;
int selectedGame = 0;
enum AppState { STATE_MENU, STATE_GAME };
AppState state = STATE_MENU;

void readButtons() {
  for (int i = 0; i < 7; i++) {
    buttonLast[i] = buttonState[i];
    buttonState[i] = (digitalRead(BUTTON_PINS[i]) == LOW);
  }
}

bool justPressed(int i) { return buttonState[i] && !buttonLast[i]; }

void drawMenu() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("== Mini Arcade ==");
  display.println("----------------");
  for (int i = 0; i < GAME_COUNT; i++) {
    display.setCursor(4, 16 + i * 12);
    if (i == selectedGame) display.print("> "); else display.print("  ");
    display.println(GAME_NAMES[i]);
  }
  display.setCursor(0, 56);
  display.println("U4/U5=nav U6=OK");
  display.display();
}

void drawGamePlaceholder() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(GAME_NAMES[selectedGame]);
  display.println("----------------");
  display.println("(jeu a implementer)");
  display.setCursor(0, 56);
  display.println("U10=retour menu");
  display.display();
}

void handleMenuInput() {
  if (justPressed(0)) { selectedGame = (selectedGame - 1 + GAME_COUNT) % GAME_COUNT; drawMenu(); }
  if (justPressed(1)) { selectedGame = (selectedGame + 1) % GAME_COUNT; drawMenu(); }
  if (justPressed(2)) { state = STATE_GAME; drawGamePlaceholder(); }
}

void handleGameInput() {
  if (justPressed(6)) { state = STATE_MENU; drawMenu(); }
}

void setup() {
  Serial.begin(115200);
  pinMode(BTN_U4, INPUT_PULLUP); pinMode(BTN_U5, INPUT_PULLUP);
  pinMode(BTN_U6, INPUT_PULLUP); pinMode(BTN_U7, INPUT_PULLUP);
  pinMode(BTN_U8, INPUT_PULLUP); pinMode(BTN_U9, INPUT_PULLUP);
  pinMode(BTN_U10, INPUT);
  Wire.begin(OLED_SDA, OLED_SCL);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) Serial.println("Erreur: OLED non detecte");
  drawMenu();
}

void loop() {
  readButtons();
  if (state == STATE_MENU) handleMenuInput(); else handleGameInput();
  delay(60);
}