/*
 * Screen_Data_List.c
 *
 * Created: Fri Feb 04 2022 22:03:02
 *  Author Chris
 */
// ============================================================================================
// Includes
#include "../Screens.h"
#include "../UI_Control.h"
#include "../Display_Default_Configurations.h"

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

#include "../MIDI_Note_List.h"
#include "../Command_Definition.h"

#include "pico/types.h"
#include "pico/time.h"


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


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

extern History_Buffer_t _MIDI_History_Buffer[RECEIVED_MIDI_HISTORY_BUFFER_SIZE];

static Object_ID	_Object_Text_Top;
static Object_ID	_Object_Text_Bottom;
static Object_ID	_Object_Text_Return;

typedef struct {
	int Number;
	int Data;
	int Timestamp;

	Object_ID	Object_Number;
	Object_ID	Object_Data_Hex;
	Object_ID	Object_Data_Dec;
	Object_ID	Object_Type_Info;
	Object_ID	Object_Timestamp;
	Object_ID	Object_Timestamp_Text;
} Object_History_Entry_s;

static Object_History_Entry_s Objects_History[RECEIVED_MIDI_HISTORY_BUFFER_SIZE];

static int16_t _X_Number 				= 10+2;
static int16_t _X_Data_Hex 				= 10+25;
static int16_t _X_Data_Dec 				= 10+53;
static int16_t _X_Data_Info 			= 10+25;
static int16_t _X_Data_Timestamp		= 10+89;
static int16_t _X_Data_Timestamp_Text	= 10+25;

static int16_t _Y_Offest_Scroll;
static int16_t _Y_Offset_1st_Entry		= 20;
static int16_t _Y_Offset_Rows 			= 10;
static int16_t _Y_Between_Entries 		= 38;

static int16_t _Y_Scroll_Speed			= 10;


// ============================================================================================
// Function Declarations
void Screen_Setup_MIDI_Log(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_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);

void Update_Object_Coordinates(void);
void Update_Object_Values(void);
bool Create_Info_Text(char* target_text, uint entry_id, uint8_t midi_data);
bool Create_Info_Data_Text(char* target_text, uint entry_id, uint command_offset, uint8_t midi_data);
bool Check_Entry_Is_Command(uint entry_id);


/*******************************************************************
	Functions
*******************************************************************/
void Screen_Setup_MIDI_Log(Screen_Transition_Direction direction_out, Screen_Transition_Direction direction_in, Easing type, uint32_t frame_duration)
{
	_Screen_Tick					= Screen_Tick;
	_Screen_Click					= Screen_Click;
	_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);

	Font_ID Font_10_0 		= Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_6x12, 0);
	Font_ID Font_10_1 		= Display_Objects_Add_Font(_Font_DejaVu_Sans_Mono_6x12, 1);

	Font_ID Font_Number		= Font_10_0;
	Font_ID Font_Data		= Font_10_0;
	Font_ID Font_Info		= Font_10_0;

	Style_ID Style_Header 	= Display_Objects_Add_Style(DISPLAY_COLOR_LIGHTGREY, DISPLAY_COLOR_BLACK, 0, 2, PADDING_1(2), STYLE_WIDTH_HEIGHT_RATIO_AUTO);

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

	_Y_Offest_Scroll = 0;

	_Object_Text_Top = Display_Objects_Add_Text(CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, 0, NOT_SELECTABLE, "Received MIDI Data", Font_10_0, DISPLAY_COLOR_BLACK, Style_Header, NO_ANIMATION);

	for(int16_t i=0;i<RECEIVED_MIDI_HISTORY_BUFFER_SIZE;i++)
	{
		Object_History_Entry_s *E = &Objects_History[i];
		
		E->Number = i+1;
		E->Object_Number = Display_Objects_Add_Integer(LEFT_TOP, BOTH_IN_PIXEL, 0, 0, NOT_SELECTABLE, &E->Number, "%u:", Font_Number, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);
	
		E->Data = 0;
		E->Object_Data_Hex = Display_Objects_Add_Integer(LEFT_TOP, BOTH_IN_PIXEL, 0, 0, NOT_SELECTABLE, &E->Data, "0x%02X", Font_Data, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);
		E->Object_Data_Dec = Display_Objects_Add_Integer(LEFT_TOP, BOTH_IN_PIXEL, 0, 0, NOT_SELECTABLE, &E->Data, "(%u)", Font_Data, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);

		E->Object_Type_Info = Display_Objects_Add_Text(LEFT_TOP, BOTH_IN_PIXEL, 0, 0, NOT_SELECTABLE, "Info here", Font_Info, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);
		
		E->Timestamp = 0;
		E->Object_Timestamp = Display_Objects_Add_Integer(LEFT_TOP, BOTH_IN_PIXEL, 0, 0, NOT_SELECTABLE, &E->Timestamp, "%-8u", Font_Data, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);
		E->Object_Timestamp_Text = Display_Objects_Add_Text(LEFT_TOP, BOTH_IN_PIXEL, 0,0, NOT_SELECTABLE, "Timestamp:", Font_Data, DISPLAY_COLOR_WHITE, NO_STYLE, NO_ANIMATION);
	}

	_Object_Text_Bottom = Display_Objects_Add_Text(CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, 0, NOT_SELECTABLE, "List End", Font_10_0, DISPLAY_COLOR_BLACK, Style_Header, NO_ANIMATION);
	_Object_Text_Return = Display_Objects_Add_Text(CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, 0, NOT_SELECTABLE, "Click to return", Font_10_0, DISPLAY_COLOR_LIGHTGREY, NO_STYLE, NO_ANIMATION);

	Update_Object_Coordinates();
	Display_Select_Object();
}

void Screen_Tick(void)
{
	Update_Object_Values();
}

void Screen_Click(uint button_return_value)
{

}

void Screen_Action_CW(Object_ID object_id)
{
	_Y_Offest_Scroll -= _Y_Scroll_Speed;
	Update_Object_Coordinates();
}

void Screen_Action_CCW(Object_ID object_id)
{
	_Y_Offest_Scroll += _Y_Scroll_Speed;
	Update_Object_Coordinates();
}

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_RIGHT, TRANSITION_RIGHT, SCREEN_TRANSITION_DEFAULT_EASING, SCREEN_TRANSITION_DEFAULT_FRAMES, false, 0);
}

/*******************************************************************
	Internal Functions
*******************************************************************/
void Update_Object_Coordinates(void)
{
	Display_Objects_Update_Coordinates(_Object_Text_Top, CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, _Y_Offest_Scroll);
	
	for(uint i=0;i<RECEIVED_MIDI_HISTORY_BUFFER_SIZE;i++)
	{
		Object_History_Entry_s *E = &Objects_History[i];

		Display_Objects_Update_Coordinates(E->Object_Number			, LEFT_TOP, BOTH_IN_PIXEL, _X_Number				, _Y_Offset_1st_Entry + _Y_Offest_Scroll + i*_Y_Between_Entries);
		Display_Objects_Update_Coordinates(E->Object_Data_Hex		, LEFT_TOP, BOTH_IN_PIXEL, _X_Data_Hex				, _Y_Offset_1st_Entry + _Y_Offest_Scroll + i*_Y_Between_Entries);
		Display_Objects_Update_Coordinates(E->Object_Data_Dec		, LEFT_TOP, BOTH_IN_PIXEL, _X_Data_Dec				, _Y_Offset_1st_Entry + _Y_Offest_Scroll + i*_Y_Between_Entries);
		Display_Objects_Update_Coordinates(E->Object_Type_Info		, LEFT_TOP, BOTH_IN_PIXEL, _X_Data_Info				, _Y_Offset_1st_Entry + _Y_Offest_Scroll + i*_Y_Between_Entries + 1 * _Y_Offset_Rows);
		Display_Objects_Update_Coordinates(E->Object_Timestamp		, LEFT_TOP, BOTH_IN_PIXEL, _X_Data_Timestamp		, _Y_Offset_1st_Entry + _Y_Offest_Scroll + i*_Y_Between_Entries + 2 * _Y_Offset_Rows);
		Display_Objects_Update_Coordinates(E->Object_Timestamp_Text	, LEFT_TOP, BOTH_IN_PIXEL, _X_Data_Timestamp_Text	, _Y_Offset_1st_Entry + _Y_Offest_Scroll + i*_Y_Between_Entries + 2 * _Y_Offset_Rows);
	}

	Display_Objects_Update_Coordinates(_Object_Text_Bottom, CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, _Y_Offset_1st_Entry + _Y_Offest_Scroll + RECEIVED_MIDI_HISTORY_BUFFER_SIZE*_Y_Between_Entries);
	Display_Objects_Update_Coordinates(_Object_Text_Return, CENTER_TOP, X_IN_PERCENT_Y_IN_PIXEL, 50, _Y_Offset_1st_Entry + _Y_Offest_Scroll + RECEIVED_MIDI_HISTORY_BUFFER_SIZE*_Y_Between_Entries + _Y_Offset_1st_Entry);
}

void Update_Object_Values(void)
{
	for(uint i=0;i<RECEIVED_MIDI_HISTORY_BUFFER_SIZE;i++)
	{
		Object_History_Entry_s 	*E = &Objects_History[i];
		History_Buffer_t		*H = &_MIDI_History_Buffer[i];
		char Info_Text[64];

		if(H->Data != HISTORY_ENTRY_UNDEFINED)
		{
			E->Data 		= H->Data;
			E->Timestamp 	= H->Timestamp_ms;
			bool Success 	= Create_Info_Text((char *)Info_Text, i, (uint8_t)H->Data);
			Display_Objects_Update_Text(E->Object_Type_Info, Info_Text);

			if(IS_MIDI_COMMAND((uint8_t)H->Data))
			{
				Display_Objects_Update_Color(E->Object_Type_Info, DISPLAY_COLOR_GREENYELLOW);
			}
			else
			{
				if(Success)
				{
					Display_Objects_Update_Color(E->Object_Type_Info, DISPLAY_COLOR_CYAN);
				}
				else
				{
					Display_Objects_Update_Color(E->Object_Type_Info, DISPLAY_COLOR_RED);
				}
			}
		}
		else
		{
			Display_Objects_Update_Text(E->Object_Type_Info, "No Data");
			Display_Objects_Update_Color(E->Object_Type_Info, DISPLAY_COLOR_DARKGREY);
		}
	}
}

bool Create_Info_Text(char* target_text, uint entry_id, uint8_t midi_data)
{
	if(IS_MIDI_DATA(midi_data))
	{
		uint Data_Offset = 0;
		for(uint i=0;i<3;i++)
		{
			Data_Offset++;
			if(Check_Entry_Is_Command(entry_id + Data_Offset))
			{
				return Create_Info_Data_Text(target_text, entry_id, Data_Offset, midi_data);
			}
		}
		
		sprintf(target_text, "Data");
		return true;
	}
	

	// Otherwise, it is a MIDI Command
	uint8_t MIDI_Event = MIDI_EVENT_FROM_COMMAND(midi_data); 
	
	if(IS_MIDI_COMMAND_WITH_CHANNEL(midi_data))
	{
		uint8_t MIDI_Channel = MIDI_CHANNEL_FROM_COMMAND(midi_data) + 1;
		
		switch (MIDI_Event)
		{
		case MIDI_EVENT_NOTE_OFF: 					sprintf(target_text, "Note Off Ch%u"		, MIDI_Channel);	break;
		case MIDI_EVENT_NOTE_ON:					sprintf(target_text, "Note On Ch%u"			, MIDI_Channel);	break;
		case MIDI_EVENT_POLYPHONIC_KEY_PRESSURE: 	sprintf(target_text, "Poly. Key Ch%u"		, MIDI_Channel);	break;
		case MIDI_EVENT_CONTROL_CHANGE:				sprintf(target_text, "Control Change Ch%u"	, MIDI_Channel); 	break;
		case MIDI_EVENT_PROGRAM_CHANGE:				sprintf(target_text, "Program Change Ch%u"	, MIDI_Channel); 	break;
		case MIDI_EVENT_CHANNEL_PRESSURE:			sprintf(target_text, "Channel Pressure Ch%u", MIDI_Channel);	break;
		case MIDI_EVENT_PITCH_BEND:					sprintf(target_text, "Pitch Bend Ch%u"		, MIDI_Channel); 	break;
		
		default:									sprintf(target_text, "<unknown> Ch%u"		, MIDI_Channel);	return false;
		}

		return true;
	}
	
	uint8_t MIDI_System_Message = MIDI_CHANNEL_FROM_COMMAND(midi_data);

	switch (MIDI_System_Message)
	{
	case MIDI_SYSTEM_EXCLUSIVE:					sprintf(target_text, "System_Exclusive");			break;
	case MIDI_SYSTEM_TIME_CODE_QUARTER_FRAME:	sprintf(target_text, "Time Code Quarter Frame");	break;
	case MIDI_SYSTEM_SONG_POSITION_POINTER:		sprintf(target_text, "Song Position Pointer"); 		break;
	case MIDI_SYSTEM_SONG_SELECT:				sprintf(target_text, "Song Select"); 				break;
	case MIDI_SYSTEM_TUNE_REQUEST:				sprintf(target_text, "System Tune Request");		break;
	case MIDI_SYSTEM_EXCLUSIVE_END:				sprintf(target_text, "End of Exclusive");			break;
	case MIDI_SYSTEM_TIMING_CLOCK:				sprintf(target_text, "Timing Clock");				break;
	case MIDI_SYSTEM_START:						sprintf(target_text, "Start Sequence");				break;
	case MIDI_SYSTEM_CONTINUE:					sprintf(target_text, "Continue Sequence");			break;
	case MIDI_SYSTEM_STOP:						sprintf(target_text, "Stop Sequence");				break;
	case MIDI_SYSTEM_ACTIVE_SENSING:			sprintf(target_text, "Active Sensing");				break;
	case MIDI_SYSTEM_RESET:						sprintf(target_text, "Sytem Reset");				break;

	case MIDI_SYSTEM_UNDEFINED_1:
	case MIDI_SYSTEM_UNDEFINED_2:
	case MIDI_SYSTEM_UNDEFINED_3:
	case MIDI_SYSTEM_UNDEFINED_4:				sprintf(target_text, "Undefined (Reserved)"); 		break;
	
	default:									sprintf(target_text, "<unknown>"); 					return false;
	}

	return true;
}

bool Create_Info_Data_Text(char* target_text, uint entry_id, uint command_offset, uint8_t midi_data)
{
	if(entry_id + command_offset >= RECEIVED_MIDI_HISTORY_BUFFER_SIZE)
	{
		sprintf(target_text, "Error 0 -> Contact Chris");
		return false;
	}
	
	uint8_t MIDI_Data_Command 	= (uint8_t)_MIDI_History_Buffer[entry_id + command_offset].Data;
	uint8_t MIDI_Event 			= MIDI_EVENT_FROM_COMMAND(MIDI_Data_Command);
	
	if(IS_MIDI_COMMAND_WITH_CHANNEL(MIDI_Data_Command))
	{
		switch (MIDI_Event)
		{
		case MIDI_EVENT_NOTE_OFF:
		case MIDI_EVENT_NOTE_ON:
		case MIDI_EVENT_POLYPHONIC_KEY_PRESSURE:
			if(command_offset == 1) { sprintf(target_text, "Note %s%i", _MIDI_Note_List[midi_data].Tone_Name, _MIDI_Note_List[midi_data].Octave); return true; } else
			if(command_offset == 2) { sprintf(target_text, "Velocity of %u", midi_data); return true; }
			break;

		case MIDI_EVENT_CONTROL_CHANGE:
			if(command_offset == 1) { sprintf(target_text, "Controller %u", midi_data); return true; } else
			if(command_offset == 2) { sprintf(target_text, "Velocity of %u", midi_data); return true; }
			break;

		case MIDI_EVENT_PROGRAM_CHANGE:
			if(command_offset == 1) { sprintf(target_text, "Program %u", midi_data+1); return true; }
			break;

		case MIDI_EVENT_CHANNEL_PRESSURE:
			if(command_offset == 1) { sprintf(target_text, "Pressure value %u", midi_data); return true; }
			break;

		case MIDI_EVENT_PITCH_BEND:
			if(command_offset == 1) { sprintf(target_text, "Pitch upper value"); return true; } else
			if(command_offset == 2) { sprintf(target_text, "Pitch lower value"); return true; }
			break;
		
		default:
			break;
		}
	}
	else
	{
		uint8_t MIDI_System_Message = MIDI_CHANNEL_FROM_COMMAND(midi_data);

		switch (MIDI_System_Message)
		{
		case MIDI_SYSTEM_EXCLUSIVE:
			sprintf(target_text, "System Message Data"); return true;
			break;

		case MIDI_SYSTEM_TIME_CODE_QUARTER_FRAME:
			if(command_offset == 1) { sprintf(target_text, "Type %u  Value %u", (midi_data & 0x70) >> 4, (midi_data & 0x0F)); return true; }
			break;

		case MIDI_SYSTEM_SONG_POSITION_POINTER:
			if(command_offset == 1) { sprintf(target_text, "Beat upper value %u", midi_data); return true; } else
			if(command_offset == 2) { sprintf(target_text, "Beat lower value %u", midi_data); return true; }
			break;
		
		case MIDI_SYSTEM_SONG_SELECT:
			if(command_offset == 1) { sprintf(target_text, "Selected song %u", midi_data); return true; }
			break;
		
		default:
			break;
		}
	}

	sprintf(target_text, "Unexpected MIDI Data");
	return false;
}

bool Check_Entry_Is_Command(uint entry_id)
{
	if(entry_id >= RECEIVED_MIDI_HISTORY_BUFFER_SIZE)
	{
		return false;
	}

	if(_MIDI_History_Buffer[entry_id].Data == HISTORY_ENTRY_UNDEFINED)
	{
		return false;
	}

	return IS_MIDI_COMMAND((uint8_t)_MIDI_History_Buffer[entry_id].Data);
}