/*
 * File: Hierarchical_Menu.c
 *
 * Created: Created: Sunday September 2025 07:47:28
 *   Author: Chris
 */
#include "Hierarchical_Menu.h"


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


// ============================================================================================
// Forward Declaration
static const Menu_Item _Menu_Items_MIDI_Config[];
static const Menu_Item _Menu_Items_MIDI_Notes[];  
static const Menu_Item _Menu_Items_MIDI_Pause[];
static const Menu_Item _Menu_Items_MIDI[];

static const Menu_List _Menu_List_MIDI_Config;
static const Menu_List _Menu_List_MIDI_Notes;  
static const Menu_List _Menu_List_MIDI_Pause;
static const Menu_List _Menu_List_MIDI;
const Hierarchical_Menu _Hierarchical_Menu_MIDI;


static const Menu_Item _Menu_Items_Jam[];
static const Menu_List _Menu_List_Jam;
const Hierarchical_Menu _Hierarchical_Menu_Jam;


static const Menu_Item _Menu_Items_Const_Light[];
static const Menu_List _Menu_List_Const_Light;
const Hierarchical_Menu _Hierarchical_Menu_Const_Light;


static const Menu_Item _Menu_Items_Device[];
static const Menu_List _Menu_List_Device;
const Hierarchical_Menu _Hierarchical_Menu_Device;


// ============================================================================================
// Variables Configuration
static const Menu_Configuration_Select_Value _Select_Value_Octave = {
	.Min = -2,
	.Max = 8,
    .Value_Display_Ratio = 1,
	.Format = "Octave %+d",
    .Use_Acceleration = false,
    .Cycle_Selector = false
};

static const Menu_Configuration_Select_Value _Select_Value_Fade_Speed = {
	.Min = 1,
	.Max = UINT8_MAX,
    .Value_Display_Ratio = 1,
	.Format = "%u",
    .Use_Acceleration = true,
    .Cycle_Selector = true
};

static const Menu_Configuration_Select_Value _Select_Value_Jam_Start_Color = {
	.Min = 0,
	.Max = 359,
    .Value_Display_Ratio = 1,
	.Format = "%u Deg",
    .Use_Acceleration = true,
    .Cycle_Selector = true
};

static const Menu_Configuration_Select_Value _Select_Value_Jam_Color_Change = {
	.Min = COLOR_CHANGE_MIN,
	.Max = COLOR_CHANGE_MAX,
    .Value_Display_Ratio = 1,
	.Format = "%u Deg",
    .Use_Acceleration = false,
    .Cycle_Selector = false
};

static const Menu_Configuration_Select_MinMax _Select_MinMax_Jam_Durations = {
	.Min = DURATION_MIN_s,
	.Max = DURATION_MAX_s,
	.Format_Max = "Max: %03us",
	.Format_Min = "Min: %03us",
    .Use_Acceleration = false,
    .Cycle_Selector = false
};

static const Menu_Configuration_Select_Value _Select_Value_Timeout = {
	.Min = 1,
	.Max = 600,
    .Value_Display_Ratio = 1,
	.Format = "%u Seconds",
    .Use_Acceleration = true,
    .Cycle_Selector = false
};

static const Menu_Configuration_Select_Value _Select_Value_Timeout_Device = {
	.Min = SCREEN_TIMEOUT_MIN_s,
	.Max = SCREEN_TIMEOUT_MAX_s,
    .Value_Display_Ratio = 1,
	.Format = "%u Seconds",
    .Use_Acceleration = true,
    .Cycle_Selector = false
};

static const Menu_Configuration_Select_Value _Select_Value_Current_Threshold = {
	.Min = 0,
	.Max = 1000,
    .Value_Display_Ratio = 5,
	.Format = "%u mA",
    .Use_Acceleration = true,
    .Cycle_Selector = false
};

static const char _Note_Names[NOTES_PER_OCTAVE][2] = {
	"C ",
	"C#",
	"D ",
	"D#",
	"E ",
	"F ",
	"F#",
	"G ",
	"G#",
	"A ",
	"A#",
	"B "
};

const Menu_Configuration_Select_List _Select_List_Note_Names = {
    .Item_Names = &(_Note_Names[0][0]),
    .Name_Length = 2,
    .Item_Count = NOTES_PER_OCTAVE,
    .Use_Acceleration = false,
    .Cycle_Selector = false
};


static const char _MIDI_Channel_Names[16][10] = {
	"Channel  1",
	"Channel  2",
	"Channel  3",
	"Channel  4",
	"Channel  5",
	"Channel  6",
	"Channel  7",
	"Channel  8",
	"Channel  9",
	"Channel 10",
	"Channel 11",
	"Channel 12",
	"Channel 13",
	"Channel 14",
	"Channel 15",
	"Channel 16"
};

const Menu_Configuration_Select_List _Select_List_MIDI_Channel = {
    .Item_Names = &(_MIDI_Channel_Names[0][0]),
    .Name_Length = 10,
    .Item_Count = 16,
    .Use_Acceleration = false,
    .Cycle_Selector = false
};

static const char _Pause_Light_Reset[4][21] = {
	"  Any Traffic  ",
	" Channel Match ",
	"  Event Match  ",
	"Channel & Event"
};

const Menu_Configuration_Select_List _Select_List_Pause_Light_Reset = {
    .Item_Names = &(_Pause_Light_Reset[0][0]),
    .Name_Length = 21,
    .Item_Count = 4,
    .Use_Acceleration = false,
    .Cycle_Selector = false
};

static const char _Device_Idle_Screen[4][13] = {
	"Black        ",
	"FAD Logo     ",
	"Current Mode ",
	"Mode Activity"
};

const Menu_Configuration_Select_List _Select_List_Device_Idle_Screen = {
    .Item_Names = &(_Device_Idle_Screen[0][0]),
    .Name_Length = 13,
    .Item_Count = 4,
    .Use_Acceleration = false,
    .Cycle_Selector = false
};


// ============================================================================================
// Variables Hierarchical Menu
static const Menu_Item _Menu_Items_MIDI_Config[] = {
    { "MIDI Channel"        , NULL, false, &_Menu_List_MIDI_Config, LIST, "MIDI Channel"        , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].MIDI_Channel)          , (void*)(&_Select_List_MIDI_Channel) },
	{ "Select Octave"       , NULL, false, &_Menu_List_MIDI_Config, VALUE, "Select Octave"      , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Octave)                , (void*)(&_Select_Value_Octave) },
	{ "Skip Note Off"       , NULL, false, &_Menu_List_MIDI_Config, BOOL, "Skip Note Off"       , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Skip_Note_Off_Event)   , NULL },
	{ "Note Reset Enable"   , NULL, false, &_Menu_List_MIDI_Config, BOOL, "Note Reset Enable"   , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Note_Reset_Enabled)    , NULL },
	{ "Note Reset Timeout"  , NULL, false, &_Menu_List_MIDI_Config, VALUE, "Note Reset Timeout" , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Note_Reset_Timeout)    , (void*)(&_Select_Value_Timeout) },
	{ "Back"                , NULL, true , &_Menu_List_MIDI_Config, NONE, "", NULL, NULL }
};

static const Menu_List _Menu_List_MIDI_Config = {
    .Title = "MIDI Config",
    .Items = _Menu_Items_MIDI_Config,
    .Item_Count = sizeof(_Menu_Items_MIDI_Config) / sizeof(Menu_Item),
    .Parent = &_Menu_Items_MIDI[0],
    .Root = &_Hierarchical_Menu_MIDI
};


static const Menu_Item _Menu_Items_MIDI_Notes[] = {
    { "Red"             , NULL, false, &_Menu_List_MIDI_Notes, LIST, "Note for Red"     , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Note_Color_Red)    , (void*)(&_Select_List_Note_Names) },
	{ "Green"           , NULL, false, &_Menu_List_MIDI_Notes, LIST, "Note for Green"   , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Note_Color_Green)  , (void*)(&_Select_List_Note_Names) },
	{ "Blue"            , NULL, false, &_Menu_List_MIDI_Notes, LIST, "Note for Blue"    , (void*)(&_EEPROM_Content.Channel_MIDI_Configuration[0].Note_Color_Blue)   , (void*)(&_Select_List_Note_Names) },
	{ "Default Notes"   , NULL, false, &_Menu_List_MIDI_Notes, DEFAULT_NOTES, "Reset Color Notes", NULL, NULL },
	{ "Back"            , NULL, true , &_Menu_List_MIDI_Notes, NONE, "", NULL, NULL }
};

static const Menu_List _Menu_List_MIDI_Notes = {
    .Title = "Color Notes",
    .Items = _Menu_Items_MIDI_Notes,
    .Item_Count = sizeof(_Menu_Items_MIDI_Notes) / sizeof(Menu_Item),
    .Parent = &_Menu_Items_MIDI[1],
    .Root = &_Hierarchical_Menu_MIDI
};


static const Menu_Item _Menu_Items_MIDI_Pause[] = {
    { "Enable"      , NULL, false, &_Menu_List_MIDI_Pause, BOOL , "Enable Pause Light"      , (void*)(&_EEPROM_Content.Pause_Light_Configuration[0].Enabled)        , NULL },
	{ "Color"       , NULL, false, &_Menu_List_MIDI_Pause, RGB  , "Pause Light Color"       , (void*)(&_EEPROM_Content.Pause_Light_Configuration[0].Color)          , NULL },
	{ "Fade Speed"  , NULL, false, &_Menu_List_MIDI_Pause, VALUE, "Pause Light Fade Speed"  , (void*)(&_EEPROM_Content.Pause_Light_Configuration[0].Fade_Speed)     , (void*)(&_Select_Value_Fade_Speed)},
	{ "Timeout"     , NULL, false, &_Menu_List_MIDI_Pause, VALUE, "Pause Light Timeout"     , (void*)(&_EEPROM_Content.Pause_Light_Configuration[0].Timeout)        , (void*)(&_Select_Value_Timeout) },
	{ "Reset"       , NULL, false, &_Menu_List_MIDI_Pause, LIST , "Pause Light Reset"       , (void*)(&_EEPROM_Content.Pause_Light_Configuration[0].Reset_Condition), (void*)(&_Select_List_Pause_Light_Reset) },
	{ "Back"        , NULL, true , &_Menu_List_MIDI_Pause, NONE , "", NULL, NULL }
};

static const Menu_List _Menu_List_MIDI_Pause = {
    .Title = "Pause Light",
    .Items = _Menu_Items_MIDI_Pause,
    .Item_Count = sizeof(_Menu_Items_MIDI_Pause) / sizeof(Menu_Item),
    .Parent = &_Menu_Items_MIDI[2],
    .Root = &_Hierarchical_Menu_MIDI
};


static const Menu_Item _Menu_Items_MIDI[] = {
    { "MIDI Config" , &_Menu_List_MIDI_Config   , false, &_Menu_List_MIDI, NONE, "", NULL, NULL },
	{ "Color Notes" , &_Menu_List_MIDI_Notes    , false, &_Menu_List_MIDI, NONE, "", NULL, NULL },
	{ "Pause Light" , &_Menu_List_MIDI_Pause    , false, &_Menu_List_MIDI, NONE, "", NULL, NULL },
	{ "Back"        , NULL                      , true , &_Menu_List_MIDI, NONE, "", NULL, NULL }
};

static const Menu_List _Menu_List_MIDI = {
    .Title = "MIDI",
    .Items = _Menu_Items_MIDI,
    .Item_Count = sizeof(_Menu_Items_MIDI) / sizeof(Menu_Item),
    .Parent = NULL,
    .Root = &_Hierarchical_Menu_MIDI
};


const Hierarchical_Menu _Hierarchical_Menu_MIDI = {
    .List = &_Menu_List_MIDI,
    .Parent_Function = &Screen_Setup_Settings,
    .Parent_Selected_Setting = 0
};



static const Menu_Item _Menu_Items_Jam[] = {
    { "Start Color"     , NULL, false, &_Menu_List_Jam, HUE, "Hue Angle Start Color", (void*)(&_EEPROM_Content.Jam_Light_Configuration.Hue_Angle_Start_Color), (void*)(&_Select_Value_Jam_Start_Color) },
    { "Color Change"    , NULL, false, &_Menu_List_Jam, VALUE, "Max Color Change", (void*)(&_EEPROM_Content.Jam_Light_Configuration.Color_Change), (void*)(&_Select_Value_Jam_Color_Change) },
    { "Durations"       , NULL, false, &_Menu_List_Jam, MINMAX, "Set Durations", (void*)(&_EEPROM_Content.Jam_Light_Configuration.Durations), (void*)(&_Select_MinMax_Jam_Durations) },
	{ "Fade Speed"      , NULL, false, &_Menu_List_Jam, VALUE, "Jam Fade Speed", (void*)(&_EEPROM_Content.Jam_Light_Configuration.Fade_Speed), (void*)(&_Select_Value_Fade_Speed) },
	{ "Back"            , NULL, true , &_Menu_List_Jam, NONE, "", NULL, NULL }
};

static const Menu_List _Menu_List_Jam = {
    .Title = "Jam Mode",
    .Items = _Menu_Items_Jam,
    .Item_Count = sizeof(_Menu_Items_Jam) / sizeof(Menu_Item),
    .Parent = NULL,
    .Root = &_Hierarchical_Menu_Jam
};


const Hierarchical_Menu _Hierarchical_Menu_Jam = {
    .List = &_Menu_List_Jam,
    .Parent_Function = &Screen_Setup_Settings,
    .Parent_Selected_Setting = 1
};



static const Menu_Item _Menu_Items_Const_Light[] = {
    { "Color"       , NULL, false, &_Menu_List_Const_Light, RGB     , "Constant Light Color", (void*)(&_EEPROM_Content.Const_Light_Configuration.Color), NULL },
	{ "Fade Speed"  , NULL, false, &_Menu_List_Const_Light, VALUE   , "Constant Light Fade" , (void*)(&_EEPROM_Content.Const_Light_Configuration.Fade_Speed), (void*)(&_Select_Value_Fade_Speed) },
	{ "Back"        , NULL, true , &_Menu_List_Const_Light, NONE    , "", NULL, NULL }
};

static const Menu_List _Menu_List_Const_Light = {
    .Title = "Constant Light",
    .Items = _Menu_Items_Const_Light,
    .Item_Count = sizeof(_Menu_Items_Const_Light) / sizeof(Menu_Item),
    .Parent = NULL,
    .Root = &_Hierarchical_Menu_Const_Light
};


const Hierarchical_Menu _Hierarchical_Menu_Const_Light = {
    .List = &_Menu_List_Const_Light,
    .Parent_Function = &Screen_Setup_Settings,
    .Parent_Selected_Setting = 2
};



static const Menu_Item _Menu_Items_Device[] = {
    { "Idle Screen "        , NULL, false, &_Menu_List_Device, LIST, "Idle Screen"          , (void*)(&_EEPROM_Content.Device_Configuration.Idle_Screen)            , (void*)(&_Select_List_Device_Idle_Screen) },
	{ "Screen Timeout"      , NULL, false, &_Menu_List_Device, VALUE, "Screen Timeout"      , (void*)(&_EEPROM_Content.Device_Configuration.Screen_Timeout)         , (void*)(&_Select_Value_Timeout_Device)    },
	{ "Rev. List Scroll"    , NULL, false, &_Menu_List_Device, BOOL, "Reverse Scrolling"    , (void*)(&_EEPROM_Content.Device_Configuration.Reverse_List_Scrolling) , NULL },
	{ "Color Correction"    , NULL, false, &_Menu_List_Device, BOOL, "Color Correction"     , (void*)(&_EEPROM_Content.Device_Configuration.Use_Color_Correction)   , NULL },
	{ "Current Threshold"   , NULL, false, &_Menu_List_Device, VALUE, "Current Threshold"   , (void*)(&_EEPROM_Content.Device_Configuration.Current_Threshold)      , (void*)(&_Select_Value_Current_Threshold) },
	{ "Reboot Device"       , NULL, false, &_Menu_List_Device, REBOOT, "", NULL, NULL },
	{ "Back"                , NULL, true , &_Menu_List_Device, NONE, "", NULL, NULL }
};

static const Menu_List _Menu_List_Device = {
    .Title = "Device Settings",
    .Items = _Menu_Items_Device,
    .Item_Count = sizeof(_Menu_Items_Device) / sizeof(Menu_Item),
    .Parent = NULL,
    .Root = &_Hierarchical_Menu_Device
};


const Hierarchical_Menu _Hierarchical_Menu_Device = {
    .List = &_Menu_List_Device,
    .Parent_Function = &Screen_Setup_Settings,
    .Parent_Selected_Setting = 3
};




// ============================================================================================
// Function Declarations


/*******************************************************************
    Functions
*******************************************************************/


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

