-- doom-modeline.lua
-- ============================================================================
-- Doom Emacs-inspired modeline styling and configuration.
-- Provides enhanced visual styling for the modeline.
-- ============================================================================

local doom_modeline = {}

-- Configuration options
doom_modeline.config = {
    height = 1,                  -- Number of lines (currently fixed to 1)
    bar_width = 4,               -- Width of the colored bar indicator
    icon_type = "unicode",       -- "unicode" or "ascii" (unicode uses special chars)
    show_major_mode_icon = true, -- Show icon for major mode
    show_buffer_encoding = false, -- Show buffer encoding
    show_word_count = false,     -- Show word count for text modes
}

-- Unicode icons for various states (fallback to ASCII if not supported)
doom_modeline.icons = {
    modified = "●",      -- Buffer modified
    readonly = "",      -- Read-only
    saved = "✓",         -- Just saved
    lock = "🔒",         -- Locked

    -- Major mode icons
    lua = "☾",
    python = "🐍",
    c = "C",
    cpp = "C++",
    javascript = "JS",
    typescript = "TS",
    rust = "🦀",
    go = "Go",
    fundamental = "F",

    -- Status indicators
    error = "✗",
    warning = "⚠",
    info = "ℹ",

    -- Git icons (for future use)
    git_branch = "",
    git_added = "+",
    git_modified = "~",
    git_removed = "-",
}

-- ASCII fallbacks
doom_modeline.icons_ascii = {
    modified = "*",
    readonly = "%%",
    saved = "-",
    lock = "#",

    lua = "Lua",
    python = "Py",
    c = "C",
    cpp = "C++",
    javascript = "JS",
    typescript = "TS",
    rust = "Rs",
    go = "Go",
    fundamental = "F",

    error = "!",
    warning = "?",
    info = "i",

    git_branch = "@",
    git_added = "+",
    git_modified = "~",
    git_removed = "-",
}

-- Get icon based on configuration
function doom_modeline.get_icon(name)
    if doom_modeline.config.icon_type == "ascii" then
        return doom_modeline.icons_ascii[name] or name
    end
    return doom_modeline.icons[name] or doom_modeline.icons_ascii[name] or name
end

-- Define doom-modeline faces
function doom_modeline.setup_faces()
    -- Main modeline face (active window)
    lumacs.set_face("doom-modeline", {
        foreground = "#bbc2cf",
        background = "#21242b",
    })

    -- Inactive modeline
    lumacs.set_face("doom-modeline-inactive", {
        foreground = "#5B6268",
        background = "#1c1f24",
    })

    -- Buffer name (emphasized)
    lumacs.set_face("doom-modeline-buffer-file", {
        foreground = "#51afef",
        weight = "bold",
    })

    -- Modified indicator
    lumacs.set_face("doom-modeline-buffer-modified", {
        foreground = "#da8548",
        weight = "bold",
    })

    -- Major mode
    lumacs.set_face("doom-modeline-buffer-major-mode", {
        foreground = "#51afef",
        weight = "bold",
    })

    -- Minor modes
    lumacs.set_face("doom-modeline-buffer-minor-mode", {
        foreground = "#98be65",
    })

    -- Position info
    lumacs.set_face("doom-modeline-buffer-position", {
        foreground = "#bbc2cf",
    })

    -- Error indicator (for flycheck-like features)
    lumacs.set_face("doom-modeline-error", {
        foreground = "#ff6c6b",
        weight = "bold",
    })

    -- Warning indicator
    lumacs.set_face("doom-modeline-warning", {
        foreground = "#ECBE7B",
        weight = "bold",
    })

    -- Info indicator
    lumacs.set_face("doom-modeline-info", {
        foreground = "#98be65",
    })

    -- Project/workspace
    lumacs.set_face("doom-modeline-project", {
        foreground = "#c678dd",
        weight = "bold",
    })

    -- Bar segment (the colored vertical bar)
    lumacs.set_face("doom-modeline-bar", {
        background = "#51afef",
    })

    lumacs.set_face("doom-modeline-bar-inactive", {
        background = "#3f444a",
    })

    -- Evil/modal state faces (for future modal editing support)
    lumacs.set_face("doom-modeline-evil-normal", {
        foreground = "#21242b",
        background = "#51afef",
        weight = "bold",
    })

    lumacs.set_face("doom-modeline-evil-insert", {
        foreground = "#21242b",
        background = "#98be65",
        weight = "bold",
    })

    lumacs.set_face("doom-modeline-evil-visual", {
        foreground = "#21242b",
        background = "#c678dd",
        weight = "bold",
    })

    lumacs.set_face("doom-modeline-evil-emacs", {
        foreground = "#21242b",
        background = "#c678dd",
        weight = "bold",
    })
end

-- Apply doom-modeline styling to standard modeline faces
function doom_modeline.apply_theme()
    -- Apply doom styling to the standard mode-line faces
    lumacs.set_face("mode-line", {
        foreground = "#bbc2cf",
        background = "#21242b",
    })

    lumacs.set_face("mode-line-inactive", {
        foreground = "#5B6268",
        background = "#1c1f24",
    })

    lumacs.set_face("mode-line-buffer-id", {
        foreground = "#51afef",
        weight = "bold",
    })
end

-- Color presets that can be applied
doom_modeline.presets = {
    doom_one = {
        bar = "#51afef",
        bar_inactive = "#3f444a",
        buffer = "#51afef",
        modified = "#da8548",
        mode = "#c678dd",
        position = "#bbc2cf",
        background = "#21242b",
        foreground = "#bbc2cf",
    },

    nord = {
        bar = "#88c0d0",
        bar_inactive = "#3b4252",
        buffer = "#88c0d0",
        modified = "#ebcb8b",
        mode = "#b48ead",
        position = "#d8dee9",
        background = "#2e3440",
        foreground = "#d8dee9",
    },

    dracula = {
        bar = "#bd93f9",
        bar_inactive = "#44475a",
        buffer = "#ff79c6",
        modified = "#ffb86c",
        mode = "#bd93f9",
        position = "#f8f8f2",
        background = "#282a36",
        foreground = "#f8f8f2",
    },

    gruvbox = {
        bar = "#83a598",
        bar_inactive = "#3c3836",
        buffer = "#83a598",
        modified = "#fe8019",
        mode = "#d3869b",
        position = "#ebdbb2",
        background = "#282828",
        foreground = "#ebdbb2",
    },

    solarized = {
        bar = "#268bd2",
        bar_inactive = "#073642",
        buffer = "#268bd2",
        modified = "#cb4b16",
        mode = "#6c71c4",
        position = "#839496",
        background = "#002b36",
        foreground = "#839496",
    },
}

-- Apply a preset color scheme
function doom_modeline.set_preset(preset_name)
    local preset = doom_modeline.presets[preset_name]
    if not preset then
        editor:message("Unknown preset: " .. preset_name, "warning")
        return
    end

    lumacs.set_face("mode-line", {
        foreground = preset.foreground,
        background = preset.background,
    })

    lumacs.set_face("mode-line-inactive", {
        foreground = preset.foreground,
        background = preset.bar_inactive,
    })

    lumacs.set_face("mode-line-buffer-id", {
        foreground = preset.buffer,
        weight = "bold",
    })

    lumacs.set_face("doom-modeline-buffer-modified", {
        foreground = preset.modified,
        weight = "bold",
    })

    lumacs.set_face("doom-modeline-buffer-major-mode", {
        foreground = preset.mode,
        weight = "bold",
    })

    lumacs.set_face("doom-modeline-buffer-position", {
        foreground = preset.position,
    })

    lumacs.set_face("doom-modeline-bar", {
        background = preset.bar,
    })

    lumacs.set_face("doom-modeline-bar-inactive", {
        background = preset.bar_inactive,
    })

    editor:message("Applied doom-modeline preset: " .. preset_name)
end

-- Register commands
editor:register_command("doom-modeline-preset", "Set doom-modeline color preset", function(args)
    if #args == 0 then
        local presets = {}
        for name, _ in pairs(doom_modeline.presets) do
            table.insert(presets, name)
        end
        table.sort(presets)
        return {success = false, message = "Available presets: " .. table.concat(presets, ", ")}
    end

    doom_modeline.set_preset(args[1])
    return {success = true}
end, {"dm-preset"}, true, "s")

-- Setup command to apply all doom-modeline styling
editor:register_command("doom-modeline-setup", "Enable doom-modeline styling", function(args)
    doom_modeline.setup_faces()
    doom_modeline.apply_theme()
    editor:message("Doom modeline styling applied")
    return {success = true}
end, {"dm-setup"})

-- Toggle ASCII/Unicode mode
editor:register_command("doom-modeline-icons", "Toggle doom-modeline icon style", function(args)
    if #args > 0 then
        doom_modeline.config.icon_type = args[1]
    else
        if doom_modeline.config.icon_type == "unicode" then
            doom_modeline.config.icon_type = "ascii"
        else
            doom_modeline.config.icon_type = "unicode"
        end
    end
    editor:message("Icon type: " .. doom_modeline.config.icon_type)
    return {success = true}
end, {"dm-icons"}, true, "s")

-- Store in lumacs namespace
lumacs.doom_modeline = doom_modeline

return doom_modeline
