#ifndef _CONFIG_H_
#define _CONFIG_H_

#include <Arduino.h>
#include <Wire.h>
#include <I2C_eeprom.h>

#include "globals.h"
#include "dtc.h"
#include "common.h"

#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC64
#define EEPROM_ENDURANCE 1000000

typedef enum
{
  NONE,
  FACTION_1,
  FACTION_2,
  FACTION_3
} factions_t;

typedef struct
{
  uint32_t writeCycleCounter = 0;
  uint32_t faction_1_timer = 0;
  uint32_t faction_2_timer = 0;
  uint32_t faction_3_timer = 0;
  factions_t activeFaction = NONE;
  uint32_t checksum = 0;
} persistenceData_t;

typedef enum
{
  BATTERY_UNDEFINED,
  BATTERY_LIPO_2S,
  BATTERY_LIPO_3S
} batteryType_t;

const char BatteryString[][10]{
  "Undefined",
  "LiPo 2S",
  "LiPo 3S"
};

const size_t BatteryString_Elements = sizeof(BatteryString) / sizeof(BatteryString[0]);

typedef struct
{
  uint8_t EEPROM_Version = 1;
  batteryType_t batteryType = BATTERY_UNDEFINED;
  uint32_t checksum = 0;
} configData_t;

const configData_t ConfigData_defaults = {
 0, BATTERY_LIPO_3S, 0
};

void 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);

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