/**
 * @file config.h
 *
 * @brief Header file for configuration settings and EEPROM operations in the ChainLube application.
 *
 * This file defines configuration settings for the ChainLube project, including default values,
 * EEPROM structures, and functions for EEPROM operations. It also defines enums for different sources
 * of speed input, GPS baud rates, and CAN bus sources. Additionally, it includes functions for EEPROM handling
 * such as storing, retrieving, and formatting configuration data.
 *
 * @author Marcel Peterkau
 * @date   09.01.2024
 */

#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"

#if PCB_REV == 1 || PCB_REV == 2 || PCB_REV == 3
#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC64
#elif PCB_REV == 4
#define EEPROM_SIZE_BYTES I2C_DEVICESIZE_24LC256
#endif

// Enum for different sources of speed input
typedef enum SpeedSource_e
{
#ifdef FEATURE_ENABLE_TIMER
  SOURCE_TIME,
#endif
  SOURCE_IMPULSE,
  SOURCE_GPS,
  SOURCE_CAN
} SpeedSource_t;

// String representation of SpeedSource enum
const char SpeedSourceString[][8] = {
#ifdef FEATURE_ENABLE_TIMER
    "Timer",
#endif
    "Impuls",
    "GPS",
    "CAN-Bus"
};

const size_t SpeedSourceString_Elements = sizeof(SpeedSourceString) / sizeof(SpeedSourceString[0]);

// Enum for GPS baud rates
typedef enum GPSBaudRate_e
{
  BAUD_9600,
  BAUD_115200
} GPSBaudRate_t;

// String representation of GPSBaudRate enum
const char GPSBaudRateString[][7] = {
    "9600",
    "115200"};

const size_t GPSBaudRateString_Elements = sizeof(GPSBaudRateString) / sizeof(GPSBaudRateString[0]);

// Enum for CAN bus sources
typedef enum CANSource_e
{
  KTM_890_ADV_R_2021,
  KTM_1290_SD_R_2023
} CANSource_t;

// String representation of CANSource enum
const char CANSourceString[][30] = {
    "KTM 890 Adventure R (2021)",
    "KTM 1290 Superduke R (2023)"};

const size_t CANSourceString_Elements = sizeof(CANSourceString) / sizeof(CANSourceString[0]);

// Structure for persistence data stored in EEPROM
typedef struct
{
  uint16_t writeCycleCounter;
  uint32_t tankRemain_microL;
  uint32_t TravelDistance_highRes_mm;
  uint32_t odometer_mm;
  uint32_t odometer;
  uint32_t checksum;
} persistenceData_t;

// Structure for configuration settings stored in EEPROM
typedef struct
{
  uint8_t EEPROM_Version;
  uint32_t DistancePerLube_Default;
  uint32_t DistancePerLube_Rain;
  uint32_t tankCapacity_ml;
  uint32_t amountPerDose_microL;
  uint8_t TankRemindAtPercentage;
  uint8_t PulsePerRevolution;
  uint32_t TireWidth_mm;
  uint32_t TireWidthHeight_Ratio;
  uint32_t RimDiameter_Inch;
  uint32_t DistancePerRevolution_mm;
  uint16_t BleedingPulses;
  SpeedSource_t SpeedSource;
  GPSBaudRate_t GPSBaudRate;
  CANSource_t CANSource;
  bool LED_Mode_Flash;
  uint8_t LED_Max_Brightness;
  uint8_t LED_Min_Brightness;
  uint32_t checksum;
} LubeConfig_t;

// Default configuration settings
const LubeConfig_t LubeConfig_defaults = {
    0, 8000, 4000, 320, DEFAULT_PUMP_DOSE, 30, 1, 150, 70, 18, 2000, 25, SOURCE_IMPULSE,
    BAUD_115200,
    KTM_890_ADV_R_2021,
    false,
    255,
    5,
    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 LubeConfig_t LubeConfig;
extern persistenceData_t PersistenceData;
extern uint16_t eePersistenceMarker;
#endif // _CONFIG_H_
