/**
 * @file eeprom.h
 *
 * @brief Header file for configuration settings and EEPROM operations in the DE-Timer application.
 *
 * This file defines configuration settings for the DE-Timer project, including default values,
 * EEPROM structures, and functions for EEPROM operations. It also defines enums for different Battery Types.
 * Additionally, it includes functions for EEPROM handling such as storing, retrieving, and formatting configuration data.
 *
 * @author Marcel Peterkau
 * @date   09.01.2024
 */

#ifndef _EEPROM_H_
#define _EEPROM_H_

#include <Arduino.h>
#include <Wire.h>
#include <I2C_eeprom.h>
#include "dtc.h"
#include "common.h"

#define I2C_EE_ADDRESS 0x50
#define EEPROM_STRUCTURE_REVISION 4 // Increment this version when changing EEPROM structures
#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256

typedef enum Factions_e
{
  NONE,
  FACTION_1,
  FACTION_2,
  FACTION_3
} Factions_t;

typedef enum EERequest_e
{
  EE_IDLE,
  EE_CFG_SAVE,
  EE_CFG_LOAD,
  EE_CFG_FORMAT,
  EE_PDS_SAVE,
  EE_PDS_LOAD,
  EE_PDS_FORMAT,
  EE_FORMAT_ALL,
  EE_ALL_SAVE

} EERequest_t;

// Structure for persistence data stored in EEPROM
typedef struct
{
  uint32_t writeCycleCounter;
  uint32_t faction_1_timer;
  uint32_t faction_2_timer;
  uint32_t faction_3_timer;
  Factions_t activeFaction;
  uint32_t checksum;
} persistenceData_t;

// Structure for configuration settings stored in EEPROM
typedef struct
{
  uint8_t EEPROM_Version;
  batteryType_t batteryType;
  bool active_faction_on_reboot;
  char Faction_1_Name[33];
  char Faction_2_Name[33];
  char Faction_3_Name[33];
  char wifi_ap_ssid[33];
  char wifi_ap_password[64];
  char wifi_client_ssid[33];
  char wifi_client_password[64];
  bool wifi_autoconnect;
  uint32_t checksum;
} configData_t;

// Default configuration settings
const configData_t ConfigData_defaults = {
    2,               // EEPROM_Version (incerease this if anything on Layout changes!)
    BATTERY_LIPO_3S, // batteryType
    false,           // active_faction_on_reboot
    "FACTION 1",     // Faction_1_Name
    "FACTION 2",     // Faction_2_Name
    "FACTION 3",     // Faction_3_Name
    "ArisoftTimer",
    QUOTE(WIFI_AP_PASSWORD),
    QUOTE(WIFI_SSID_CLIENT),
    QUOTE(WIFI_PASSWORD_CLIENT),
    true,
    0 // checksum
};

boolean InitEEPROM();
void EEPROM_Process();
void StoreConfig_EEPROM();
void GetConfig_EEPROM();
void StorePersistence_EEPROM();
void GetPersistence_EEPROM();
void FormatConfig_EEPROM();
void FormatPersistence_EEPROM();
uint32_t Checksum_EEPROM(uint8_t const *data, size_t len);
void dumpEEPROM(uint16_t memoryAddress, uint16_t length);
void MovePersistencePage_EEPROM(boolean reset);
uint32_t ConfigSanityCheck(bool autocorrect = false);
bool validateWiFiString(char *string, size_t size);
void writeSequentialToEEPROM(uint16_t memoryAddress, uint16_t length);
void writeZeroToEEPROM(uint16_t memoryAddress, uint16_t length);

extern configData_t ConfigData;
extern persistenceData_t PersistenceData;
extern uint16_t eePersistenceMarker;
#endif // _CONFIG_H_
