#include "menu.h"
#include "graphics.h"
#include "fonts/font.h"
#include "display.h"

static uint8_t selected_entry_idx = 0;

void draw_menu(const graphical_menu_t *const menu)
{
    const uint16_t entry_height = 40;
    const uint16_t padding_x = 10;
    const uint16_t padding_y = 0;
    const pixel_t enabled_text_color = MAKE_PIXEL(31, 63, 31);  // White text
    const pixel_t disabled_text_color = MAKE_PIXEL(15, 31, 15);  // Gray text for disabled
    const pixel_t entry_bg_color = MAKE_PIXEL(15, 15, 15);  // Slightly darker for entry background
    const pixel_t highlighted_bg_color = MAKE_PIXEL(0, 31, 63);  // Blue background for highlighted entry
    const pixel_t highlighted_text_color = MAKE_PIXEL(31, 63, 31);  // White text for highlighted entry
    
    // Ensure selected_entry_idx is within bounds
    if (selected_entry_idx >= menu->num_entries)
    {
        selected_entry_idx = 0;
    }
    
    for (uint8_t i = 0; i < menu->num_entries; i++)
    {
        const uint16_t y_pos = i * entry_height;
        const graphical_menu_entry_t *entry = &menu->entries[i];
        const bool is_highlighted = (i == selected_entry_idx) && entry->enabled;
        
        // Draw entry background - use highlighted color if this is the selected entry
        pixel_t bg_color = is_highlighted ? highlighted_bg_color : entry_bg_color;
        DrawBox(0, y_pos, DISPLAY_WIDTH, entry_height, bg_color);
        
        // Draw entry text - use highlighted color if selected, otherwise use enabled/disabled color
        pixel_t text_color;
        if (is_highlighted)
        {
            text_color = highlighted_text_color;
        }
        else
        {
            text_color = entry->enabled ? enabled_text_color : disabled_text_color;
        }
        
        // Calculate baseline from top position: baseline = top + (line_height - base_line)
        const uint16_t text_baseline_y = y_pos + padding_y + (roboto_bold_font.line_height - roboto_bold_font.base_line);
        draw_string((pixel_t *)framebuffer, &roboto_bold_font, 
                    padding_x, text_baseline_y, 
                    entry->title, text_color);
    }
}

void select_menu_entry(const graphical_menu_t *const menu)
{
    // Ensure selected_entry_idx is within bounds
    if (selected_entry_idx >= menu->num_entries)
    {
        return;
    }
    
    const graphical_menu_entry_t *entry = &menu->entries[selected_entry_idx];
    
    // Only select if the entry is enabled
    if (entry->enabled && entry->selected_callback_function != NULL)
    {
        entry->selected_callback_function(entry->selected_callback_function_args);
    }
}

uint8_t get_selected_menu_entry_idx(void)
{
    return selected_entry_idx;
}

void set_selected_menu_entry_idx(const graphical_menu_t *const menu, uint8_t idx)
{
    // Clamp the index to valid range
    if (idx >= menu->num_entries)
    {
        idx = menu->num_entries > 0 ? menu->num_entries - 1 : 0;
    }
    
    // Only allow selecting enabled entries
    if (menu->entries[idx].enabled)
    {
        selected_entry_idx = idx;
        
        // Call highlighted callback if it exists
        if (menu->entries[idx].highlighted_callback_function != NULL)
        {
            menu->entries[idx].highlighted_callback_function(menu->entries[idx].highlighted_callback_function_args);
        }
    }
}