/*
 * File: Screen_Select_MinMax.c
 *
 * Created: Created: Friday October 2025 16:50:54
 *   Author: Chris
 */


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

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


// ============================================================================================
// Defines
#define DISPLAY_COLOR_VERYDARKGREY			DISPLAY_COLOR_FROM_RGB888(64, 64, 64)


// ============================================================================================
// Variables
extern const uint8_t _Font_DejaVu_Sans_Mono_Bold_7x15[];
extern const uint8_t _Font_DejaVu_Sans_Mono_10x17[];

static Object_ID _Object_Text_Min, _Object_Text_Max;
static Object_ID _Object_Box;
static Object_ID _Object_Message_Box;
static Object_ID _Object_Value_Bar;

static const Hierarchical_Menu* _Return_Menu = NULL;
static const Menu_List*         _Return_List = NULL;
static int32_t                  _Return_Selected_Item;

static char*        _Title;
static uint32_t     _Title_Length;

static MinMax_t*  	_Value;
static int32_t      _Value_Min;
static int32_t      _Value_Max;
static int32_t*     _Current_Value;
static int32_t      _Limit_Min;
static int32_t      _Limit_Max;

static int16_t      _Box_Y_Current;
static int16_t      _Box_Y_Target;

static const Menu_Configuration_Select_MinMax* _Config;
static const int16_t _Y_Center_Min = DISPLAY_Y_CENTER;
static const int16_t _Y_Center_Max = DISPLAY_Y_CENTER + 40;


static bool	        _Decision_Made;
static uint32_t	    _Counter;


// ============================================================================================
// Function Declarations
void Screen_Setup_Select_MinMax(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration, char* title, uint32_t title_length, MinMax_t* value, const Menu_Configuration_Select_MinMax* config, const Hierarchical_Menu* return_menu, const Menu_List* return_list, int32_t return_selected_item);

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_Select_MinMax(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration, char* title, uint32_t title_length, MinMax_t* value, const Menu_Configuration_Select_MinMax* config, const Hierarchical_Menu* return_menu, const Menu_List* return_list, int32_t return_selected_item)
{
    _Title = title;
    _Title_Length = title_length;
    
    _Value = value;
    _Value_Min = _Value->Min;
    _Value_Max = _Value->Max;

    _Current_Value = &_Value_Min;
    _Limit_Min = config->Min;
    _Limit_Max = config->Max - 1;

    _Box_Y_Target = _Y_Center_Min;
    _Box_Y_Current = _Box_Y_Target;

	_Config = config;

    Screen_Init(direction_out, direction_in, type, frame_duration);

    _Return_Menu = return_menu;
    _Return_List = return_list;
    _Return_Selected_Item = return_selected_item;

    _Decision_Made = false;
    _Counter = 0;
}

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_7x15, DISPLAY_DEFAULT_CHAR_SPACING);
    Font_ID Font_Value = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_10x17, 0);
    Display_Objects_Add_Text(CENTER_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 50, DISPLAY_Y_CENTER - 50, NOT_SELECTABLE, _Title, Font_Title, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION); 

    _Object_Text_Min = Display_Objects_Add_Integer(CENTER_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 50, _Y_Center_Min, NOT_SELECTABLE, (int*)(&_Value_Min), (char*)_Config->Format_Min, Font_Value, DISPLAY_COLOR_LIGHTGREY, NO_STYLE, NO_ANIMATION);
    _Object_Text_Max = Display_Objects_Add_Integer(CENTER_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 50, _Y_Center_Max, NOT_SELECTABLE, (int*)(&_Value_Max), (char*)_Config->Format_Max, Font_Value, DISPLAY_COLOR_DARKGREY, NO_STYLE, NO_ANIMATION);

    _Object_Box = Display_Objects_Add_Rounded_Rectangle_Frame(CENTER_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 50, _Box_Y_Current, NOT_SELECTABLE, DISPLAY_COLOR_LIGHTGREY, 140, 30, 3, 1, NO_STYLE, NO_ANIMATION);

    _Object_Message_Box = Display_Objects_Add_Message_Box(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, "Saved", MESSAGE_BOX_ICON_CIRCLE_CHECKMARK, &_Message_Box_Style_Regular);

    Display_Select_Object();

    UI_Control_Acceleration_Reset();
    UI_Control_Acceleration_Set_Enabled(_Config->Use_Acceleration);
}

void Screen_Tick(void)
{
    // Update Values in actual datasctructure passed via pointer
    _Value->Min = _Value_Min;
    _Value->Max = _Value_Max;

    // Animate the slection box to see which values in currently being edited
    if(_Box_Y_Target > _Box_Y_Current) {
        int16_t Distance = abs(_Box_Y_Current - _Box_Y_Target);
        _Box_Y_Current += ((Distance >> 1) + 1);
    }
    Display_Objects_Update_Coordinates(_Object_Box, CENTER_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 50, _Box_Y_Current);

    // Adjust the colors of the text depending if the text is currently edited or not
    int16_t Max_Distance = abs(_Y_Center_Max - _Y_Center_Min);
    float Ratio = ((float)(_Box_Y_Current - _Y_Center_Min)) / (float)Max_Distance;
    Display_Color Color_Min = Display_Color_Interpolate_Float(DISPLAY_COLOR_LIGHTGREY, DISPLAY_COLOR_VERYDARKGREY, Ratio);
    Display_Color Color_Max = Display_Color_Interpolate_Float(DISPLAY_COLOR_VERYDARKGREY, DISPLAY_COLOR_LIGHTGREY, Ratio);

    Display_Objects_Update_Color(_Object_Text_Min, Color_Min);
    Display_Objects_Update_Color(_Object_Text_Max, Color_Max);

    // Increment tick counter for message box if all values have been selected
    if(_Decision_Made) {
        _Counter++;
    }

    // Change back to the former screen (Hierarchical Menu), when the ticks are up
    if(_Counter == MESSAGE_BOX_DEFAULT_TICKS) {
        Screen_Setup_Settings_Hierarchical_Menu(TRANSITION_DOWN, TRANSITION_RIGHT, SCREEN_TRANSITION_DEFAULT_EASING, SCREEN_TRANSITION_DEFAULT_FRAMES, _Return_Menu, _Return_List, _Return_Selected_Item);
    }
}

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(_Decision_Made) {
        return;
    }
    
    UI_Control_Selector_Inc(_Current_Value, _Limit_Min, _Limit_Max, _Config->Cycle_Selector);

    if(_Current_Value == &_Value_Min) {
        if(_Value_Min >= _Value_Max) {
            _Value_Max = _Value_Min + 1;
        }
    }
    else {
        if(_Value_Max <= _Value_Min) {
            _Value_Min = _Value_Max - 1;
        }
    }
}

void Screen_Action_CCW(Object_ID object_id)
{
    if(_Decision_Made) {
        return;
    }
    
    UI_Control_Selector_Dec(_Current_Value, _Limit_Min, _Limit_Max, _Config->Cycle_Selector);

    if(_Current_Value == &_Value_Min) {
        if(_Value_Min >= _Value_Max) {
            _Value_Max = _Value_Min + 1;
        }
    }
    else {
        if(_Value_Max <= _Value_Min) {
            _Value_Min = _Value_Max - 1;
        }
    }
}

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)
{
    if(_Current_Value == &_Value_Min) {
        _Current_Value = &_Value_Max;

        _Limit_Min++;
        _Limit_Max++;

        _Box_Y_Target = _Y_Center_Max;

        Display_Select_Object();
        return;
    }
    
    if(!_Decision_Made) {
        Display_Objects_Show_Message_Box(_Object_Message_Box, MESSAGE_BOX_DEFAULT_TICKS);
        _Decision_Made = true;
    }
}


/*******************************************************************
    Internal Functions
*******************************************************************/

