#
# Neovim home-manager configuration (CoC).
#
# vim: et:ts=2:sw=2:
#
{ config, ... }:
{
  home-manager.users.deprekated =
    { pkgs, ... }:
    let
      colors = config.lib.stylix.colors;
    in
    {

      # CoC configuration for Neovim.
      programs.neovim = {

        # Use LuaLine...
        plugins = with pkgs.vimPlugins; [ lualine-nvim ];

        # ... and insert its config.
        extraLuaConfig = ''
          local lualine = require('lualine')

          local colors = {
            bg       = '#${colors.base00}',
            fg       = '#${colors.base07}',
            red      = '#${colors.base08}';
            green    = '#${colors.base0B}';
            blue     = '#${colors.base0D}';
            yellow   = '#${colors.base0A}';
            magenta  = '#${colors.base0E}';
            orange   = '#${colors.base09}';
            cyan     = '#${colors.base0C}';
            black    = '#${colors.base03}';
            white    = '#${colors.base05}';
          }

          -- auto change color according to neovims mode
          local mode_colors = {
            n = colors.violet,
            i = colors.green,
            v = colors.blue,
            [''] = colors.blue,
            V = colors.blue,
            c = colors.magenta,
            no = colors.violet,
            s = colors.orange,
            S = colors.orange,
            [''] = colors.orange,
            ic = colors.yellow,
            R = colors.red,
            Rv = colors.red,
            cv = colors.red,
            ce = colors.red,
            r = colors.cyan,
            rm = colors.cyan,
            ['r?'] = colors.cyan,
            ['!'] = colors.violet,
            t = colors.violet,
          }

          local conditions = {
            buffer_not_empty = function()
              return vim.fn.empty(vim.fn.expand('%:t')) ~= 1
            end,
            hide_in_width = function()
              return vim.fn.winwidth(0) > 80
            end,
            check_git_workspace = function()
              local filepath = vim.fn.expand('%:p:h')
              local gitdir = vim.fn.finddir('.git', filepath .. ';')
              return gitdir and #gitdir > 0 and #gitdir < #filepath
            end,
          }

          local config = {
            options = {
              icons_enabled = true,
              theme = 'solarized_dark',
              component_separators = "",
              section_separators = { left = '', right = '' },
            },
            sections = {
              lualine_a = {},
              lualine_b = {},
              lualine_c = {},
              lualine_x = {},
              lualine_y = {},
              lualine_z = {},
            },
            inactive_sections = {
              lualine_a = {},
              lualine_b = {},
              lualine_c = {},
              lualine_x = {},
              lualine_y = {},
              lualine_z = {}
            },
          }

          --
          -- Helpers.
          --
          local function ins_far_left(component)
            table.insert(config.sections.lualine_a, component)
          end
          local function ins_left(component)
            table.insert(config.sections.lualine_b, component)
          end
          local function ins_near_left(component)
            table.insert(config.sections.lualine_c, component)
          end
          local function ins_near_right(component)
            table.insert(config.sections.lualine_x, component)
          end
          local function ins_right(component)
            table.insert(config.sections.lualine_y, component)
          end
          local function ins_far_right(component)
            table.insert(config.sections.lualine_z, component)
          end

          --
          -- Left section.
          --

          -- Show the current mode, using a neovim icon.
          ins_far_left {
            function()
              return ' '
            end,
            color = function()
              return { bg = mode_colors[vim.fn.mode()] }
            end,
            padding = { right = 1 },
          }

          -- Show the file's size.
          ins_far_left {
            'filesize',
            cond = conditions.buffer_not_empty,
            color = function()
              return { bg = mode_colors[vim.fn.mode()] }
            end,
            padding = { right = 1 },
          }

          ins_left {
            'filename',
            cond = conditions.buffer_not_empty,
            color = { fg = colors.bg, gui = 'bold' },
          }

          ins_left { 'location' }
          ins_left { 'progress', color = { fg = colors.bg, gui = 'bold' } }

          ins_near_left {
            'diagnostics',
            symbols = { error = ' ', warn = ' ', info = ' ' },
            diagnostics_color = {
              color_error = { fg = colors.red },
              color_warn = { fg = colors.yellow },
              color_info = { fg = colors.cyan },
            },
          }

          --
          -- Center section.
          -- 

          -- Move things to the center using a hack.
          ins_near_left {
            function()
              return '%='
            end,
          }

          ins_near_left {
            -- Lsp server name .
            function()
              local msg = 'no active lsp'
              local buf_ft = vim.api.nvim_buf_get_option(0, 'filetype')
              local clients = vim.lsp.get_active_clients()
              if next(clients) == nil then
                return msg
              end
              for _, client in ipairs(clients) do
                local filetypes = client.config.filetypes
                if filetypes and vim.fn.index(filetypes, buf_ft) ~= -1 then
                  return client.name
                end
              end
              return msg
            end,
            icon = ' lsp:',
            color = { fg = colors.fg, gui = 'bold' },
          }

          --
          -- Right section
          --

          -- Add components to right sections
          ins_right {
            'o:encoding',
            cond = conditions.hide_in_width,
            color = { fg = colors.bg, gui = 'bold' },
          }

          ins_right {
            'fileformat',
            fmt = string.upper,
            color = { fg = colors.bg, gui = 'bold' },
          }

          ins_far_right {
            'branch',
            icon = '',
            color = { fg = colors.violet, gui = 'bold' },
          }

          ins_far_right {
            'diff',
            symbols = { added = ' ', modified = '󰝤 ', removed = ' ' },
            diff_color = {
              added = { fg = colors.violet },
              modified = { fg = colors.bg },
              removed = { fg = colors.yellow },
            },
            cond = conditions.hide_in_width,
          }

          --
          -- Finally, initialize things!
          --
          lualine.setup(config)
        '';
      };
    };
}
