local telescope = require('telescope.builtin')
local wk = require('which-key')

vim.keymap.set('n', '<leader>f', telescope.find_files, { desc = "File picker" })
vim.keymap.set('n', '<leader>g', telescope.live_grep, { desc = "Grep recursively" })
vim.keymap.set('n', '<leader>b', telescope.buffers, { desc = "Buffer picker" })
vim.keymap.set('n', '<leader>?', telescope.help_tags, { desc = "Help tags picker" })
vim.keymap.set('n', '<leader>s', telescope.treesitter, { desc = "Treesitter symbol picker" })
vim.keymap.set('n', '<leader>u', ":TodoTelescope<Enter>", { desc = "Grep for TODOs" })
vim.keymap.set('n', 'U', ":redo<Enter>", {})
vim.keymap.set('n', '<leader>p', ":b#<Enter>", { desc = "Go to last buffer" })

-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    vim.keymap.set('n', '<leader>ld', telescope.lsp_definitions, { desc = "Show definitons", buffer = ev.buf })
    vim.keymap.set('n', '<leader>lr', telescope.lsp_references, { desc = "Show references", buffer = ev.buf })
    vim.keymap.set('n', '<leader>li', telescope.lsp_implementations, { desc = "Show implementations", buffer = ev.buf })
    vim.keymap.set('n', '<leader>lt', telescope.lsp_type_definitions, { desc = "Show type definitions", buffer = ev.buf })
    vim.keymap.set('n', '<leader>le', telescope.diagnostics, { desc = "Show diagnostics", buffer = ev.buf })
    vim.keymap.set('n', '<leader>ls', telescope.lsp_workspace_symbols, { desc = "Show workspace symbols", buffer = ev.buf })
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = ev.buf })
    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, { buffer = ev.buf })
    vim.keymap.set('n', '<leader>wa', vim.lsp.buf.add_workspace_folder, { desc = "Add workspace folder", buffer = ev.buf })
    vim.keymap.set('n', '<leader>wr', vim.lsp.buf.remove_workspace_folder, { desc = "Remove workspace folder", buffer = ev.buf })
    vim.keymap.set('n', '<leader>wl', function()
      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, { desc = "List workspace folders", buffer = ev.buf })
    vim.keymap.set('n', '<leader>r', vim.lsp.buf.rename, { desc = "Rename symbol", buffer = ev.buf })
    vim.keymap.set('n', "<leader>a", vim.lsp.buf.code_action, { desc = "Code Action", buffer = ev.buf })

    wk.add({
      { "<leader>l", desc = "Language server stuff" },
      { "<leader>w", desc = "Workspace stuff" },
    })

  end,
})

--local rust_tools = require("rust-tools")

--rust_tools.setup({
--  server = {
--    on_attach = function(_, bufnr)
--      -- Hover actions
--      vim.keymap.set("n", "<C-space>", rust_tools.hover_actions.hover_actions, { buffer = bufnr })
--      -- Code action groups
--      vim.keymap.set("n", "<leader>a", rust_tools.code_action_group.code_action_group, { desc = "Code actions", buffer = bufnr })
--    end,
--    settings = {
--      ["rust-analyzer"] = {},
--    }
--  },
--})
