/** \file
 *  \brief This file contains functions that implement the settings interface for SystemK.
 *
 */

/* Include Files */
#include "KTag.h"


SystemKResult_T SETTINGS_get_uint8_t(SystemKSettingID_T id, uint8_t * value)
{
    SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
    
    switch (id)
    {
        case SYSTEMK_SETTING_IS_RIGHT_HANDED:
            *value = NVM_IS_RIGHT_HANDED;
            break;
        
        case SYSTEMK_SETTING_AUDIO_VOLUME:
            *value = NVM_VOLUME;
            break;
        
        case SYSTEMK_SETTING_TEAMID:
            *value = NVM_TEAM_ID;
            break;
        
        case SYSTEMK_SETTING_PLAYERID:
            *value = NVM_PLAYER_ID;
            break;
        
        case SYSTEMK_SETTING_WEAPONID:
            *value = NVM_WEAPON_ID;
            break;
        
        case SYSTEMK_SETTING_MAX_HEALTH:
            *value = NVM_MAX_HEALTH;
            break;
        
        case SYSTEMK_SETTING_N_SPECIAL_WEAPONS_ON_REENTRY:
            *value = NVM_N_SPECIAL_WEAPONS_ON_REENTRY;
            break;
        
        default:
            result = SYSTEMK_RESULT_WRONG_DATATYPE;
            break;
    }
    
    return result;
}

SystemKResult_T SETTINGS_set_uint8_t(SystemKSettingID_T id, uint8_t value)
{
    SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
    
    switch (id)
    {
        case SYSTEMK_SETTING_IS_RIGHT_HANDED:
            NVM_IS_RIGHT_HANDED = value;
            break;
        
        case SYSTEMK_SETTING_AUDIO_VOLUME:
            NVM_VOLUME = value;
            break;
        
        case SYSTEMK_SETTING_TEAMID:
            NVM_TEAM_ID = value;
            break;
        
        case SYSTEMK_SETTING_PLAYERID:
            NVM_PLAYER_ID = value;
            break;
        
        case SYSTEMK_SETTING_WEAPONID:
            NVM_WEAPON_ID = value;
            break;
        
        case SYSTEMK_SETTING_MAX_HEALTH:
            NVM_MAX_HEALTH = value;
            break;
        
        case SYSTEMK_SETTING_N_SPECIAL_WEAPONS_ON_REENTRY:
            NVM_N_SPECIAL_WEAPONS_ON_REENTRY = value;
            break;
        
        default:
            result = SYSTEMK_RESULT_WRONG_DATATYPE;
            break;
    }
    
    return result;
}

SystemKResult_T SETTINGS_get_uint32_t(SystemKSettingID_T id, uint32_t * value)
{
    SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
    
    switch (id)
    {
        case SYSTEMK_SETTING_DEVICE_TYPE:
            *value = 1; // 2020TPC
            break;
        
        case SYSTEMK_SETTING_T_START_GAME_in_ms:
            *value = NVM_T_START_GAME_IN_MS;
            break;
        
        case SYSTEMK_SETTING_T_GAME_LENGTH_in_ms:
            *value = NVM_T_GAME_LENGTH_IN_MS;
            break;
        
        case SYSTEMK_SETTING_SECONDARY_COLOR:
            *value = NVM_SECONDARY_COLOR;
            break;
        
        default:
            result = SYSTEMK_RESULT_WRONG_DATATYPE;
            break;
    }
    
    return result;
}

SystemKResult_T SETTINGS_set_uint32_t(SystemKSettingID_T id, uint32_t value)
{
    SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
    
    switch (id)
    {
        case SYSTEMK_SETTING_DEVICE_TYPE:
            result = SYSTEMK_RESULT_NOT_IMPLEMENTED;
            break;

        case SYSTEMK_SETTING_T_START_GAME_in_ms:
            NVM_T_START_GAME_IN_MS = value;
            break;
        
        case SYSTEMK_SETTING_T_GAME_LENGTH_in_ms:
            NVM_T_GAME_LENGTH_IN_MS = value;
            break;
        
        case SYSTEMK_SETTING_SECONDARY_COLOR:
            NVM_SECONDARY_COLOR = value;
            break;
        
        default:
            result = SYSTEMK_RESULT_WRONG_DATATYPE;
            break;
    }
    
    return result;
}

SystemKResult_T SETTINGS_get_device_name(char* name)
{
    static char device_name[] = "Another 2020TPC";
    
    strncpy(name, device_name, sizeof(device_name));
    
    return SYSTEMK_RESULT_SUCCESS;
}

SystemKResult_T SETTINGS_set_device_name(char* name)
{
    return SYSTEMK_RESULT_NOT_IMPLEMENTED;
}

SystemKResult_T SETTINGS_Save(void)
{
#if (CONFIG__HAS_EXTERNAL_NVM)
    NVM_SaveExternalEEPROMEntry(&NVM_Hardware_Settings);
    NVM_SaveExternalEEPROMEntry(&NVM_Game_Settings);
#else // CONFIG__HAS_EXTERNAL_NVM
    NVM_SaveOnChipEEPROMEntry(&NVM_Hardware_Settings);
    NVM_SaveOnChipEEPROMEntry(&NVM_Game_Settings);
#endif // CONFIG__HAS_EXTERNAL_NVM

    return SYSTEMK_RESULT_SUCCESS;
}
