#include "storage.h"
#include "config.h"
#include "game.h"
#include "touch.h"
#include <EEPROM.h>

void initStorage()
{
    EEPROM.begin(EEPROM_SIZE);
}

void saveCalibration()
{
    EEPROM.write(EEPROM_CAL, CAL_MAGIC);
    EEPROM.put(EEPROM_CAL + 1, tchXMin);
    EEPROM.put(EEPROM_CAL + 3, tchXMax);
    EEPROM.put(EEPROM_CAL + 5, tchYMin);
    EEPROM.put(EEPROM_CAL + 7, tchYMax);
    EEPROM.commit();
}

void loadCalibration()
{
    if (EEPROM.read(EEPROM_CAL) != CAL_MAGIC)
        return;
    EEPROM.get(EEPROM_CAL + 1, tchXMin);
    EEPROM.get(EEPROM_CAL + 3, tchXMax);
    EEPROM.get(EEPROM_CAL + 5, tchYMin);
    EEPROM.get(EEPROM_CAL + 7, tchYMax);
}

void saveSettings()
{
    EEPROM.write(EEPROM_SETTINGS, SETTINGS_MAGIC);
    EEPROM.write(EEPROM_SETTINGS + 1, currentLookAhead);
    EEPROM.write(EEPROM_SETTINGS + 2, blunderEnabled ? 1 : 0);
    EEPROM.write(EEPROM_SETTINGS + 3, blunderChance);
    EEPROM.commit();
}

void loadSettings()
{
    if (EEPROM.read(EEPROM_SETTINGS) != SETTINGS_MAGIC)
        return;
    currentLookAhead = EEPROM.read(EEPROM_SETTINGS + 1);
    blunderEnabled = EEPROM.read(EEPROM_SETTINGS + 2) != 0;
    blunderChance = EEPROM.read(EEPROM_SETTINGS + 3);
}

void saveGameLog()
{
    EEPROM.write(EEPROM_SETTINGS + 4, gameLogCount);
    for (int i = 0; i < gameLogCount; i++)
    {
        int off = EEPROM_LOG + i * GAME_ENTRY_SIZE;
        EEPROM.write(off, gameLog[i].type);
        EEPROM.write(off + 1, gameLog[i].level);
        EEPROM.write(off + 2, gameLog[i].winner);
        uint8_t len = min((int)gameLog[i].moves.length(), 42);
        EEPROM.write(off + 3, len);
        for (int j = 0; j < len; j++)
            EEPROM.write(off + 4 + j, gameLog[i].moves[j]);
    }
    EEPROM.commit();
}

void loadGameLog()
{
    gameLogCount = EEPROM.read(EEPROM_SETTINGS + 4);
    if (gameLogCount > MAX_GAME_LOG)
        gameLogCount = 0;
    for (int i = 0; i < gameLogCount; i++)
    {
        int off = EEPROM_LOG + i * GAME_ENTRY_SIZE;
        gameLog[i].type = EEPROM.read(off);
        gameLog[i].level = EEPROM.read(off + 1);
        gameLog[i].winner = EEPROM.read(off + 2);
        uint8_t len = EEPROM.read(off + 3);
        if (len > 42)
        {
            gameLogCount = i;
            break;
        }
        gameLog[i].moves = "";
        for (int j = 0; j < len; j++)
            gameLog[i].moves += (char)EEPROM.read(off + 4 + j);
    }
}

void logGame(int8_t winner)
{
    char type = (gameMenuMode == 0) ? 'Y' : (gameMenuMode == 1) ? 'R'
                                                                : '2';
    char winChar = (winner == 1) ? 'Y' : (winner == 2) ? 'R'
                                                       : 'D';
    GameEntry entry = {type, gameLevel, winChar, currentMoves};
    if (gameLogCount < MAX_GAME_LOG)
    {
        gameLog[gameLogCount++] = entry;
    }
    else
    {
        for (int i = 0; i < MAX_GAME_LOG - 1; i++)
            gameLog[i] = gameLog[i + 1];
        gameLog[MAX_GAME_LOG - 1] = entry;
    }
    saveGameLog();
}

void clearGameLog()
{
    gameLogCount = 0;
    saveGameLog();
}
