bool loadConfig() {
  prefs.begin("dcc-led", true);

  uint32_t version = prefs.getUInt("version", 0);
  if (version != CONFIG_VERSION) {
    prefs.end();
    return false;
  }

  size_t readConfigBytes = prefs.getBytes("ledconfig", config, sizeof(config));
  size_t readCountBytes  = prefs.getBytes("ledcount", ledCount, sizeof(ledCount));
  size_t readRandomBytes = prefs.getBytes("rndcfg", randomConfig, sizeof(randomConfig));
  randomDayMinutes = prefs.getUShort("rndDay", 2);
  randomNightMinutes = prefs.getUShort("rndNight", 5);
  inputMode = prefs.getUChar("inputMode", 1);
  language = prefs.getUChar("language", 0);
  size_t readMqttBytes = prefs.getBytes("mqtt", &mqttSettings, sizeof(mqttSettings));
  prefs.end();

  if (readConfigBytes != sizeof(config)) return false;
  if (readCountBytes != sizeof(ledCount)) return false;
  if (readRandomBytes != sizeof(randomConfig)) return false;
  if (readMqttBytes != sizeof(mqttSettings)) return false;
  if (inputMode < 1 || inputMode > 3) inputMode = 1;
  if (language > 2) language = 0;
  if (mqttSettings.port == 0) mqttSettings.port = mqttSettings.useTls ? 8883 : 1883;

  for (int s = 0; s < MAX_STRIPS; s++) {
    if (ledCount[s] > MAX_LEDS_PER_STRIP) {
      ledCount[s] = MAX_LEDS_PER_STRIP;
    }
  }

  return true;
}

void saveConfig() {
  prefs.begin("dcc-led", false);
  prefs.putUInt("version", CONFIG_VERSION);
  prefs.putBytes("ledconfig", config, sizeof(config));
  prefs.putBytes("ledcount", ledCount, sizeof(ledCount));
  prefs.putBytes("rndcfg", randomConfig, sizeof(randomConfig));
  prefs.putUShort("rndDay", randomDayMinutes);
  prefs.putUShort("rndNight", randomNightMinutes);
  prefs.putUChar("inputMode", inputMode);
  prefs.putUChar("language", language);
  prefs.putBytes("mqtt", &mqttSettings, sizeof(mqttSettings));
  prefs.end();

  Serial.println("Config gemt i flash.");
}

void setDefaultConfig() {
  // Standard: A og B aktive med 10 LED'er. C, D og E er klar, men inaktive.
  ledCount[0] = 10;
  ledCount[1] = 10;
  ledCount[2] = 0;
  ledCount[3] = 0;
  ledCount[4] = 0;

  randomDayMinutes = 2;
  randomNightMinutes = 5;
  inputMode = 1;
  language = 0;

  mqttSettings.enabled = false;
  strncpy(mqttSettings.broker, "broker.hivemq.com", sizeof(mqttSettings.broker));
  mqttSettings.broker[sizeof(mqttSettings.broker) - 1] = 0;
  mqttSettings.port = 1883;
  mqttSettings.user[0] = 0;
  mqttSettings.password[0] = 0;
  mqttSettings.useTls = false;
  mqttSettings.tlsInsecure = true;
  strncpy(mqttSettings.baseTopic, "WDP/DCCLysDekoder", sizeof(mqttSettings.baseTopic));
  mqttSettings.baseTopic[sizeof(mqttSettings.baseTopic) - 1] = 0;

  for (int s = 0; s < MAX_STRIPS; s++) {
    randomConfig[s].dccAddress = 200 + s;
    for (int i = 0; i < MAX_LEDS_PER_STRIP; i++) {
      config[s][i].dccAddress = 101 + globalLedIndex(s, i);
      config[s][i].mode = 1;
      config[s][i].brightness = 100;
    }
  }
}

void printConfig() {
  Serial.println();
  Serial.println("Aktuel config:");
  Serial.println("----------------------");

  Serial.print("Input mode: ");
  Serial.println(inputMode == 1 ? "DCC" : (inputMode == 2 ? "MQTT" : "DCC + MQTT"));
  Serial.print("MQTT: ");
  Serial.print(mqttSettings.enabled ? "aktiv" : "inaktiv");
  Serial.print("  broker=");
  Serial.print(mqttSettings.broker);
  Serial.print(":");
  Serial.print(mqttSettings.port);
  Serial.print("  TLS=");
  Serial.print(mqttSettings.useTls ? "ja" : "nej");
  if (mqttSettings.useTls) {
    Serial.print("  cert=");
    Serial.print(mqttSettings.tlsInsecure ? "uden validering" : "standard validering");
  }
  Serial.print("  baseTopic=");
  Serial.println(mqttSettings.baseTopic);

  Serial.print("Random dag/min: ");
  Serial.print(randomDayMinutes);
  Serial.print("  nat/min: ");
  Serial.println(randomNightMinutes);

  for (int s = 0; s < MAX_STRIPS; s++) {
    Serial.print("Baand ");
    Serial.print(stripName(s));
    Serial.print("  GPIO=");
    Serial.print(stripPins[s]);
    Serial.print("  LED'er=");
    Serial.print(ledCount[s]);
    Serial.print("  Random DCC=");
    Serial.println(randomConfig[s].dccAddress);

    for (int i = 0; i < ledCount[s]; i++) {
      Serial.print("  Global LED ");
      Serial.print(globalLedIndex(s, i) + 1);
      Serial.print("  Baand=");
      Serial.print(stripName(s));
      Serial.print("  Nr=");
      Serial.print(i + 1);
      Serial.print("  DCC=");
      Serial.print(config[s][i].dccAddress);
      Serial.print("  Mode=");
      Serial.print(config[s][i].mode);
      Serial.print(" (");
      Serial.print(modeName(config[s][i].mode));
      Serial.print(")");
      Serial.print("  Brightness=");
      Serial.println(config[s][i].brightness);
    }
  }

  Serial.println("----------------------");
}
