

// EEPROM_SIZE 512
// struct confSecrets size: 471
// EEPROM start addr: 16 (0-15 used/reserved)
// + 2 bytes size information
// resulting used size: 489

bool saveConf_Secrets() {
  return saveConf_Secrets(false);
}
bool saveConf_Secrets(bool _force)
{
    if (confSecrets_wasChanged || _force)
    {
        #ifdef STORE_SECRETS_IN_EEPROM
        EEPROM.begin(EEPROM_SIZE);

        // write sizeof(confSecrets) to EEPROM in the first 2 bytes
        EEPROM.put(EEPROM_STARTADDR_SECRETS, (uint16_t)sizeof(confSecrets));
        #endif

        #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
            struct confDataSecrets
            {
                char WiFiPW1[64];
                char WiFiPW2[64];
                char WiFiAPModePassword[64];
                char http_token[31];
                char http_user[31];
                char http_pass[31];
                char http_user1[31];
                char http_pass1[31];
                char http_user2[31];
                char http_pass2[31];
                char mqtt_user[31];
                char mqtt_pass[31];
                // --- 471 bytes
                char spare[23];
                // --- 494 bytes + 2 bytes size information 
                // in EEPROM before the actual data => 512 bytes
            } _confSecrets_encrypted;

            if(confEncryptionEnabled) {
                // copy entire struct - the copy will be encrypted and saved
                memcpy(&_confSecrets_encrypted, &confSecrets, sizeof(confSecrets));

                XORENC(_confSecrets_encrypted.WiFiPW1, encKey);
                XORENC(_confSecrets_encrypted.WiFiPW2, encKey);
                XORENC(_confSecrets_encrypted.WiFiAPModePassword, encKey);
                XORENC(_confSecrets_encrypted.http_token, encKey);
                XORENC(_confSecrets_encrypted.http_user, encKey);
                XORENC(_confSecrets_encrypted.http_pass, encKey); 
                XORENC(_confSecrets_encrypted.http_user1, encKey);
                XORENC(_confSecrets_encrypted.http_pass1, encKey);
                XORENC(_confSecrets_encrypted.http_user2, encKey);
                XORENC(_confSecrets_encrypted.http_pass2, encKey);
                XORENC(_confSecrets_encrypted.mqtt_user, encKey); 
                XORENC(_confSecrets_encrypted.mqtt_pass, encKey); 
            }
        #endif

        #ifdef DEBUG_SECRETS_ENCRYPTION
        #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
            if(confEncryptionEnabled) {
                Serial.println(F("Secrets UNencrypted:"));
                Serial.print(F("WiFiPW1:    ")); Serial.println(confSecrets.WiFiPW1);
                Serial.print(F("WiFiPW2:    ")); Serial.println(confSecrets.WiFiPW2);
                Serial.print(F("WiFiAPPW:   ")); Serial.println(confSecrets.WiFiAPModePassword);
                Serial.print(F("http_token: ")); Serial.println(confSecrets.http_token);
                Serial.print(F("http_user:  ")); Serial.println(confSecrets.http_user);
                Serial.print(F("http_pass:  ")); Serial.println(confSecrets.http_pass);
                Serial.print(F("http_user1: ")); Serial.println(confSecrets.http_user1);
                Serial.print(F("http_pass1: ")); Serial.println(confSecrets.http_pass1);
                Serial.print(F("http_user2: ")); Serial.println(confSecrets.http_user2);
                Serial.print(F("http_pass2: ")); Serial.println(confSecrets.http_pass2);
                Serial.print(F("mqtt_user:  ")); Serial.println(confSecrets.mqtt_user);
                Serial.print(F("mqtt_pass:  ")); Serial.println(confSecrets.mqtt_pass);
                Serial.println(F("Secrets Encrypted:"));
                Serial.print(F("WiFiPW1:    ")); Serial.println(_confSecrets_encrypted.WiFiPW1);
                Serial.print(F("WiFiPW2:    ")); Serial.println(_confSecrets_encrypted.WiFiPW2);
                Serial.print(F("WiFiAPPW:   ")); Serial.println(_confSecrets_encrypted.WiFiAPModePassword);
                Serial.print(F("http_token: ")); Serial.println(_confSecrets_encrypted.http_token);
                Serial.print(F("http_user:  ")); Serial.println(_confSecrets_encrypted.http_user);
                Serial.print(F("http_pass:  ")); Serial.println(_confSecrets_encrypted.http_pass);
                Serial.print(F("http_user1: ")); Serial.println(_confSecrets_encrypted.http_user1);
                Serial.print(F("http_pass1: ")); Serial.println(_confSecrets_encrypted.http_pass1);
                Serial.print(F("http_user2: ")); Serial.println(_confSecrets_encrypted.http_user2);
                Serial.print(F("http_pass2: ")); Serial.println(_confSecrets_encrypted.http_pass2);
                Serial.print(F("mqtt_user:  ")); Serial.println(_confSecrets_encrypted.mqtt_user);
                Serial.print(F("mqtt_pass:  ")); Serial.println(_confSecrets_encrypted.mqtt_pass);
                Serial.println();
            }
        #endif
        #endif


        #ifdef STORE_SECRETS_IN_EEPROM
          #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
            // encryption on - put encrypted copy of confSecrets struct to EEPROM
            if(confEncryptionEnabled) {
              EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, _confSecrets_encrypted);
            }
            else {
              // encryption off - put complete origin confSecrets struct to EEPROM
              EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, confSecrets);  
            }
          #else
            // compiled without encryption support - put complete origin confSecrets struct to EEPROM
            EEPROM.put(EEPROM_STARTADDR_SECRETS + 2, confSecrets);
          #endif

          if(EEPROM.commit()) {
            snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_storedSecrets_EEPROM);
            sendLog(logBuf, LOGLEVEL_INFO);
            EEPROM.end();
            confSecrets_wasChanged = false;
            return true;
          }
          else { // error
            EEPROM.end();
            snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_loadConf_errorStoreSecrets_EEPROM);
            sendLog(logBuf, LOGLEVEL_INFO);
            return false;
          }

        #else // STORE_SECRETS_IN_EEPROM is not defined - use file on LittleFS

          char configFileName[] = "/confSecrets";
          File configFile = LittleFS.open(configFileName, "w");
          if (!configFile)
          {
              snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
              sendLog(logBuf, LOGLEVEL_INFO);
              return false;
          }
          #ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
            if(confEncryptionEnabled) {
              // encryption on - save encrypted copy of confSecrets struct to file
              configFile.write((byte*) &_confSecrets_encrypted, sizeof(_confSecrets_encrypted));
            }
            else {
              // encryption off - save complete origin confSecrets struct to file
              configFile.write((byte*) &confSecrets, sizeof(confSecrets));
            }
          #else
            // compiled without encryption support - save complete origin confSecrets struct to file
            configFile.write((byte*) &confSecrets, sizeof(confSecrets));
          #endif

          configFile.close();

          snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
          sendLog(logBuf, LOGLEVEL_INFO);

          confSecrets_wasChanged = false;
          return true;

        #endif
    }
    else return false;
} // saveConf_Secrets





bool saveConf_DevWiFi() {
  return saveConf_DevWiFi(false);
}
bool saveConf_DevWiFi(bool _force)
{
    if (confDevWiFi_wasChanged || _force)
    {
        char configFileName[] = "/confDevWiFi";
        File configFile = LittleFS.open(configFileName, "w");
        if (!configFile)
        {
            snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
            sendLog(logBuf, LOGLEVEL_INFO);
            return false;
        }
        configFile.write((byte*) &confDevWiFi, sizeof(confDevWiFi));
        configFile.close();
        
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);

        confDevWiFi_wasChanged = false;
        return true;
    }
    else return false;
} // saveConf_DevWiFi


bool saveConf_Web() {
  return saveConf_Web(false);
}
bool saveConf_Web(bool _force)
{
  if (confWeb_wasChanged || _force)
  {
    char configFileName[] = "/confWeb";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
      snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
      sendLog(logBuf, LOGLEVEL_INFO);
      return false;
    }
    configFile.write((byte*) &confWeb, sizeof(confWeb));
    configFile.close();
    
    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);
    
    confWeb_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Web


bool saveConf_Mqtt() {
  return saveConf_Mqtt(false);
}
bool saveConf_Mqtt(bool _force)
{
    if (confMqtt_wasChanged || _force)
    {
        char configFileName[] = "/confMqtt";
        File configFile = LittleFS.open(configFileName, "w");
        if (!configFile)
        {
            snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
            sendLog(logBuf, LOGLEVEL_INFO);
            return false;
        }
        configFile.write((byte*) &confMqtt, sizeof(confMqtt));
        configFile.close();

        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);

        confMqtt_wasChanged = false;
        return true;
    }
    else return false;
} // saveConf_Mqtt


#ifdef FIRMWARE_VARIANT_THERMOSTAT
bool saveConf_Therm() {
  return saveConf_Therm(false);
}
bool saveConf_Therm(bool _force)
{
  if (confTherm_wasChanged || _force)
  {
    char configFileName[] = "/confTherm";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);
        return false;
    }
    configFile.write((byte*) &confTherm, sizeof(confTherm));
    configFile.close();

    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);
    
    confTherm_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Therm
#endif

bool saveConf_Add() {
  return saveConf_Add(false);
}
bool saveConf_Add(bool _force)
{
  if (confAdd_wasChanged || _force)
  {
    char configFileName[] = "/confAdd";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);
        return false;
    }
    configFile.write((byte*) &confAdd, sizeof(confAdd));
    configFile.close();
    
    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);

    confAdd_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Add



#ifdef ENABLE_FEATURE_NTP_TIME
bool saveConf_Time() {
  return saveConf_Time(false);
}
bool saveConf_Time(bool _force)
{
  if (confTime_wasChanged || _force)
  {
    char configFileName[] = "/confTime";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);
        return false;
    }
    configFile.write((byte*) &confTime, sizeof(confTime));
    configFile.close();
    
    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);

    confTime_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Time
#endif


bool saveConf_Log() {
  return saveConf_Log(false);
}
bool saveConf_Log(bool _force)
{
  if (confLog_wasChanged || _force)
  {
    char configFileName[] = "/confLog";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);
        return false;
    }
    configFile.write((byte*) &confLog, sizeof(confLog));
    configFile.close();

    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);

    confLog_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Log


bool saveConf_Sens() {
  return saveConf_Sens(false);
}
bool saveConf_Sens(bool _force)
{
  if (confSens_wasChanged || _force)
  {
    char configFileName[] = "/confSens";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);
        return false;
    }
    configFile.write((byte*) &confSens, sizeof(confSens));
    configFile.close();
    
    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);

    confSens_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Sens



#ifdef FIRMWARE_VARIANT_HEATCONTROL
bool saveConf_Heatc() {
  return saveConf_Heatc(false);
}
bool saveConf_Heatc(bool _force)
{
  if (confHeatc_wasChanged || _force)
  {
    char configFileName[] = "/confHeatc";
    File configFile = LittleFS.open(configFileName, "w");
    if (!configFile)
    {
        snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWriteError, configFileName);
        sendLog(logBuf, LOGLEVEL_INFO);
        return false;
    }
    configFile.write((byte*) &confHeatc, sizeof(confHeatc));
    configFile.close();

    snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_safeConf_configFileWritten, configFileName);
    sendLog(logBuf, LOGLEVEL_INFO);
    
    confHeatc_wasChanged = false;
    return true;
  }
  else
    return false;
} // saveConf_Heatc
#endif
