/*
 * Screen_Loading.c
 *
 * Created: Fri Apr 02 2021 14:34:01
 *  Author Chris
 */

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

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


// ============================================================================================
// Defines
#define LOADING_MAX		           100

#define DOT_ANIMATION_SPEED        8
#define TEXT_CHANGE_INTERVAL       25
#define PROGRESS_UPDATE_DELAY      2

// Logo fade-in animation settings
#define LOGO_FADE_START_FRAME      40       // Frame when logo starts appearing
#define LOGO_FADE_DURATION         25       // Frames to complete fade-in
#define LOGO_HOLD_DURATION         35       // Frames to hold at full opacity before transition

// Clean Monochromatic Colors (RGB565)
#define COLOR_BACKGROUND           0x0000    // Pure black  #000000
#define COLOR_TEXT_PRIMARY         0xBEF7    // Off-white   #F5F5F5
#define COLOR_TEXT_SECONDARY       0x1084    // Medium gray #808080
#define COLOR_PROGRESS_TRACK       0x4529    // Dark track  #252832
#define COLOR_PROGRESS_FILL        0xFBDE    // Light gray	#DEDEDE
#define COLOR_DOT_ACTIVE           0xFFFF    // Pure white	#FFFFFF
#define COLOR_DOT_INACTIVE         0x0842    // Dark gray	#404450


// ============================================================================================
// Variables
extern const unsigned char 	_Font_Victor_Mono_Bold_8x19[];
extern const unsigned char 	_Font_Victor_Mono_Regular_6x11[];
extern const unsigned char 	_Font_Victor_Mono_Regular_7x13[];
extern const unsigned char 	_Font_DejaVu_Sans_Mono_10x17[];
extern const unsigned char 	_Font_DejaVu_Sans_Mono_6x12[];
extern const unsigned char 	_Font_DejaVu_Sans_Mono_Bold_Oblique_10x19[];

extern const uint16_t _Image_Fad_Logo_Background_160x160[];

static Object_ID _Object_Progress_Track;
static Object_ID _Object_Progress_Fill;
static Object_ID _Object_Loading_Text;
static Object_ID _Object_Status_Text;
static Object_ID _Object_Version_Text;
static Object_ID _Object_Dots[3];
static Object_ID _Object_Logo_Image;

static int32_t _Counter;
static int32_t _Loading_Value;
static int32_t _Post_Load_Wait;
static int32_t _Animation_Counter;
static int32_t _Dot_Animation_Phase;
static int32_t _Text_Phase;
static int32_t _Logo_Blend_Phase;
static int32_t _Logo_Fade_Counter;

// Loading status messages
static char* _Loading_Messages[] = {
    "Initializing system...",
    "Loading configuration...",
    "Preparing interface...",
    "Setting up hardware...",
    "Configuring display...",
    "Loading resources...",
    "Finalizing setup...",
    "Ready to start!"
};
static const int _Message_Count = 8;


// ============================================================================================
// Function Declarations
void Screen_Setup_Loading();

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);

// Helper functions
static void Update_Dot_Animation(void);
static void Update_Status_Text(void);
static void Update_Logo_Fade_Animation(void);
static float Smooth_Fade_Curve(float t);


/*******************************************************************
	Functions
*******************************************************************/
void Screen_Setup_Loading()
{
	_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();
	
    // Initialize variables
    _Loading_Value          = 0;
    _Animation_Counter      = 0;
    _Dot_Animation_Phase	= 0;
    _Text_Phase				= 0;
    _Post_Load_Wait			= 0;
	_Logo_Blend_Phase		= 0;
    _Logo_Fade_Counter		= 0;

    // Create fonts
    Font_ID Font_Primary   = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_10x17, 1);
    Font_ID Font_Secondary = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_6x12, 0);
    Font_ID Font_Micro     = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_6x12, 0);
    

	//////////////////////////////
	// Add Display Objects here //
	//////////////////////////////

	// Progress bar background track
    _Object_Progress_Track = Display_Objects_Add_Rectangle_Filled(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 45, NOT_SELECTABLE, COLOR_PROGRESS_TRACK, DISPLAY_WIDTH - 60, 8, NO_STYLE, NO_ANIMATION);

    // Progress bar fill
    _Object_Progress_Fill = Display_Objects_Add_Value_Bar_Rect(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 45, NOT_SELECTABLE, &_Loading_Value, LOADING_MAX, 0, LEFT_TO_RIGHT, COLOR_PROGRESS_FILL, DISPLAY_WIDTH - 60, 8, NO_STYLE, NO_ANIMATION);

    // Three dots positioned below progress bar
    for(int i = 0; i < 3; i++) {
        _Object_Dots[i] = Display_Objects_Add_Circle_Frame(CENTER_MIDDLE, BOTH_IN_PERCENT, 46 + (i * 4), 55, NOT_SELECTABLE, COLOR_DOT_INACTIVE, 3, 1, NO_STYLE, NO_ANIMATION);
	}
    
    // Main loading text, // Above progress bar
    _Object_Loading_Text = Display_Objects_Add_Text(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 35, NOT_SELECTABLE, "Loading", Font_Primary, COLOR_TEXT_PRIMARY, NO_STYLE, NO_ANIMATION);

    // Dynamic status text // Below dots
    _Object_Status_Text = Display_Objects_Add_Text(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 65, NOT_SELECTABLE, _Loading_Messages[0], Font_Secondary, COLOR_TEXT_SECONDARY, NO_STYLE, NO_ANIMATION);

    // Version text (minimal, bottom corner)
    char version_string[16];
    sprintf(version_string, "v%s", VERSION_BUILD_STRING);
    _Object_Version_Text = Display_Objects_Add_Text(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 96, NOT_SELECTABLE, version_string, Font_Micro, COLOR_TEXT_SECONDARY, NO_STYLE, NO_ANIMATION);




	// Logo image (initially disabled, will be shown during blend)
	_Object_Logo_Image = Display_Objects_Add_Image(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, NOT_SELECTABLE, _Image_Fad_Logo_Background_160x160, 0, NO_STYLE, NO_ANIMATION);
    Display_Objects_Update_Enabled(_Object_Logo_Image, false);
    Display_Objects_Update_Alpha(_Object_Logo_Image, 0);

	Display_Select_First_Object();
	Display_Select_Object();

	 _Counter = 0;
}

void Screen_Tick(void)
{
	_Animation_Counter++;
    
    // Update loading progress
    if(_Loading_Value < LOADING_MAX) {
        if(_Animation_Counter % PROGRESS_UPDATE_DELAY == 0) {
            _Loading_Value++;
        }
    }

    // Update animated dots
    Update_Dot_Animation();

    // Update status text periodically
    if(_Animation_Counter % TEXT_CHANGE_INTERVAL == 0) {
        Update_Status_Text();
    }

    // Handle completion
    if(_Loading_Value >= LOADING_MAX)
	{
        _Post_Load_Wait++;
        
        if(_Post_Load_Wait == 20)
		{
            // Set all dots to active state
            for(int i = 0; i < 3; i++) {
                Display_Objects_Update_Color(_Object_Dots[i], COLOR_DOT_ACTIVE);
            }
            Display_Objects_Update_Text(_Object_Status_Text, "Complete!");
            // Recenter the completion text
            Display_Objects_Update_Coordinates(_Object_Status_Text, CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 65);
        }
		else if(_Post_Load_Wait >= 25 && _Post_Load_Wait < LOGO_FADE_START_FRAME)
		{
            // Start fading out loading elements early (15 frames before logo appears)
            float Fade_Factor = (float)(_Post_Load_Wait - 25) / (LOGO_FADE_START_FRAME - 25);
            
            // Apply smooth easing curve for more natural fade
            Fade_Factor = Smooth_Fade_Curve(Fade_Factor);
            
            // Calculate faded colors for loading elements
            Display_Color Faded_Primary 	= Display_Color_Interpolate_Float(COLOR_TEXT_PRIMARY	, COLOR_BACKGROUND, Fade_Factor);
            Display_Color Faded_Secondary 	= Display_Color_Interpolate_Float(COLOR_TEXT_SECONDARY	, COLOR_BACKGROUND, Fade_Factor);
            Display_Color Faded_Progress 	= Display_Color_Interpolate_Float(COLOR_PROGRESS_FILL	, COLOR_BACKGROUND, Fade_Factor);
            Display_Color Faded_Dots 		= Display_Color_Interpolate_Float(COLOR_DOT_ACTIVE		, COLOR_BACKGROUND, Fade_Factor);
            Display_Color Faded_Track 		= Display_Color_Interpolate_Float(COLOR_PROGRESS_TRACK	, COLOR_BACKGROUND, Fade_Factor);
            
            // Apply faded colors
            Display_Objects_Update_Color(_Object_Loading_Text, Faded_Primary);
            Display_Objects_Update_Color(_Object_Status_Text, Faded_Secondary);
            Display_Objects_Update_Color(_Object_Version_Text, Faded_Secondary);
            Display_Objects_Update_Color(_Object_Progress_Fill, Faded_Progress);
            Display_Objects_Update_Color(_Object_Progress_Track, Faded_Track);
            
            for(int i = 0; i < 3; i++) {
                Display_Objects_Update_Color(_Object_Dots[i], Faded_Dots);
            }
        }
		else if(_Post_Load_Wait >= LOGO_FADE_START_FRAME)
		{
            Update_Logo_Fade_Animation();

            if(_Post_Load_Wait >= LOGO_FADE_START_FRAME + LOGO_FADE_DURATION + LOGO_HOLD_DURATION)
			{
                Screen_Setup_Menu_Main(TRANSITION_LEFT, TRANSITION_NONE, INOUT_SINE, 25, true, 3);
            }
        }
    }
}

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)
{

}

void Screen_Action_CCW(Object_ID 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)
{

}

/*******************************************************************
	Internal Functions
*******************************************************************/
static void Update_Dot_Animation(void)
{
    // Only animate dots while loading
    if(_Loading_Value >= LOADING_MAX) return;
    
    _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);
    }
}

static void Update_Status_Text(void)
{
    // Only update text while loading
    if(_Loading_Value >= LOADING_MAX) return;
    
    // Calculate which message to show based on progress
    int message_index = (_Loading_Value * _Message_Count) / LOADING_MAX;
    if(message_index >= _Message_Count) {
        message_index = _Message_Count - 1;
    }
    
    // Update the status text
    Display_Objects_Update_Text(_Object_Status_Text, _Loading_Messages[message_index]);
}

static void Update_Logo_Fade_Animation(void)
{
    // Enable logo on first frame if not already enabled
    if(_Logo_Fade_Counter == 0) {
        Display_Objects_Update_Enabled(_Object_Logo_Image, true);
    }
    
    _Logo_Fade_Counter++;
    
    // Calculate fade progress (0.0 to 1.0)
    float Fade_Progress = (float)_Logo_Fade_Counter / (float)LOGO_FADE_DURATION;
    
    // Clamp to valid range
    if(Fade_Progress > 1.0f) {
        Fade_Progress = 1.0f;
    }
    
    // Apply easing for smoother fade-in (ease-out cubic)
    float Eased_Progress = Ease_Out_Cubic(Fade_Progress);
    
    // Convert to alpha value (0-255)
    uint8_t Alpha_Value = (uint8_t)(Eased_Progress * 127.0f);
    
    // Update the logo's alpha
    Display_Objects_Update_Alpha(_Object_Logo_Image, Alpha_Value);
}

static float Smooth_Fade_Curve(float t)
{
    // Smoothstep function for natural easing: 3t² - 2t³
    // Provides smooth acceleration and deceleration
    if(t <= 0.0f) return 0.0f;
    if(t >= 1.0f) return 1.0f;
    
    return t * t * (3.0f - 2.0f * t);
}