#include <stdint.h>
#include <stddef.h>

#pragma pack(push, 1)
struct DeviceInfo {
    
    // The index where the measurement data starts in the eeprom storage
    uint16_t startIndex;

    // The time when the device was powered on (seconds since 1970)
    uint32_t deviceStartTime;
};

struct Measurement {

    uint16_t measurementIntervalsSinceDeviceStart;

    uint8_t temperature0;

    uint8_t temperature1;
};
#pragma pack(pop)

constexpr size_t eepromTotalSize = 13350;
constexpr uint16_t maxiumDeviceInfoCount = 10;
constexpr uint16_t dataStorageStartIndex = sizeof(DeviceInfo) * maxiumDeviceInfoCount;
constexpr uint16_t sizeOfMeasurement = sizeof(Measurement);

class EEPROMInterface {

public:

    bool isInitialized;

    /**
     * @brief Initialize the storage
     * 
     */
    EEPROMInterface();

    /**
     * @brief 
     * 
     */
   uint8_t startNewDeviceSession();

    /**
     * @brief Remove all stored data
     * 
     * @return true The data was deleted
     * @return false There was an error deleting the data
     */
    bool clearAllData();

    bool writeData(uint8_t* data, uint16_t count);

    /**
     * @brief Free space in the buffer 
     * 
     * @param buffer 
     * @param count 
     * @return uint16_t The number 
     */
    uint16_t readData(uint8_t* buffer, uint16_t count);

    uint16_t getStoredByteCount();

private:

    /**
     * @brief The index into the buffer where the next byte should be written
     * 
     */
    uint16_t writeIndex;

    /**
     * @brief The index into the buffer where the first byte is stored
     * 
     */
    uint16_t readIndex;

    /**
     * @brief Find the write index, the first zero byte after a non-zero byte
     * 
     * @return uint16_t The write index
     */
    uint16_t findEndIndex();

    uint16_t findStartIndex();
};