/*
 * File: Screen_Settings_Reboot.c
 *
 * Created: Created: Tuesday September 2025 20:36:48
 *   Author: Chris
 */


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

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

#include "hardware/watchdog.h"


// ============================================================================================
// Defines
#define COLOR_DOT_ACTIVE           0xFFFF    // Pure White	#FFFFFF
#define COLOR_DOT_INACTIVE         0x0842    // Dark Gray	#404450

#define DOT_ANIMATION_SPEED        8


// ============================================================================================
// Variables
extern const uint8_t _Font_DejaVu_Sans_Mono_Bold_15x26[];

static Object_ID _Object_YesNo;
static Object_ID _Object_Text;
static Object_ID _Object_Dots[3];

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

static bool     _Bool_Value;
static bool	    _Decision_Made;
static uint32_t	_Counter;
static int32_t  _Dot_Animation_Phase;


// ============================================================================================
// Function Declarations
void Screen_Setup_Settings_Reboot(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration, const Hierarchical_Menu* return_menu, const Menu_List* return_list, int32_t return_selected_item);

static void Update_Dot_Animation(void);

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_Settings_Reboot(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration, const Hierarchical_Menu* return_menu, const Menu_List* return_list, int32_t return_selected_item)
{
    _Bool_Value = false;
   
    Screen_Init(direction_out, direction_in, type, frame_duration);

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

    _Counter                = 0;
    _Dot_Animation_Phase	= 0;
    _Decision_Made          = false;
}

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_Text = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_Bold_15x26, 0);

    Display_Render_Complex_Select_YesNo_Set(_Bool_Value, &_Configuration_Default_Select_YesNo);
    _Object_YesNo = Display_Objects_Add_Select_YesNo("Reboot Device", 13, &_Bool_Value, &_Configuration_Default_Select_YesNo);

    _Object_Text = Display_Objects_Add_Text(CENTER_BOTTOM, BOTH_IN_PERCENT, 50, 50, NOT_SELECTABLE, "Rebooting", Font_Text, DISPLAY_COLOR_LIGHTGREY, NO_STYLE, NO_ANIMATION);
    Display_Objects_Update_Enabled(_Object_Text, false);

    for(int i = 0; i < 3; i++) {
        _Object_Dots[i] = Display_Objects_Add_Circle_Frame(CENTER_MIDDLE, BOTH_IN_PERCENT, 50 + (i-1) * 6, 60, NOT_SELECTABLE, COLOR_DOT_INACTIVE, 4, 1, NO_STYLE, NO_ANIMATION);
        Display_Objects_Update_Enabled(_Object_Dots[i], false);
	}

    Display_Select_Object();
}

void Screen_Tick(void)
{
    if(_Decision_Made && _Bool_Value) {
        Update_Dot_Animation();
    }
}

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;
    }
   
	_Bool_Value = !_Bool_Value;
}

void Screen_Action_CCW(Object_ID object_id)
{
    Screen_Action_CW(object_id);
}

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) {
        _Decision_Made = true;

        if(_Bool_Value == false) {
            Screen_Setup_Settings_Hierarchical_Menu(TRANSITION_DOWN, TRANSITION_RIGHT, SCREEN_TRANSITION_DEFAULT_EASING, SCREEN_TRANSITION_DEFAULT_FRAMES, _Return_Menu, _Return_List, _Return_Selected_Item);
            return;
        }

        Display_Objects_Update_Enabled(_Object_YesNo, false);
        Display_Objects_Update_Enabled(_Object_Text, true);
        for(int i = 0; i < 3; i++) {
            Display_Objects_Update_Enabled(_Object_Dots[i], true);
    	}

        watchdog_reboot(0, 0, 5000);
    }
}


/*******************************************************************
    Internal Functions
*******************************************************************/
void Update_Dot_Animation(void)
{
    _Dot_Animation_Phase++;

    if(_Dot_Animation_Phase >= (DOT_ANIMATION_SPEED * 3)) {
        _Dot_Animation_Phase = 0;
    }
    
    // Determine which dot should be active
    int Active_Dot = _Dot_Animation_Phase / DOT_ANIMATION_SPEED;
    
    // Update dot colors
    for(int i = 0; i < 3; i++) {
        Display_Color Dot_Color = (i == Active_Dot) ? COLOR_DOT_ACTIVE : COLOR_DOT_INACTIVE;
        Display_Objects_Update_Color(_Object_Dots[i], Dot_Color);
    }
}
