/*
 * Display_Objects_Datatypes.h
 *
 * Created: Sun Apr 04 2021 11:48:15
 *  Author Chris
 */

#ifndef DISPLAY_OBJECTS_DATATYPES_H_
#define DISPLAY_OBJECTS_DATATYPES_H_

// ============================================================================================
// Includes
#include "Display_Config.h"
#include "Command_Definition.h"


// ============================================================================================
// Defines
#define	PADDING_TOP			0
#define	PADDING_RIGHT		1
#define	PADDING_BOTTOM		2
#define	PADDING_LEFT		3

#define HEIGHT_CONTENT		0
#define WIDTH_CONTENT		0

#define NO_STYLE			-1
#define NO_ANIMATION		-1

#define PADDING_1(__ALL__)						__ALL__,__ALL__,__ALL__,__ALL__
#define PADDING_2(__HOR__, __VER__)				__HOR__,__VER__,__HOR__,__VER__
#define PADDING_4(__T__, __R__, __B__, __L__)	__T__,__R__,__B__,__L__

#define SELECTABLE			true
#define NOT_SELECTABLE		false

#define TEXT_AND_LENGTH(_TEXT_)		(uint8_t*)_TEXT_, sizeof(_TEXT_)-1

// Animation configuration constants (adjustable in frames)
#define MENU_RING_APPEAR_CIRCLE_DRAW_SPEED      12		// Frames per degree of circle drawing
#define MENU_RING_APPEAR_CIRCLE_GROW_SPEED      8		// Frames for center circle to grow to full size
#define MENU_RING_APPEAR_ITEM_POP_SPEED         4		// Frames for each item to appear
#define MENU_RING_APPEAR_ITEM_DELAY             1		// Frames between each item appearance

// Animation timing constants Menu Hierarchical
#define MENU_HIERARCHICAL_TRANSITION_FRAMES     20              // Frames for slide transition
#define MENU_HIERARCHICAL_SLIDE_DISTANCE        DISPLAY_WIDTH   // Pixels to slide (full screen width)


// ============================================================================================
// Variables / Datatypes


/*******************************************************************
	Forwward Declarations
*******************************************************************/
typedef struct Menu_Item_S          Menu_Item;
typedef struct Menu_List_S          Menu_List;
typedef struct Hierarchical_Menu_S  Hierarchical_Menu;


/*******************************************************************
	Generic Types
*******************************************************************/
typedef union 
{
	struct
	{
		uint8_t R;		
		uint8_t G;
		uint8_t B;
		uint8_t A;	// Alpha channel (unused, for 32-bit alignment)
	};
	uint8_t Array[4];
	uint32_t Pixel;
} __packed RGB_Color;

typedef enum {
	FLOAT,
	INTEGER,
	TEXT,
	VALUE_BAR_RECT,
	VALUE_BAR_ARC,
	GRAPH,
	BUTTON,
	IMAGE,
	BOOLEAN,
	SHAPE,
	CANVAS,
	MESSAGE_BOX,
	MENU_SELECT,
	MENU_ICON_ROW,
	MENU_RING,
    MENU_HIERARCHICAL,
	SELECT_YESNO,
	SELECT_LIST,
	SELECT_VALUE,
	SELECT_RGB,
	ENTRY_INDICATOR
} Object_Type;

typedef enum  {
	RECTANGLE_FILLED,
	RECTANGLE_FRAME,
	ROUNDED_RECTANGLE_FILLED,
	ROUNDED_RECTANGLE_FRAME,
    CIRCLE_FILLED,
	CIRCLE_FRAME,
	ARC,
	LINE_XY,
	LINE_RAD
} Shape_Type;

typedef enum 
{
	CENTER		= 0x00,
	LEFT		= 0x01,
	RIGHT		= 0x02,

	MIDDLE		= 0x00,
	TOP			= 0x10,
	BOTTOM		= 0x20,

	CENTERED	= CENTER | MIDDLE
} Alignment;

typedef enum
{
	LEFT_TOP 		= LEFT | TOP,
	LEFT_MIDDLE 	= LEFT | MIDDLE,
	LEFT_BOTTOM 	= LEFT | BOTTOM,

	CENTER_TOP 		= CENTER | TOP,
	CENTER_MIDDLE 	= CENTER | MIDDLE,
	CENTER_BOTTOM 	= CENTER | BOTTOM,

	RIGHT_TOP 		= RIGHT | TOP,
	RIGHT_MIDDLE 	= RIGHT | MIDDLE,
	RIGHT_BOTTOM 	= RIGHT | BOTTOM
} Anchor_Point;

typedef enum {
	BOTH_IN_PIXEL,
	X_IN_PIXEL_Y_IN_PERCENT,
	X_IN_PERCENT_Y_IN_PIXEL,
	BOTH_IN_PERCENT
} Coordinates_Type;

typedef enum {
	LEFT_TO_RIGHT,
	RIGHT_TO_LEFT,
	BOTTOM_TO_TOP,
	TOP_TO_BOTTOM
} Orientation;

typedef enum {
	START_WITH_PREVIOUS,
	START_AFTER_PREVIOUS
} Animation_Order;

typedef enum {
	NO_STARTED,
	DELAYING,
	MOVING,
	COMPLETE
} Animation_State;

typedef enum {
	CANVAS_UPDATE_OK	= 0,
	OBJECT_NOT_FOUND 	= 1,
	OBJECT_WRONG_TYPE	= 2,
	X_OUT_OF_RANGE 		= 3,
	Y_OUT_OF_RANGE 		= 4
} Canvas_Update_Error;

typedef struct {
	int16_t X;
	int16_t Y;
} Coordinates;

typedef struct {
	uint16_t Width;
	uint16_t Height;
} Dimension;

typedef int Object_ID;
typedef int Style_ID;
typedef int Animation_ID;
typedef int Font_ID;

typedef enum {
	TRANSITION_NONE,
	TRANSITION_LEFT,
	TRANSITION_RIGHT,
	TRANSITION_UP,
	TRANSITION_DOWN
} Screen_Transition_Direction;

// Animation states for menu ring appear effect
typedef enum {
    MENU_RING_APPEAR_STATE_IDLE,              // No animation running
    MENU_RING_APPEAR_STATE_DRAWING_RING,      // Drawing outer ring and growing center
    MENU_RING_APPEAR_STATE_POPPING_ITEMS,     // Items appearing one by one
    MENU_RING_APPEAR_STATE_COMPLETE           // Animation finished
} Menu_Ring_Appear_State;


/*******************************************************************
	Font Type
*******************************************************************/
typedef struct {
	const unsigned char 	*Font;
	uint8_t					Character_Spacing;
} Font;


/*******************************************************************
	Different Format Types
*******************************************************************/
typedef struct {
	float* 			Value;
	char 			Format[64];
	Font* 			Font;
	Display_Color 	Color;
} Object_Float;

typedef struct {
	int* 			Value;
	char 			Format[64];
	Font* 			Font;
	Display_Color 	Color;
} Object_Integer;

typedef struct {
	char 			Text[64];
	uint8_t			Length;
	Font*			Font;
	Display_Color 	Color;
} Object_Text;

typedef struct {
	Shape_Type 		Type;
	Display_Color 	Color;
	Dimension		Dimension;
	uint16_t		Radius_Start;
	uint16_t		Radius_End;
	uint16_t 		Thickness;
	uint16_t		Draw_Steps;
	int16_t			Angle_Start;
	int16_t			Angle_End;
} Object_Shape;

typedef struct {
	uint32_t*		Value;
	uint32_t		Max;
	uint32_t		Min;
	Orientation		Orientation;
	Display_Color 	Color;
	Dimension		Dimension;
} Object_Value_Bar_Rect;

typedef struct {
	uint32_t*	Value;
	uint32_t	Max;
	uint32_t 	Min;
	uint32_t 	Current;

	uint32_t Delta_Inc;
	uint32_t Delta_Dec;

	Object_Shape* Arc;
	int16_t	Angle_End;
} Object_Value_Bar_Arc;

typedef struct {
	uint16_t*		Data;
	uint16_t		Data_Length;
	uint			Max;
	uint			Min;
	Display_Color 	Color;
	Dimension		Dimension;
} Object_Graph;

typedef struct {
	char 			Text[64];
	uint8_t			Length;
	Font*			Font;
	uint			Return_Value;
	Display_Color 	Color;
	Dimension		Dimension;
} Object_Button;

typedef const uint16_t 	Image_Color;
typedef const uint8_t 	Image_Alpha;

typedef struct {
	Image_Color *Image;
	uint16_t Rotation_Angle;
	uint8_t Alpha;
} Object_Image_Color;

typedef struct {
	bool*			Value;			
	char 			Text_True[16];
	uint8_t			Length_True;
	Display_Color 	Color_True;
	char 			Text_False[16];
	uint8_t			Length_False;
	Display_Color 	Color_False;
	Font*			Font;
} Object_Bool;

typedef struct {
	 Dimension		Dimension;
	 uint16_t*		Data;
} Object_Canvas;

/*******************************************************************
	MESSAGE_BOX Configuration and Object Structure
*******************************************************************/
typedef enum {
	MESSAGE_BOX_TEXT_BAR_DISTANCE 	= 2,
	MESSAGE_BOX_BAR_HEIGHT 			= 3,
	MESSAGE_BOX_TEXT_ICON_DISTANCE 	= 10
} Object_Message_Box_Properties;

typedef enum {
	MESSAGE_BOX_ICON_NONE,
	MESSAGE_BOX_ICON_CIRCLE_ADD,
	MESSAGE_BOX_ICON_CIRCLE_ALERT,
	MESSAGE_BOX_ICON_CRCLE_BAN,
	MESSAGE_BOX_ICON_CHATBUBBLE,
	MESSAGE_BOX_ICON_CIRCLE_CHECKMARK,
	MESSAGE_BOX_ICON_CIRCLE_CLOSE,
	MESSAGE_BOX_ICON_CIRCLE_INFORMATION,
	MESSAGE_BOX_ICON_EMOJI_HAPPY,
	MESSAGE_BOX_ICON_EMOJI_SAD,
	MESSAGE_BOX_ICON_EMOJI_SKULL
} Object_Message_Box_Icon;

typedef struct {
	const unsigned char* Font;

    Display_Color Background_Color;
    Display_Color Border_Color;
    Display_Color Text_Color;
    Display_Color Accent_Color;
    Display_Color Shadow_Color;
    uint8_t Border_Radius;
    uint8_t Border_Thickness;

    uint32_t Padding[4];
} Message_Box_Style;

typedef struct {
	char Text[64];
	int  Length;

	Object_Message_Box_Icon Icon;
    const Message_Box_Style* Style;

	uint32_t Show_Ticks_Max;
	uint32_t Show_Ticks_Left;
} Object_Message_Box;



typedef struct {
	int16_t X_Offset;
    int16_t X_Indent;

    const unsigned char* Font[3];
    Display_Color Color[3];
} Configuration_Menu_Select;

typedef struct {
	char* 		Menu_Titles;
	uint32_t 	Menu_Entry_Count;
	uint32_t 	Title_Char_Length;
	uint32_t* 	Selected_Entry;
	Configuration_Menu_Select* Config;
} Object_Menu_Select;


typedef struct {
	uint32_t Icon_Space_Width;
	float Shrink_Factor;
	int16_t Y_Images;
	int16_t Y_Images_Warp;
	int16_t Y_Text;

    const unsigned char *Font;
    Display_Color Color;
} Configuration_Menu_Icon_Row;

typedef struct {
	Image_Color* 	Image;
	char* 			Title;
	uint32_t		Title_Length;
} Icon_Row_Item;

typedef struct {
	Icon_Row_Item* 	Items;
	uint32_t 		Item_Count;
	uint32_t* 		Selected_Item;

	Configuration_Menu_Icon_Row* Config;
} Object_Menu_Icon_Row;


/*******************************************************************
	MENU_RING Configuration and Object Structure
*******************************************************************/
// Animation data structure
typedef struct {
    Menu_Ring_Appear_State State;
    
    // Phase 1: Ring drawing and center growing
    float Ring_Draw_Angle;				// Current angle of ring drawn (0-360)
    float Center_Scale;					// Current scale of center circle (0-1)
    uint32_t Ring_Draw_Counter; 		// Frame counter for ring drawing
    uint32_t Center_Grow_Counter;		// Frame counter for center growing
    
    // Phase 2: Item popping
    uint32_t Current_Item_Appearing;	// Which item is currently appearing (0-item_count)
    float Current_Item_Scale;			// Scale of currently appearing item (0-1)
    uint32_t Item_Pop_Counter; 			// Frame counter for current item
    uint32_t Item_Delay_Counter;		// Frame counter for delay between items
    
    // Animation timing
    uint32_t Total_Ring_Draw_Frames;	// Total frames to draw complete ring
    uint32_t Total_Center_Grow_Frames;	// Total frames to grow center
    uint32_t Total_Item_Pop_Frames;		// Total frames for one item to appear
    uint32_t Total_Item_Delay_Frames;	// Total frames to wait between items
} Menu_Ring_Appear_Animation;

typedef struct {
    const char* Label;
    Image_Color* Icon;
} Menu_Ring_Item_Config;

typedef struct {
    // Colors
    Display_Color Center_Text_Color;
    Display_Color Center_BG_Color;
    Display_Color Center_Border_Color;
    Display_Color Selection_Ring_Color;

	// Fonts
	const unsigned char *Center_Text_Font;
    
    // Layout dimensions
    uint16_t Item_Radius;					// Distance from center to items
    uint16_t Center_Size;					// Diameter of center circle
    uint16_t Image_Size;					// Expected image size (renamed for clarity)
    
    // Animation settings
    float Selection_Scale;					// Scale factor for selected item
    uint16_t Animation_Duration;			// Animation duration in frames
    float Idle_Rotation_Speed;				// Degrees per frame for idle animation

	// Selection ring parameters
	uint16_t Selection_Ring_Diameter;		// Diameter of selection ring (0 = auto-calculate from image)
    uint16_t Selection_Ring_Thickness;		// Thickness of selection ring border (default: 3)
    uint16_t Selection_Ring_Padding;		// Extra padding around image (default: 4)
    
    // Layout options
    float Start_Angle_Degrees;				// Starting angle for first item
    bool Distribute_Evenly;					// If true, items are evenly spaced
    float Fixed_Angle_Step;					// Fixed angle between items

	// Appear animation settings
	bool Enable_Appear_Animation;			// Enable/disable appear animation
} Configuration_Menu_Ring;

typedef struct {
    Menu_Ring_Item_Config* Items;    // Dynamic array of menu items
    uint32_t Item_Count;             // Number of items
    uint32_t* Selected_Item;         // Pointer to current selection (0 to item_count-1)
    Configuration_Menu_Ring* Config;
    
    // Internal animation state (dynamic arrays)
    uint32_t Animation_Counter;
    float Idle_Rotation_Angle;
    uint32_t Selection_Animation_Progress;
    uint32_t Animation_Target;
    float* Item_Scales;              // Dynamic array of scales
    uint8_t* Item_Glow_Intensity;    // Dynamic array of glow intensities

	// Appear animation state
    Menu_Ring_Appear_Animation* Appear_Animation;
    bool Appear_Animation_Active;
} Object_Menu_Ring;


/*******************************************************************
	MENU_HIERARCHICAL Configuration and Object Structure
*******************************************************************/
// Animation states for hierarchical menu transitions
typedef enum {
    MENU_HIERARCHICAL_TRANSITION_STATE_IDLE,           // No transition running
    MENU_HIERARCHICAL_TRANSITION_STATE_SLIDE_OUT,      // Current menu sliding out
    MENU_HIERARCHICAL_TRANSITION_STATE_SLIDE_IN,       // New menu sliding in
    MENU_HIERARCHICAL_TRANSITION_STATE_COMPLETE        // Transition finished
} Menu_Hierarchical_Transition_State;

// Direction of menu transition
typedef enum {
    MENU_TRANSITION_ENTER_SUBMENU,     // Moving deeper into menu hierarchy
    MENU_TRANSITION_EXIT_SUBMENU       // Moving back up in hierarchy
} Menu_Transition_Direction;

// Transition animation structure
typedef struct {
    Menu_Hierarchical_Transition_State State;
    Menu_Transition_Direction Direction;
    
    // Animation progress
    uint16_t Transition_Counter;
    uint16_t Total_Transition_Frames;
    
    // Menu states
    const Menu_List* Previous_List;     // Menu being transitioned from
    const Menu_List* Target_List;       // Menu being transitioned to
    int32_t Previous_Selected_Item;
    int32_t Target_Selected_Item;
    int32_t Previous_Scroll_Offset;
    int32_t Target_Scroll_Offset;
    
    // Visual offsets for slide animation
    int16_t Current_Menu_X_Offset;      // Current menu position
    int16_t Target_Menu_X_Offset;       // Target menu position 
} Menu_Hierarchical_Transition_Animation;

typedef struct {
    // Fonts
    const unsigned char *Title_Font;
    const unsigned char *Item_Font;
    const unsigned char *Back_Font;
    
    // Colors
    Display_Color Title_Color;
    Display_Color Item_Selected_Color;
    Display_Color Item_Normal_Color;
    Display_Color Item_Back_Color;
    Display_Color Background_Color;
    Display_Color Border_Color;
    Display_Color Indicator_Color;
    
    // Layout
    int16_t Title_Y_Offset;
    int16_t Menu_Start_Y;
    int16_t Item_Height;
    int16_t Item_Spacing;
    uint8_t Item_Alignment;
    int16_t Side_Padding;
    int16_t Visible_Items;
    
    // Animation
    uint16_t Animation_Speed;
    uint16_t Transition_Frames;
    
    // Visual indicators
    bool Show_Scroll_Indicators;
    bool Show_Item_Icons;
    bool Show_Sub_Menu_Indicators;
    bool Show_Selection_Box;
    uint16_t Selection_Box_Padding;
} Configuration_Menu_Hierarchical;

typedef struct {
    const Menu_List** Current_List;
    int32_t* Selected_Item;
    int32_t* Scroll_Offset;
    Configuration_Menu_Hierarchical* Config;
    
    // Internal state
    uint32_t Animation_Counter;
    float Transition_Progress;
    int32_t Last_Selected_Item;

    const Menu_List* Last_Current_List;     // Previous frame's current list
    int32_t Last_Selected_Item_Value;       // Previous frame's selected item
    int32_t Last_Scroll_Offset_Value;       // Previous frame's scroll offset

    Menu_Hierarchical_Transition_Animation* Transition_Animation;
    bool Transition_Animation_Active;
} Object_Menu_Hierarchical;


typedef struct {
	const unsigned char *Title_Font;
	Display_Color Title_Color;
	int16_t Title_Y_Center;

	const unsigned char *Value_Font;
	int16_t Value_Center_X_Offset;
	int16_t Value_Y_Center;
	uint16_t Animation_Speed;
} Configuration_Select_YesNo;

typedef struct {
	char* Title;
	uint32_t Title_Length;

	bool* Value;
	
	Configuration_Select_YesNo* Config;
} Object_Select_YesNo;


typedef struct {
    const unsigned char *Font;
	int16_t List_Entry_Y_Gap;
	Display_Color Color_Selected;
	Display_Color Color_Not_Selected;
} Configuration_Select_List;

typedef struct {
	char* 		List_Titles;
	uint32_t 	List_Entry_Count;
	uint32_t 	List_Char_Length;
	uint32_t* 	Selected_Entry;
	Configuration_Select_List* Config;
} Object_Select_List;


typedef struct {
	const unsigned char *Title_Font;
	Display_Color Title_Color;
	int16_t Title_Y_Center;

	const unsigned char *Value_Font;
	Display_Color Value_Color;
	int16_t Value_Y_Center;

	bool Show_Arc;
	Display_Color Arc_Color;
} Configuration_Select_Value;

typedef struct {
	char* Title;
	uint32_t Title_Length;

	int32_t* Value;
	int32_t Max;
	int32_t Min;

	char Format[32];

	Configuration_Select_Value* Config;
} Object_Select_Value;

/*******************************************************************
	SELECT_RGB Configuration and Object Structure
*******************************************************************/
typedef struct {
	// Ring colors for each RGB component
	Display_Color Ring_Color_Red;
	Display_Color Ring_Color_Green;
	Display_Color Ring_Color_Blue;
	
	// Geometry settings
	uint16_t Center_Preview_Radius;			// Radius of central color preview circle
	uint16_t Progress_Ring_Radius;			// Radius of progress ring
	uint16_t Progress_Ring_Thickness;		// Thickness of progress ring
	uint16_t Indicator_Radius;				// Radius of value indicator dot
	
	// Colors for UI elements
	Display_Color Preview_Border_Color;		// Border color for preview circle
	uint16_t Preview_Border_Thickness;		// Border thickness for preview circle
	Display_Color Indicator_Core_Color;		// Core color of indicator dot
	Display_Color Indicator_Glow_Color;		// Glow color of indicator dot
	Display_Color Background_Ring_Color;	// Color for unfilled part of ring
	uint16_t Background_Ring_Thickness;		// Thickness of background ring
	
	// Previous color markers
    Display_Color Previous_Marker_Color;        // Color for previous component markers
    uint16_t Previous_Marker_Radius;            // Radius of previous marker dots
    uint16_t Previous_Marker_Ring_Offset;       // Distance from main ring (inner offset)
    bool Show_Previous_Markers;                 // Enable/disable previous markers

	// Text display settings
	const unsigned char* Component_Label_Font;	// Font for "RED", "GREEN", "BLUE" labels
	const unsigned char* Value_Font;			// Font for numeric value display
	Display_Color Text_Color;					// Color for all text elements
	int16_t Text_Y_Offset;						// Vertical offset from bottom for text
	
	// Animation settings
	uint16_t Smooth_Animation_Steps;	// Steps for smooth value transitions
	bool Enable_Encoder_Acceleration;	// Enable faster changes on fast rotation
	uint8_t Acceleration_Threshold;		// Encoder speed threshold for acceleration
	uint8_t Acceleration_Multiplier;	// Multiplier for fast rotation (2-10)
	
} Configuration_Select_RGB;

typedef struct {
	// RGB value pointer
	LED_Data_t* Color_Value;				// Pointer to RGB color structure
	
	// Current state
	uint8_t *Current_Component;			// Active component: 0=Red, 1=Green, 2=Blue
	uint8_t Component_Labels[3][6]; 	// Text labels: "RED", "GREEN", "BLUE"
	
	// Animation state
	uint8_t Target_Value;				// Target value for smooth animation
	uint8_t Display_Value;				// Currently displayed value (for smooth transitions)
	uint16_t Animation_Counter;			// Counter for smooth value changes
		
	// UI geometry (calculated at setup)
	uint16_t Progress_Start_Angle;		// Starting angle for progress arc (270° = 12 o'clock)
	
	// Configuration pointer
	Configuration_Select_RGB* Config;
} Object_Select_RGB;




typedef enum {
	INDICATOR_ARC,
	INDICATOR_DOT
} Type_Entry_Endicator;

typedef struct {
	Type_Entry_Endicator Type;

	Display_Color Color_Selected;
	Display_Color Color_Unselected;

	union Options {
		struct {
			int16_t Angle_Span;
			uint16_t Thickness;
			uint16_t Radius;
		} Arc;
		struct {
			int16_t Y;
			uint16_t Dot_Size;
			uint16_t Dot_Distance;
			bool Unselected_Frame_Only;
		} Dot;
	} Options;
} Configuration_Entry_Indicator;

typedef struct {
	uint32_t Entry_Count;
	int32_t* Entry_Value;

	Configuration_Entry_Indicator* Config;
} Object_Entry_Indicator;

/*******************************************************************
	Style Type
*******************************************************************/
typedef struct {
	Display_Color 	Background_Color;
	Display_Color	Border_Color;
	uint			Border_Thickness;
	uint			Border_Radius;
	uint			Padding[4];
	float			Width_Height_Ratio;
} Style;


/*******************************************************************
	Animation Type
*******************************************************************/
typedef struct {
	Coordinates 	Offset;
	uint			Tick_Delay;
	uint			Tick_Duration;
	Animation_Order	Animation_Start;
} Animation;

typedef struct {
	Coordinates			Target;
	Animation_State		State;
	uint				Tick_Counter;
} Animation_Status;


/*******************************************************************
	Main Object Type
*******************************************************************/
typedef struct {
	Object_Type			Type;
	Coordinates			Coordinates;
	Coordinates			Content_Offset;
	Dimension			Dimension;
	bool				Enabled;
	bool				Selectable;
	bool				Focused;
	void* 				Data;
	Style*				Style;
	Animation*			Animation;
	Animation_Status* 	Animation_Status;
} Display_Object;

#endif /* DISPLAY_OBJECTS_DATATYPES_H_ */