/*
 * File: Screen_Graph.c
 *
 * Created: Created: Thursday August 2025 12:59:42
 *   Author: Chris
 */

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

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

#include "../INA260.h"


// ============================================================================================
// Defines
#define GRAPH_DATA_WDITH	200


// ============================================================================================
// Variables
extern const unsigned char 	_Font_DejaVu_Sans_Mono_10x17[];

static uint16_t _Data_BusVoltage[GRAPH_DATA_WDITH];
static uint16_t _Data_BusVoltage_Threshold_Overvoltage[GRAPH_DATA_WDITH];
static uint16_t _Data_BusVoltage_Threshold_Undervoltage[GRAPH_DATA_WDITH];
static float	_Current_BusVoltage_V;


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

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_Graph(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration)
{
    Screen_Init(direction_out, direction_in, type, frame_duration);

	for(int i=0;i<GRAPH_DATA_WDITH;i++) {
		_Data_BusVoltage[i] = 0;
		_Data_BusVoltage_Threshold_Overvoltage[i] = THRESHOLD_OVERVOLTAGE_mV;
		_Data_BusVoltage_Threshold_Undervoltage[i] = THRESHOLD_UNDERVOLTAGE_mV;
	}
}

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_10_1 = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_10x17, 1);

	Display_Objects_Add_Graph(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, false, _Data_BusVoltage, GRAPH_DATA_WDITH, 32000, 0, DISPLAY_COLOR_WHITE, 200, NO_STYLE, NO_ANIMATION);
	Display_Objects_Add_Graph(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, false, _Data_BusVoltage_Threshold_Overvoltage, GRAPH_DATA_WDITH, 32000, 0, DISPLAY_COLOR_RED, 200, NO_STYLE, NO_ANIMATION);
	Display_Objects_Add_Graph(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, false, _Data_BusVoltage_Threshold_Undervoltage, GRAPH_DATA_WDITH, 32000, 0, DISPLAY_COLOR_RED, 200, NO_STYLE, NO_ANIMATION);

	Display_Objects_Add_Float(CENTER_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 50, 232, NOT_SELECTABLE, &_Current_BusVoltage_V, "%2.3fV", Font_10_1, DISPLAY_COLOR_LIGHTGREY, NO_STYLE, NO_ANIMATION);
}

void Screen_Tick(void)
{
	for(int i=0;i<GRAPH_DATA_WDITH-1;i++) {
		_Data_BusVoltage[i] = _Data_BusVoltage[i+1];
	}

	_Data_BusVoltage[GRAPH_DATA_WDITH-1] = INA260_Get_BusVoltage_mV();
	_Current_BusVoltage_V = ((float)_Data_BusVoltage[GRAPH_DATA_WDITH-1]) / 1000.0f;
}

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)
{
	Screen_Setup_Menu_Main(TRANSITION_LEFT, TRANSITION_LEFT, SCREEN_TRANSITION_DEFAULT_EASING, SCREEN_TRANSITION_DEFAULT_FRAMES, false, 2);
}

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

