local M = {
  "neovim/nvim-lspconfig",
  lazy = false,
  dependencies = {
    { "mason-org/mason.nvim", version = "1.11.0" },
    { "mason-org/mason-lspconfig.nvim", version = "1.32.0" },
    { "saghen/blink.cmp" },
  },
}

function M.on_attach(client, bufnr)
  -- Enable completion triggered by <c-x><c-o>
  vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings.
  -- See `:help vim.lsp.*` for documentation on any of the below functions
  local bufopts = { noremap=true, silent=true, buffer=bufnr }
  vim.keymap.set('n', '<space>f', function() vim.lsp.buf.format { async = true } end, bufopts)
end

function M.config()
  local lspconfig = require("lspconfig")
  local mason_lspconfig = require("mason-lspconfig")
  local capabilities = require('blink.cmp').get_lsp_capabilities(vim.lsp.protocol.make_client_capabilities())
  capabilities.textDocument.completion.completionItem.snippetSupport = true
  capabilities.textDocument.foldingRange = {
    dynamicRegistration = false,
    lineFoldingOnly = true
  }
  vim.diagnostic.config({
    virtual_text = false,
  })


  -- Diagnostics symbols for display in the sign column.
  local signs = { Error = "", Warn = "", Hint = "", Info = "" }
  for type, icon in pairs(signs) do
    local hl = "DiagnosticSign" .. type
    vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
  end

  require("mason").setup({
    ui = {
      border = {"┏", "━", "┓", "┃", "┛", "━", "┗", "┃"},
      icons = {
        package_installed = "✓",
        package_pending = "➜",
        package_uninstalled = "✗"
      }
    }
  })
  mason_lspconfig.setup {
    ensure_installed = {
      'html',
      'ts_ls',
      'solargraph',
      'cssls',
      'dockerls',
      'jsonls',
      'vimls',
      -- 'rust_analyzer',
      'clangd',
      'pyright',
      'bashls',
    },
  }
  mason_lspconfig.setup_handlers {
    -- The first entry (without a key) will be the default handler
    -- and will be called for each installed server that doesn't have
    -- a dedicated handler.
    function (server_name) -- default handler (optional)
      lspconfig[server_name].setup {
        on_attach = M.on_attach,
        capabilities,
      }
    end,
    ["html"] = function()
      lspconfig.html.setup({
        on_attach = M.on_attach,
        capabilities,
        filetypes = { "html" }
      })
    end,
    ["solargraph"] = function()
      lspconfig.solargraph.setup({
        on_attach = M.on_attach,
        capabilities,
        filetypes = { "ruby", "eruby" },
        settings = {
          solargraph = {
            useBundler = true,
            diagnostic = true,
            completion = true,
            hover = true,
            formatting = true,
            symbols = true,
            definitions = true,
            rename = true,
            references = true,
            folding = true
          }
        }
      })
    end,
    -- ["rust_analyzer"] = function(server_name)
    --   lspconfig.rust_analyzer.setup({
    --     on_attach = M.on_attach,
    --     capabilities,
    --     settings = {
    --       ["rust-analyzer"] = {
    --         assist = {
    --           importMergeBehavior = "last",
    --           importPrefix = "by_self",
    --         },
    --         diagnostics = {
    --           disabled = { "unresolved-import" }
    --         },
    --         cargo = {
    --           loadOutDirsFromCheck = true
    --         },
    --         procMacro = {
    --           enable = true
    --         },
    --         checkOnSave = {
    --           command = "clippy"
    --         },
    --       }
    --     }
    --   })
    -- end,
    ["jsonls"] = function(server_name)
      lspconfig.jsonls.setup({
        on_attach = M.on_attach,
        capabilities,
        commands = {
          Format = {
            function()
              vim.lsp.buf.range_formatting({},{0,0},{vim.fn.line("$"),0})
            end
          }
        }
      })
    end,
  }
  -- require("ufo").setup()
end

return M
