/*
 * File: Screen_Select_Hue.c
 *
 * Created: Created: Friday October 2025 16:12:34
 *   Author: Chris
 */


// ============================================================================================
// Includes
#include "../Screens.h"
#include "../Hue.h"
#include "../Command.h"
#include "../UI_Control.h"
#include "../Mode_Manager.h"

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


// ============================================================================================
// Defines


// ============================================================================================
// Variables
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 int32_t*  	_Value;
static int32_t  	_Display_Value;
static const Menu_Configuration_Select_Value* _Config;

static bool	        _Decision_Made;
static uint32_t	    _Counter;
static Mode	        _Current_Mode;


// ============================================================================================
// Function Declarations
void Screen_Setup_Select_Hue(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration, char* title, uint32_t title_length, int32_t* hue_value, const Menu_Configuration_Select_Value* 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_Hue(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration, char* title, uint32_t title_length, int32_t* hue_value, const Menu_Configuration_Select_Value* config, const Hierarchical_Menu* return_menu, const Menu_List* return_list, int32_t return_selected_item)
{
    _Title = title;
    _Title_Length = title_length;
    _Value = hue_value;
    _Display_Value = *hue_value * config->Value_Display_Ratio;
	_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;

    _Current_Mode = Mode_Manager_Get_Current_Mode();

    Mode_Manager_Set_Mode(PREVIEW);
}

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 //
    //////////////////////////////
    Style_ID Style_Value_Var = Display_Objects_Add_Style(DISPLAY_COLOR_BLACK, DISPLAY_COLOR_LIGHTGREY, 1, 0, PADDING_1(3), STYLE_WIDTH_HEIGHT_RATIO_AUTO);

    Display_Objects_Add_Select_Value(_Title, _Title_Length, &_Display_Value, _Config->Max, _Config->Min, (char*)_Config->Format, &_Configuration_Default_Select_Value);
    _Object_Value_Bar = Display_Objects_Add_Value_Bar_Rect(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 63, NOT_SELECTABLE, _Value, 359, 0, LEFT_TO_RIGHT, DISPLAY_COLOR_BLACK, 200, 10, Style_Value_Var, 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)
{
    LED_Data_t RGB_Value;
    RGB_Value.Pixel = Hue_Get_Color_From_Angle(*_Value);
    
    Command_Issue_Set_Request(MULTICORE_COMMAND_SET_DIRECT_RED	, MULTICORE_NO_PARAMETER, RGB_Value.R);
	Command_Issue_Set_Request(MULTICORE_COMMAND_SET_DIRECT_GREEN, MULTICORE_NO_PARAMETER, RGB_Value.G);
	Command_Issue_Set_Request(MULTICORE_COMMAND_SET_DIRECT_BLUE	, MULTICORE_NO_PARAMETER, RGB_Value.B);

    Display_Objects_Update_Color(_Object_Value_Bar, DISPLAY_COLOR_FROM_RGB888(RGB_Value.R, RGB_Value.G, RGB_Value.B));

    if(_Decision_Made) {
        _Counter++;
    }

    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(_Value, _Config->Min, _Config->Max, _Config->Cycle_Selector);

    _Display_Value = *_Value * _Config->Value_Display_Ratio;
}

void Screen_Action_CCW(Object_ID object_id)
{
    if(_Decision_Made) {
        return;
    }
    
    UI_Control_Selector_Dec(_Value, _Config->Min, _Config->Max, _Config->Cycle_Selector);

    _Display_Value = *_Value * _Config->Value_Display_Ratio;
}

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(!_Decision_Made) {
        Display_Objects_Show_Message_Box(_Object_Message_Box, MESSAGE_BOX_DEFAULT_TICKS);
        _Decision_Made = true;

        Command_Issue_Set_Request(MULTICORE_COMMAND_SET_DIRECT_RED	, MULTICORE_NO_PARAMETER, 0);
	    Command_Issue_Set_Request(MULTICORE_COMMAND_SET_DIRECT_GREEN, MULTICORE_NO_PARAMETER, 0);
	    Command_Issue_Set_Request(MULTICORE_COMMAND_SET_DIRECT_BLUE	, MULTICORE_NO_PARAMETER, 0);

        Mode_Manager_Set_Mode(_Current_Mode);
    }
}


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

