/*
 * File: Screen_EEPROM_Code.c
 *
 * Created: Created: Monday September 2025 16:11:48
 *   Author: Chris
 */


// ============================================================================================
// Includes
#include "../Screens.h"
#include "../UI_Control.h"

#include "../Display.h"
#include "../Display_Objects.h"
#include "../Display_Default_Configurations.h"

// ============================================================================================
// Defines
#define BOX_X_TARGET    (DISPLAY_X_CENTER + (_Character_Select - 1) * _Character_X_Distance - 1)


// ============================================================================================
// Variables
extern const unsigned char 	_Font_DejaVu_Sans_Mono_Bold_11x17[];
extern const uint8_t _Font_DejaVu_Sans_Mono_20x34[];

static Object_ID    _Object_Code[3];
static Object_ID    _Object_Box;

static int32_t      _Code[3]  = { 4, 4, 12 };
static int32_t      _Input[3];
static int32_t      _Character_Select;
static char         _Character[3][2];

static const int16_t    _Character_X_Distance = 50;
static int16_t          _Box_X_Current;
static int16_t          _Box_X_Target;


// ============================================================================================
// Function Declarations
void Screen_Setup_EEPROM_Code(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration);

static char HexNumber_To_ASCII         (char hex_number);

static void Screen_Init                (Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration);
static void Screen_Tick                (void);
static void Screen_Click               (uint button_return_value);
static void Screen_Touch_Event         (int16_t x, int16_t y);
static void Screen_Action_CW           (Object_ID object_id);
static void Screen_Action_CCW          (Object_ID object_id);
static void Screen_On_Object_Focused   (Object_ID object_id);
static void Screen_On_Object_Defocused (Object_ID object_id);
static void Screen_On_Object_Select    (Object_ID object_id);
static void Screen_On_Object_Deselect  (Object_ID object_id);


/*******************************************************************
    Functions
*******************************************************************/
void Screen_Setup_EEPROM_Code(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration)
{
    _Input[0] = 0;
	_Input[1] = 0;
	_Input[2] = 0;

	_Character_Select = 0;

    _Box_X_Target = BOX_X_TARGET;
    _Box_X_Current = _Box_X_Target;

   
    Screen_Init(direction_out, direction_in, type, frame_duration);
}

void Screen_Init(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration)
{
    _Screen_Last                   = Screen_Init;
    _Screen_Tick                   = Screen_Tick;
    _Screen_Click                  = Screen_Click;
    _Screen_Touch_Event            = Screen_Touch_Event;
    _Screen_Action_CW              = Screen_Action_CW;
    _Screen_Action_CCW             = Screen_Action_CCW;
    _Screen_On_Objects_Focused     = Screen_On_Object_Focused;
    _Screen_On_Objects_Defocused   = Screen_On_Object_Defocused;
    _Screen_On_Object_Select       = Screen_On_Object_Select;
    _Screen_On_Object_Deselect     = Screen_On_Object_Deselect;

    Display_Objects_Clear();
    Display_Screen_Transition_Start(direction_out, direction_in, type, frame_duration);


    //////////////////////////////
    // Add Display Objects here //
    //////////////////////////////
    Font_ID Font_Title = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_Bold_11x17, DISPLAY_DEFAULT_CHAR_SPACING);
    Font_ID Font_Characters = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_20x34, 0);

    Display_Objects_Add_Text(CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, 45, NOT_SELECTABLE, "Enter Code", Font_Title, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);

    for(int i=0;i<3;i++) {
        _Character[i][0] = HexNumber_To_ASCII(_Input[i]);
        _Character[i][1] = '\0';
        _Object_Code[i] = Display_Objects_Add_Text(CENTER_MIDDLE, X_IN_PIXEL_Y_IN_PERCENT, DISPLAY_X_CENTER + (i-1)*_Character_X_Distance, 50, NOT_SELECTABLE, &(_Character[i][0]), Font_Characters, DISPLAY_COLOR_LIGHTGREY, NO_STYLE, NO_ANIMATION);
    }

    _Object_Box = Display_Objects_Add_Rounded_Rectangle_Frame(CENTER_MIDDLE, X_IN_PIXEL_Y_IN_PERCENT, _Box_X_Current, 49, NOT_SELECTABLE, DISPLAY_COLOR_LIGHTGREY, 33, 39, 6, 2, NO_STYLE, NO_ANIMATION);

    UI_Control_Acceleration_Reset();
    UI_Control_Acceleration_Set_Enabled(false);
    
    Display_Select_Object();
}

void Screen_Tick(void)
{
    for(int i=0;i<3;i++) {
        Display_Color Color = i == _Character_Select ? DISPLAY_COLOR_LIGHTGREY : DISPLAY_COLOR_DARKGREY;
        _Character[i][0] = HexNumber_To_ASCII(_Input[i]);

        Display_Objects_Update_Text(_Object_Code[i], &_Character[i][0]);
        Display_Objects_Update_Color(_Object_Code[i], Color);
    }


    _Box_X_Target = BOX_X_TARGET;

    if(_Box_X_Target > _Box_X_Current) {
        int16_t Distance = abs(_Box_X_Current - _Box_X_Target);
        _Box_X_Current += ((Distance >> 1) + 1);
    }

    Display_Objects_Update_Coordinates(_Object_Box, CENTER_MIDDLE, X_IN_PIXEL_Y_IN_PERCENT, _Box_X_Current, 49);
}

void Screen_Click(uint button_return_value)
{

}

void Screen_Touch_Event(int16_t x, int16_t y)
{

}

void Screen_Action_CW(Object_ID object_id)
{
    if(_Character_Select >= (sizeof(_Input) / sizeof(int32_t))) {
		return;
	}

	UI_Control_Selector_Inc(&_Input[_Character_Select], 0, 15, true);
}

void Screen_Action_CCW(Object_ID object_id)
{
    if(_Character_Select >= (sizeof(_Input) / sizeof(int32_t))) {
		return;
	}

    UI_Control_Selector_Dec(&_Input[_Character_Select], 0, 15, true);
}

void Screen_On_Object_Focused(Object_ID object_id)
{

}

void Screen_On_Object_Defocused(Object_ID object_id)
{

}

void Screen_On_Object_Select(Object_ID object_id)
{

}

void Screen_On_Object_Deselect(Object_ID object_id)
{
    Display_Select_Object();
    
    if(_Character_Select < (sizeof(_Input) / sizeof(int32_t))) {
		_Character_Select++;
	}

	if(_Character_Select < (sizeof(_Input) / sizeof(int32_t))) {
		return;
	}

    bool Code_Correct = (_Input[0] == _Code[0]) && (_Input[1] == _Code[1]) && (_Input[2] == _Code[2]);

    _Input[0] = 0;
	_Input[1] = 0;
	_Input[2] = 0;

    // Move to write screen instead directly to settings menu
    Screen_Setup_EEPROM_Write(TRANSITION_UP, TRANSITION_UP, SCREEN_TRANSITION_DEFAULT_EASING, SCREEN_TRANSITION_DEFAULT_FRAMES, Code_Correct);
}


/*******************************************************************
    Internal Functions
*******************************************************************/
char HexNumber_To_ASCII(char hex_number)
{
	if(hex_number >=0 && hex_number <= 9)
	{
		return '0' + hex_number;
	}
	else if(hex_number >= 10 && hex_number <= 15)
	{
		return 'A' + hex_number - 10;
	}

	return '0';
}