-- Lualine configuration with bearded-arc theme colors
-- Supports dark and light variants

local palettes = {
  dark = {
    bg = "#232b3a",
    fg = "#c3cfd9",
    white = "#ABB7C1",
    black = "#1c2433",
    grey = "#444c5b",
    light_grey = "#626a79",
    nord_blue = "#6da4cd",
    cyan = "#22ECDB",
    dark_purple = "#B78AFF",
    green = "#3CEC85",
    red = "#FF738A",
    yellow = "#EACD61",
    orange = "#FF955C",
    blue = "#69C3FF",
    lightbg = "#303847",
  },
  light = {
    bg = "#f0e8df",
    fg = "#3a3228",
    white = "#2a2a2a",
    black = "#faf4ed",
    grey = "#b0a89c",
    light_grey = "#6a6258",
    nord_blue = "#2a6a9a",
    cyan = "#147a8a",
    dark_purple = "#7b4fc4",
    green = "#1e9b52",
    red = "#d1344f",
    yellow = "#b8890f",
    orange = "#c45a1c",
    blue = "#1a7db5",
    lightbg = "#e8e0d4",
  },
}

local function build_theme(colors)
  return {
    normal = {
      a = { bg = colors.nord_blue, fg = colors.black, gui = "bold" },
      b = { bg = colors.lightbg, fg = colors.white },
      c = { bg = colors.bg, fg = colors.fg },
    },
    insert = {
      a = { bg = colors.dark_purple, fg = colors.black, gui = "bold" },
      b = { bg = colors.lightbg, fg = colors.white },
    },
    visual = {
      a = { bg = colors.cyan, fg = colors.black, gui = "bold" },
      b = { bg = colors.lightbg, fg = colors.white },
    },
    replace = {
      a = { bg = colors.red, fg = colors.black, gui = "bold" },
      b = { bg = colors.lightbg, fg = colors.white },
    },
    command = {
      a = { bg = colors.orange, fg = colors.black, gui = "bold" },
      b = { bg = colors.lightbg, fg = colors.white },
    },
    terminal = {
      a = { bg = colors.green, fg = colors.black, gui = "bold" },
      b = { bg = colors.lightbg, fg = colors.white },
    },
    inactive = {
      a = { bg = colors.bg, fg = colors.grey },
      b = { bg = colors.bg, fg = colors.grey },
      c = { bg = colors.bg, fg = colors.grey },
    },
  }
end

-- Custom component for LSP status
local function lsp_status()
  local clients = vim.lsp.get_clients({ bufnr = 0 })
  if #clients == 0 then
    return ""
  end

  local client_names = {}
  for _, client in ipairs(clients) do
    table.insert(client_names, client.name)
  end

  return "  " .. table.concat(client_names, ", ")
end

-- Custom component for LSP diagnostics count
local function lsp_diagnostics()
  local error_count = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR })
  local warn_count = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.WARN })
  local info_count = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.INFO })
  local hint_count = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.HINT })

  local parts = {}
  if error_count > 0 then
    table.insert(parts, string.format("%%#DiagnosticError# %d", error_count))
  end
  if warn_count > 0 then
    table.insert(parts, string.format("%%#DiagnosticWarn# %d", warn_count))
  end
  if info_count > 0 then
    table.insert(parts, string.format("%%#DiagnosticInfo# %d", info_count))
  end
  if hint_count > 0 then
    table.insert(parts, string.format("%%#DiagnosticHint# %d", hint_count))
  end

  return table.concat(parts, " ")
end

-- Custom component for git status
local function git_status()
  local gitsigns = vim.b.gitsigns_status_dict
  if not gitsigns then
    return ""
  end

  local parts = {}
  if gitsigns.added and gitsigns.added > 0 then
    table.insert(parts, string.format("%%#GitSignsAdd#+%d", gitsigns.added))
  end
  if gitsigns.changed and gitsigns.changed > 0 then
    table.insert(parts, string.format("%%#GitSignsChange#~%d", gitsigns.changed))
  end
  if gitsigns.removed and gitsigns.removed > 0 then
    table.insert(parts, string.format("%%#GitSignsDelete#-%d", gitsigns.removed))
  end

  return table.concat(parts, " ")
end

local M = {}

function M.setup()
  local colors = palettes[vim.o.background] or palettes.dark
  local theme = build_theme(colors)

  require("lualine").setup({
    options = {
      theme = theme,
      component_separators = { left = "", right = "" },
      section_separators = { left = "", right = "" },
      disabled_filetypes = {
        statusline = { "alpha", "dashboard", "NvimTree", "neo-tree" },
        winbar = {},
      },
      ignore_focus = {},
      always_divide_middle = true,
      globalstatus = true,
      refresh = {
        statusline = 1000,
        tabline = 1000,
        winbar = 1000,
      },
    },
    sections = {
      lualine_a = {
        {
          "mode",
          fmt = function(str)
            return str:sub(1, 1)
          end,
        },
      },
      lualine_b = {
        {
          "branch",
          icon = "",
          color = { fg = colors.light_grey },
        },
        {
          git_status,
          color = { bg = colors.lightbg },
        },
      },
      lualine_c = {
        {
          "filename",
          path = 1,
          symbols = {
            modified = " ",
            readonly = " ",
            unnamed = "[No Name]",
            newfile = " ",
          },
          color = { bg = colors.lightbg, fg = colors.white },
        },
        {
          function()
            return require("nvim-navic").get_location()
          end,
          cond = function()
            return package.loaded["nvim-navic"] and require("nvim-navic").is_available()
          end,
          color = { bg = colors.bg, fg = colors.light_grey },
        },
        {
          lsp_diagnostics,
          color = { bg = colors.bg },
        },
      },
      lualine_x = {
        {
          lsp_status,
          color = { fg = colors.nord_blue, bg = colors.bg },
        },
        {
          "filetype",
          colored = true,
          icon_only = false,
          icon = { align = "right" },
        },
      },
      lualine_y = {
        {
          "progress",
          color = { bg = colors.lightbg, fg = colors.white },
        },
      },
      lualine_z = {
        {
          "location",
          icon = "",
          color = { bg = colors.green, fg = colors.black },
        },
      },
    },
    inactive_sections = {
      lualine_a = {},
      lualine_b = {},
      lualine_c = { "filename" },
      lualine_x = { "location" },
      lualine_y = {},
      lualine_z = {},
    },
    tabline = {},
    winbar = {},
    inactive_winbar = {},
    extensions = { "lazy", "mason" },
  })
end

M.setup()

return M
