/*
 * 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.h"
#include "../Display_Font.h"
#include "../Display_Objects.h"
#include "../Display_Default_Configurations.h"

#include "../INA260.h"
#include "../EEPROM_M24C64.h"


// ============================================================================================
// Defines
#define GRAPH_DATA_WDITH	200
#define COLOR_DARKGREEN     DISPLAY_COLOR_FROM_RGB888(0, 100, 0)


// ============================================================================================
// Variables
extern const unsigned char 	_Font_DejaVu_Sans_Mono_6x12[];
extern const unsigned char 	_Font_DejaVu_Sans_Mono_10x17[];
extern const unsigned char 	_Font_DejaVu_Sans_Mono_Bold_7x15[];
extern const unsigned char 	_Font_DejaVu_Sans_Mono_Bold_11x17[];

static Object_ID _Object_Value_BusVoltage;
static Object_ID _Object_Value_Current;

static uint16_t _Data_BusVoltage[GRAPH_DATA_WDITH];
static uint16_t _Data_Current[GRAPH_DATA_WDITH];
static uint16_t _Data_Threshold_Current[GRAPH_DATA_WDITH];
static float	_Actual_BusVoltage_V;
static float	_Actual_Current_A;


// ============================================================================================
// 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_Current[i] = 0;
         _Data_Threshold_Current[i] = _EEPROM_Content.Device_Configuration.Current_Threshold * 5; // 5x, due to 4x 1.25mA
	}
}

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 //
	//////////////////////////////
    char Current_Threshold[10];
    Display_Font_Set_Font(_Font_DejaVu_Sans_Mono_6x12);
    int16_t Font_6_1_Height = Display_Font_Get_Font_Height();

    sprintf(Current_Threshold, "%.3fA", (float)(_EEPROM_Content.Device_Configuration.Current_Threshold * 5) / 1000.0f);
    int16_t Current_Threshold_Label_Y = DISPLAY_Y_CENTER + 100 - _EEPROM_Content.Device_Configuration.Current_Threshold * 5 / 25;

    if((Current_Threshold_Label_Y <= (149 + Font_6_1_Height)) && (Current_Threshold_Label_Y >= 149)) {
        Current_Threshold_Label_Y += (Font_6_1_Height + 1);
    }
    else if((Current_Threshold_Label_Y <= 149) && (Current_Threshold_Label_Y >= 141)) {
        Current_Threshold_Label_Y = 141;
    }


    Font_ID Font_6_1 = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_6x12, 1);
    Font_ID Font_10_1 = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_10x17, 1);
    Font_ID Font_7_1_B = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_Bold_7x15, 1);
    Font_ID Font_11_1_B = Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_Bold_11x17, 1);

    // Threshold Lines / Areas
    Display_Objects_Add_Rectangle_Frame(CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, 141, NOT_SELECTABLE, COLOR_DARKGREEN, GRAPH_DATA_WDITH+2, 8, 1, NO_STYLE, NO_ANIMATION);
    Display_Objects_Add_Graph(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, false, _Data_Threshold_Current, GRAPH_DATA_WDITH, 5000, 0, DISPLAY_COLOR_ORANGE, 200, NO_STYLE, NO_ANIMATION);

    // Threshold Labels
    Display_Objects_Add_Text(LEFT_BOTTOM, BOTH_IN_PIXEL, 19, 140, NOT_SELECTABLE, "12.5V", Font_6_1, COLOR_DARKGREEN, NO_STYLE, NO_ANIMATION);
    Display_Objects_Add_Text(LEFT_TOP   , BOTH_IN_PIXEL, 19, 149, NOT_SELECTABLE, "11.5V", Font_6_1, COLOR_DARKGREEN, NO_STYLE, NO_ANIMATION);
    Display_Objects_Add_Text(CENTER_BOTTOM, X_IN_PERCENT_Y_IN_PIXEL, 50, Current_Threshold_Label_Y, NOT_SELECTABLE, Current_Threshold, Font_6_1, DISPLAY_COLOR_ORANGE, NO_STYLE, NO_ANIMATION);

    // Data Graphs (Voltage and Current)
	Display_Objects_Add_Graph(CENTER_MIDDLE, BOTH_IN_PERCENT, 50, 50, false, _Data_BusVoltage, 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_Current, GRAPH_DATA_WDITH, 5000, 0, DISPLAY_COLOR_YELLOW, 200, NO_STYLE, NO_ANIMATION);
	
    // Data Values (Voltage and Current)
	_Object_Value_BusVoltage = Display_Objects_Add_Float(RIGHT_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 48, 50, NOT_SELECTABLE, &_Actual_BusVoltage_V, "%2.1fV", Font_11_1_B, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);
	_Object_Value_Current = Display_Objects_Add_Float(LEFT_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 52, 50, NOT_SELECTABLE, &_Actual_Current_A, "%1.3fA", Font_11_1_B, DISPLAY_COLOR_YELLOW, NO_STYLE, NO_ANIMATION);
}

void Screen_Tick(void)
{
    Display_Objects_Update_Coordinates(_Object_Value_BusVoltage, RIGHT_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 48, 50);
    Display_Objects_Update_Coordinates(_Object_Value_Current, LEFT_MIDDLE, X_IN_PERCENT_Y_IN_PIXEL, 52, 50);
    
    for(int i=0;i<GRAPH_DATA_WDITH-1;i++) {
		_Data_BusVoltage[i] = _Data_BusVoltage[i+1];
		_Data_Current[i] = _Data_Current[i+1];
	}

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

    _Data_Current[GRAPH_DATA_WDITH-1] = INA260_Get_Current_mA();
	_Actual_Current_A = ((float)_Data_Current[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
*******************************************************************/

