// =========================================================
// OLED display - SSD1306 128x64 I2C
// Tilslutning:
//   OLED GND -> ESP32 GND
//   OLED VCC -> ESP32 3.3V
//   OLED SDA -> ESP32 GPIO32
//   OLED SCL -> ESP32 GPIO33
//
// Kræver biblioteker:
//   Adafruit SSD1306
//   Adafruit GFX
// =========================================================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

bool oledAvailable = false;
uint32_t lastOledUpdate = 0;
uint8_t oledPage = 0;

void oledPrintLine(int y, const String &text, uint8_t size = 1) {
  display.setTextSize(size);
  display.setCursor(0, y);
  display.println(text);
}

int countActiveLedsOnStrip(int s) {
  int count = 0;
  if (s < 0 || s >= MAX_STRIPS) return 0;
  for (int i = 0; i < ledCount[s]; i++) {
    if (ledState[s][i]) count++;
  }
  return count;
}

void setupOled() {
  Wire.begin(OLED_SDA_PIN, OLED_SCL_PIN);

  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
    oledAvailable = false;
    Serial.println("OLED ikke fundet");
    return;
  }

  oledAvailable = true;
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("DCC/MQTT");
  display.println("LysDekoder V20.0");
  display.println("OLED OK");
  display.println("SDA: GPIO32");
  display.println("SCL: GPIO33");
  display.display();
  Serial.println("OLED startet");
}

void oledShowMessage(const String &l1, const String &l2, const String &l3, const String &l4) {
  if (!oledAvailable) return;

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  oledPrintLine(0, l1, 1);
  oledPrintLine(16, l2, 1);
  oledPrintLine(28, l3, 1);
  oledPrintLine(40, l4, 1);
  display.display();
}

void drawOledPageSystem() {
  display.clearDisplay();
  oledPrintLine(0, "LysDekoder V20.0", 1);

  if (setupMode) {
    oledPrintLine(14, "SETUP MODE", 1);
    oledPrintLine(28, String("SSID: ") + SETUP_AP_SSID, 1);
    oledPrintLine(40, "IP: 192.168.4.1", 1);
    oledPrintLine(52, "Indtast WiFi", 1);
    display.display();
    return;
  }

  oledPrintLine(14, "Mode: " + inputModeName(), 1);

  if (WiFi.status() == WL_CONNECTED) {
    oledPrintLine(28, "WiFi: OK", 1);
    oledPrintLine(40, WiFi.localIP().toString(), 1);
    oledPrintLine(52, "RSSI: " + String(WiFi.RSSI()) + " dBm", 1);
  } else {
    oledPrintLine(28, "WiFi: FEJL", 1);
    oledPrintLine(40, "Ingen IP", 1);
  }

  display.display();
}

void drawOledPageMqtt() {
  display.clearDisplay();
  oledPrintLine(0, "MQTT status", 1);

  if (!mqttSettings.enabled) {
    oledPrintLine(18, "MQTT: Fra", 1);
  } else if (inputMode == 1) {
    oledPrintLine(18, "MQTT: Ikke i DCC", 1);
  } else if (setupMode) {
    oledPrintLine(18, "Setup Mode", 1);
    oledPrintLine(32, "LysDekoder-Setup", 1);
    oledPrintLine(46, "192.168.4.1", 1);
  } else if (WiFi.status() != WL_CONNECTED) {
    oledPrintLine(18, "WiFi mangler", 1);
  } else if (mqttClient.connected()) {
    oledPrintLine(18, "MQTT: OK", 1);
  } else {
    oledPrintLine(18, "MQTT: FEJL", 1);
    oledPrintLine(32, "State: " + String(lastMqttState), 1);
  }

  String broker = String(mqttSettings.broker);
  if (broker.length() > 18) broker = broker.substring(0, 18);
  oledPrintLine(50, broker, 1);
  display.display();
}

void drawOledPageLeds() {
  display.clearDisplay();
  oledPrintLine(0, "LED status", 1);

  int y = 14;
  for (int s = 0; s < MAX_STRIPS; s++) {
    String line = "Hus ";
    line += stripName(s);
    line += ": ";
    line += String(countActiveLedsOnStrip(s));
    line += "/";
    line += String(ledCount[s]);
    if (randomState[s].running) line += " R";
    oledPrintLine(y, line, 1);
    y += 10;
  }

  display.display();
}


void drawOledPageQr() {
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  if (setupMode) {
    oledPrintLine(0, "Setup WiFi", 1);
    oledPrintLine(16, "LysDekoder-Setup", 1);
    oledPrintLine(32, "http://192.168.4.1", 1);
  } else {
    oledPrintLine(0, "Web config", 1);
    oledPrintLine(16, String("http://") + getWifiHostname() + ".local", 1);
    oledPrintLine(32, WiFi.localIP().toString(), 1);
  }
  display.display();
}

void updateOled() {
  if (!oledAvailable) return;

  uint32_t now = millis();
  if (now - lastOledUpdate < 3000UL) return;
  lastOledUpdate = now;

  if (oledPage == 0) {
    drawOledPageSystem();
  } else if (oledPage == 1) {
    drawOledPageMqtt();
  } else if (oledPage == 2) {
    drawOledPageLeds();
  } else {
    drawOledPageQr();
  }

  oledPage++;
  if (oledPage > 3) oledPage = 0;
}
