-- Theme configuration for Lumacs
-- This file demonstrates how to configure and switch themes from Lua

-- Theme switching functions
function switch_to_theme(theme_name)
    local success, message = editor:execute_command("set-theme", {theme_name})
    if not success then
        message("Failed to switch theme: " .. message)
    end
end

-- Specific theme switchers
function switch_to_everforest()
    switch_to_theme("everforest-dark")
end

function switch_to_default()
    switch_to_theme("default")
end

function switch_to_dracula()
    switch_to_theme("dracula")
end

function switch_to_solarized()
    switch_to_theme("solarized-dark")
end

function switch_to_nord()
    switch_to_theme("nord")
end

function switch_to_gruvbox_light()
    switch_to_theme("gruvbox-light")
end

-- Enhanced theme listing
function list_themes()
    local success, message = editor:execute_command("list-themes")
    if success then
        message(message)
    else
        message("Failed to list themes: " .. message)
    end
end

-- Theme management utilities
function create_theme(name, config)
    -- Helper function to create custom themes from Lua
    if not config then
        message("Theme configuration required")
        return
    end
    
    -- This would be extended to allow custom theme creation
    message("Custom theme creation not yet implemented")
end

function get_current_theme()
    local themes = editor.theme_manager:theme_names()
    -- This could be enhanced to return the actual current theme
    message("Current theme detection needs implementation")
end

-- Register theme management commands
editor:register_command("list-available-themes", "List all available themes with descriptions", function(args)
    local themes = editor.theme_manager:theme_names()
    if #themes == 0 then
        return {success = true, message = "No themes available"}
    end
    
    local descriptions = {
        ["default"] = "Default light theme with basic colors",
        ["everforest-dark"] = "Everforest dark theme - comfortable green-tinted dark theme",
        ["dracula"] = "Dracula theme - popular dark theme with purple accents",
        ["solarized-dark"] = "Solarized Dark - precision colors for machines and people",
        ["nord"] = "Nord - arctic, north-bluish clean and elegant theme",
        ["gruvbox-light"] = "Gruvbox Light - retro groove warm light theme"
    }
    
    local result = "Available themes:\n"
    local current_theme = editor.theme_manager.active_theme
    local current_name = current_theme and current_theme:name() or "none"
    
    for i, name in ipairs(themes) do
        local indicator = (name == current_name) and " (current)" or ""
        local desc = descriptions[name] or "No description available"
        result = result .. "  " .. name .. indicator .. " - " .. desc .. "\n"
    end
    
    return {success = true, message = result}
end)

editor:register_command("set-random-theme", "Switch to a random theme", function(args)
    local themes = editor.theme_manager:theme_names()
    if #themes == 0 then
        return {success = false, message = "No themes available"}
    end
    
    math.randomseed(os.time())
    local random_index = math.random(1, #themes)
    local random_theme = themes[random_index]
    
    local success, message = editor:execute_command("set-theme", {random_theme})
    if success then
        return {success = true, message = "Switched to random theme: " .. random_theme}
    else
        return {success = false, message = "Failed to switch theme: " .. message}
    end
end)

editor:register_command("cycle-themes", "Cycle through available themes", function(args)
    local themes = editor.theme_manager:theme_names()
    if #themes <= 1 then
        return {success = false, message = "Need at least 2 themes to cycle"}
    end
    
    local current_theme = editor.theme_manager.active_theme
    local current_name = current_theme and current_theme:name() or ""
    
    -- Find current theme index
    local current_index = 1
    for i, name in ipairs(themes) do
        if name == current_name then
            current_index = i
            break
        end
    end
    
    -- Switch to next theme (wrap around)
    local next_index = (current_index % #themes) + 1
    local next_theme = themes[next_index]
    
    local success, message = editor:execute_command("set-theme", {next_theme})
    if success then
        return {success = true, message = "Cycled to theme: " .. next_theme}
    else
        return {success = false, message = "Failed to cycle theme: " .. message}
    end
end)

-- Key bindings for theme management
editor:bind_key("C-x t e", switch_to_everforest, "Switch to Everforest theme")
editor:bind_key("C-x t d", switch_to_default, "Switch to default theme")  
editor:bind_key("C-x t v", switch_to_dracula, "Switch to Dracula theme")
editor:bind_key("C-x t s", switch_to_solarized, "Switch to Solarized Dark theme")
editor:bind_key("C-x t n", switch_to_nord, "Switch to Nord theme")
editor:bind_key("C-x t g", switch_to_gruvbox_light, "Switch to Gruvbox Light theme")
editor:bind_key("C-x t l", list_themes, "List all themes")
editor:bind_key("C-x t r", function() editor:execute_command("set-random-theme") end, "Switch to random theme")
editor:bind_key("C-x t c", function() editor:execute_command("cycle-themes") end, "Cycle through themes")

print("Enhanced theme configuration loaded - try C-x t for theme commands or M-x set-theme")