local M = {
  "folke/which-key.nvim",
  event = "VeryLazy",
}

function M.config()
  local mappings = {
    ["q"] = { "<cmd>confirm q<CR>", "Quit" },
    ["/"] = { "<Plug>(comment_toggle_linewise_current)", "Comment" },
    ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },
    M = {
      name = "Scala Metals",
      i = { "<cmd>:MetalsInfo<cr>", "Metals [i]nfo" },
      r = { "<cmd>:MetalsRestartBuildServer<cr>", "[r]estart Build Server" },
      d = { "<cmd>:MetalsRunDoctor<cr>", "Run [d]octor" },
      f = { "<cmd>:MetalsRunScalafix<cr>", "Run Scala[f]ix" },
      l = { "<cmd>:MetalsToggleLogs<cr>", "Toggle [l]ogs" },
      o = { "<cmd>:MetalsOrganizeImports<cr>", "[O]rganize imports" },
      c = { "<cmd>:MetalsCompileCascade<cr>", "[C]ompile cascade" },
    },
    s = {
      name = "Spelling",
      s = { "<cmd>:set spell<cr>", "[s]tart spelling" },
      q = { "<cmd>:set spell!<cr>", "[q]uit spelling" },
    },
    j = {
      name = "Jump to",
      D = { "<cmd>lua vim.lsp.buf.definition()<cr><cmd>zz<cr>", "Jump to [D]efinition" },
      d = { "<cmd>lua vim.lsp.buf.declaration()<cr><cmd>zz<cr>", "Jump to [d]eclaration" },
      i = { "<cmd>lua vim.lsp.buf.implementation()<cr>", "Jump to [i]mplementations" },
      r = { "<cmd>lua vim.lsp.buf.references()<cr>", "Jump to [r]eferences" },
      h = { "<cmd>lua vim.lsp.buf.signature_help()<cr>", "Signature [h]elp" },
      s = { "<cmd>lua vim.lsp.buf.document_symbol()<cr>", "Document [s]ymbols" },
      S = { "<cmd>lua vim.lsp.buf.workspace_symbol()<cr>", "Workspace [s]ymbols" },
    },
    w = {
      name = "Windows",
      h = { "<cmd>:sp<cr>", "Split [H]orizontally" },
      v = { "<cmd>:vsplit<cr>", "Split [V]ertically" },
      f = { "<cmd>:only<cr>", "[F]ocus current" },
      d = { "<cmd>:q<cr>", "[D]elete current" },
    },
    b = {
      name = "Buffers",
      b = { "<cmd>Telescope buffers previewer=false<cr>", "Find [B]uffer" },
      p = { "<cmd>:e #<cr>", "Open [P]revious" },
      d = { "<cmd>:bdelete<cr>", "[d]elete buffer" },
      D = { "<cmd>:%bd|e#|bd#<cr>", "[D]elete other Buffers" },
      S = { "<cmd>:wa<cr>", "[S]ave all Buffers" },
    },
    z = {
      name = "Folds",
      c = { "zc", "[c]lose fold" },
      o = { "zo", "[o]pen fold" },
      C = { "zC", "[C]lose All fold" },
      O = { "zO", "[O]pen All fold" },
    },
    Z = {
      name = "La[z]y",
      i = { "<cmd>Lazy install<cr>", "Install" },
      s = { "<cmd>Lazy sync<cr>", "Sync" },
      S = { "<cmd>Lazy clear<cr>", "Status" },
      c = { "<cmd>Lazy clean<cr>", "Clean" },
      u = { "<cmd>Lazy update<cr>", "Update" },
      p = { "<cmd>Lazy profile<cr>", "Profile" },
      l = { "<cmd>Lazy log<cr>", "Log" },
      d = { "<cmd>Lazy debug<cr>", "Debug" },
    },
    f = {
      name = "Find",
      b = { "<cmd>Telescope git_branches<cr>", "Checkout branch" },
      c = { "<cmd>Telescope colorscheme<cr>", "Colorscheme" },
      f = { "<cmd>Telescope find_files<cr>", "Find files" },
      p = { "<cmd>lua require('telescope').extensions.projects.projects()<cr>", "Projects" },
      t = { "<cmd>Telescope live_grep<cr>", "Find Text" },
      s = { "<cmd>Telescope grep_string<cr>", "Find String" },
      h = { "<cmd>Telescope help_tags<cr>", "Help" },
      H = { "<cmd>Telescope highlights<cr>", "Highlights" },
      i = { "<cmd>lua require('telescope').extensions.media_files.media_files()<cr>", "Media" },
      l = { "<cmd>Telescope resume<cr>", "Last Search" },
      M = { "<cmd>Telescope man_pages<cr>", "Man Pages" },
      r = { "<cmd>Telescope oldfiles<cr>", "Recent File" },
      R = { "<cmd>Telescope registers<cr>", "Registers" },
      k = { "<cmd>Telescope keymaps<cr>", "Keymaps" },
      C = { "<cmd>Telescope commands<cr>", "Commands" },
      m = { "<cmd>:Telescope metals commands<cr>", "Show [M]etals commands" },
    },
    l = {
      name = "LSP",
      a = { "<cmd>lua vim.lsp.buf.code_action()<cr>", "Code Action" },
      d = { "<cmd>Telescope diagnostics bufnr=0 theme=get_ivy<cr>", "Buffer Diagnostics" },
      w = { "<cmd>Telescope diagnostics<cr>", "Diagnostics" },
      f = { "<cmd>lua require('conform').format()<cr>", "Format" },
      i = { "<cmd>LspInfo<cr>", "Info" },
      R = { "<cmd>LspRestart<cr>", "Resart" },
      I = { "<cmd>Mason<cr>", "Mason Info" },
      j = {
        "<cmd>lua vim.diagnostic.goto_next()<cr>",
        "Next Diagnostic",
      },
      k = {
        "<cmd>lua vim.diagnostic.goto_prev()<cr>",
        "Prev Diagnostic",
      },
      l = { "<cmd>lua vim.lsp.codelens.run()<cr>", "CodeLens Action" },
      q = { "<cmd>lua vim.diagnostic.setloclist()<cr>", "Quickfix" },
      r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename" },
      s = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" },
      S = {
        "<cmd>Telescope lsp_dynamic_workspace_symbols<cr>",
        "Workspace Symbols",
      },
      e = { "<cmd>Telescope quickfix<cr>", "Telescope Quickfix" },
    },

    --     t = {
    --       name = "Tab",
    --       t = {
    --         "<cmd>lua require('telescope').extensions['telescope-tabs'].list_tabs(require('telescope.themes').get_dropdown{previewer = false, initial_mode='normal', prompt_title='Tabs'})<cr>",
    --         "Find Tab",
    --       },
    --       a = { "<cmd>$tabnew<cr>", "New Empty Tab" },
    --       A = { "<cmd>tabnew %<cr>", "New Tab" },
    --       n = { "<cmd>tabn<cr>", "Next" },
    --       o = { "<cmd>tabonly<cr>", "Only" },
    --       p = { "<cmd>tabp<cr>", "Prev" },
    --       h = { "<cmd>-tabmove<cr>", "Move Left" },
    --       l = { "<cmd>+tabmove<cr>", "Move Right" },
    --     },

    T = {
      name = "Treesitter",
      i = { ":TSConfigInfo<cr>", "Info" },
    },
  }

  local opts = {
    mode = "n", -- NORMAL mode
    prefix = "<leader>",
    buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
    silent = true, -- use `silent` when creating keymaps
    noremap = true, -- use `noremap` when creating keymaps
    nowait = true, -- use `nowait` when creating keymaps
  }

  -- NOTE: Prefer using : over <cmd> as the latter avoids going back in normal-mode.
  -- see https://neovim.io/doc/user/map.html#:map-cmd
  local vmappings = {
    ["/"] = { "<Plug>(comment_toggle_linewise_visual)", "Comment toggle linewise (visual)" },
    l = {
      name = "LSP",
      a = { "<cmd>lua vim.lsp.buf.code_action()<cr>", "Code Action" },
    },
  }

  local vopts = {
    mode = "v", -- VISUAL mode
    prefix = "<leader>",
    buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
    silent = true, -- use `silent` when creating keymaps
    noremap = true, -- use `noremap` when creating keymaps
    nowait = true, -- use `nowait` when creating keymaps
  }

  local which_key = require "which-key"

  which_key.setup {
    plugins = {
      marks = false, -- shows a list of your marks on ' and `
      registers = false, -- shows your registers on " in NORMAL or <C-r> in INSERT mode
      spelling = {
        enabled = true,
        suggestions = 20,
      }, -- use which-key for spelling hints
      -- the presets plugin, adds help for a bunch of default keybindings in Neovim
      -- No actual key bindings are created
      presets = {
        operators = false, -- adds help for operators like d, y, ...
        motions = false, -- adds help for motions
        text_objects = false, -- help for text objects triggered after entering an operator
        windows = false, -- default bindings on <c-w>
        nav = false, -- misc bindings to work with windows
        z = false, -- bindings for folds, spelling and others prefixed with z
        g = false, -- bindings for prefixed with g
      },
    },
    popup_mappings = {
      scroll_down = "<c-d>", -- binding to scroll down inside the popup
      scroll_up = "<c-u>", -- binding to scroll up inside the popup
    },
    window = {
      border = "rounded", -- none, single, double, shadow
      position = "bottom", -- bottom, top
      margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left]
      padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left]
      winblend = 0,
    },
    layout = {
      height = { min = 4, max = 25 }, -- min and max height of the columns
      width = { min = 20, max = 50 }, -- min and max width of the columns
      spacing = 3, -- spacing between columns
      align = "left", -- align columns left, center or right
    },
    ignore_missing = true, -- enable this to hide mappings for which you didn't specify a label
    hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "call", "lua", "^:", "^ " }, -- hide mapping boilerplate
    show_help = true, -- show help message on the command line when the popup is visible
    show_keys = true, -- show the currently pressed key and its label as a message in the command line
    triggers = "auto", -- automatically setup triggers
    -- triggers = {"<leader>"} -- or specify a list manually
    triggers_blacklist = {
      -- list of mode / prefixes that should never be hooked by WhichKey
      -- this is mostly relevant for key maps that start with a native binding
      -- most people should not need to change this
      i = { "j", "k" },
      v = { "j", "k" },
    },
    -- disable the WhichKey popup for certain buf types and file types.
    -- Disabled by default for Telescope
    disable = {
      buftypes = {},
      filetypes = { "TelescopePrompt" },
    },
  }

  which_key.register(mappings, opts)
  which_key.register(vmappings, vopts)
end

return M
