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

-- the leader key is <space>
wk.add({
    -- Telescope
    { "<leader>f", group = "Telescope" },
    { "<leader>ff", telescope.find_files, desc = "Find files", mode = "n" },
    { "<leader>fg", telescope.live_grep, desc = "Live grep", mode ="n"},
    { "<leader>fb", telescope.buffers, desc = "Find in buffers", mode ="n"},
    { "<leader>fc", telescope.current_buffer_fuzzy_find, desc = "Find in current buffer", mode ="n"},
    { "<leader>fh", telescope.help_tags, desc = "Find in help tags", mode ="n"},
    { "<leader>fm", telescope.marks, desc = "Find in marks", mode ="n"},
    { "<leader>fr", telescope.resume, desc = "Resume last find", mode ="n"},
    -- LspSaga
    { "<leader>l", group = "Lsp" },
    { "<leader>lc", "<cmd>Lspsaga code_action<cr>", desc = "Code Action", mode ="n"},
    { "<leader>lo", "<cmd>Lspsaga outline<cr>", desc = "Outline", mode ="n"},
    { "<leader>lr", "<cmd>Lspsaga rename<cr>", desc = "Rename", mode ="n"},
    { "<leader>ld", "<cmd>Lspsaga goto_definition<cr>", desc = "Lsp GoTo Definition", mode ="n"},
    { "<leader>lf", "<cmd>Lspsaga finder<cr>", desc = "Lsp Finder", mode ="n"},
    { "<leader>lp", "<cmd>Lspsaga peek_definition<cr>", desc = "Preview Definition", mode ="n"},
    { "<leader>lw", "<cmd>Lspsaga show_workspace_diagnostics<cr>", desc = "Show Workspace Diagnostics", mode ="n"},
    { "<leader>lb", "<cmd>Lspsaga show_buf_diagnostics<cr>", desc = "Show Buffer Diagnostics", mode ="n"},
    { "<leader>lm", "<cmd>TermExec cmd='lmsfixfile %:p:h' direction='float'<cr>", desc = "LMS fixer this file", mode ="n"},
    -- UndoTree
    { "<leader>u", group = "UndoTree" },
    { "<leader>uu", "<cmd>lua require('undotree').toggle()<cr>", desc="Toggle undo tree", mode ="n"},
    -- TagBar 
    { "<leader><F8>", "<cmd>:TagbarToggle fj<cr>", desc="Toggle tagbar", mode ="n"},
    -- Custom
    { "<leader>n", "<cmd>:noh<cr>", desc="Remove highlight", mode = "n"},
    -- Jump to definition in new tab
    { "<leader><F5>", "<cmd>tab split | lua vim.lsp.buf.definition()<cr>", desc="Definition in new tab", mode = "n"},
    -- See error in diagnostic window
    { "<leader>e", "<cmd>lua vim.diagnostic.open_float()<CR>", desc="See diagnostic", mode = "n"},
    -- Closing stuff
    { "<leader>Z", group = "Closing"},
    { "<leader>ZZ", "<cmd>wq!<cr>", desc = "Write en close", mode = "n"},
    { "<leader>ZQ", "<cmd>q!<cr>", desc = "Close without write", mode = "n"},
    -- Creating and browsing marks
    { "<leader>m", group = "Create / Jump mark"},
    { "<leader>mx", marks.set, desc = "Set mark [x]", mode = "n"},
    { "<leader>m,", marks.set_next, desc = "Set the next available alphabetical (lowercase) mark", mode = "n"},
    { "<leader>m;", marks.toggle, desc = "Toggle the next available mark at the current line", mode = "n"},
    { "<leader>m]", marks.next, desc = "Move to next mark", mode = "n"},
    { "<leader>m[", marks.prev, desc = "Move to previous mark", mode = "n"},
    { "<leader>m:", marks.preview, desc = "Preview mark. This will prompt you for a specific mark to preview; press <cr> to preview the next mark.", mode = "n"},
    { "<leader>m[0-9]", marks.set_bookmark, desc = "Add a bookmark from bookmark group[0-9].", mode = "n"},
    { "<leader>m}", marks.next_bookmark, desc = "Move to the next bookmark having the same type as the bookmark under the cursor. Works across buffers.", mode = "n"},
    { "<leader>m{", marks.prev_bookmark, desc = "Move to the previous bookmark having the same type as the bookmark under the cursor. Works across buffers.", mode = "n"},
    -- Deleting marks
    { "<leader>s", group = "Delete marks"},
    { "<leader>dmx", marks.delete, desc = "Delete mark x", mode = "n"},
    { "<leader>dm-", marks.delete_line, desc = "Delete all marks on the current line", mode = "n"},
    { "<leader>dm<space>", marks.delete_buf, desc = "Delete all marks in the current buffer", mode = "n"},
    { "<leader>dm=", marks.delete_bookmark, desc = "Delete the bookmark under the cursor.", mode = "n"},
    { "<leader>dm[0-9]", marks.delete_bookmark, desc = "Delete all bookmarks from bookmark group[0-9].", mode = "n"},
    -- Git
    { "<leader>h", group = "Git"},
    -- Tabs
    { "<leader>t", group = "Tabs"},
    { "<leader>tf", "<cmd>tabm 0<cr>", desc = "Move before first tab", mode = "n"},
    { "<leader>tl", "<cmd>tabm<cr>", desc = "Move after last tab", mode = "n"},
    { "<leader>t,", "<cmd>tabm -<cr>", desc = "Move tab to left", mode = "n"},
    { "<leader>t.", "<cmd>tabm +<cr>", desc = "Move tab to right", mode = "n"},
    { "<leader>tn", "<cmd>tabnext<cr>", desc = "Go to next tab", mode = "n"},
    { "<leader>tp", "<cmd>tabprevious<cr>", desc = "Go to previous tab", mode = "n"},
    { "<leader>tb", "<cmd>tabfirst<cr>", desc = "Go to first tab", mode = "n"},
    { "<leader>te", "<cmd>tablast<cr>", desc = "Go to last tab", mode = "n"},
    { "<leader>to", "<cmd>tabonly<cr>", desc = "Close all except current", mode = "n"},
    -- Buffer
    { "<leader>b", group = "Buffers"},
    { "<leader>bd", "<cmd>Bufdel<cr>", desc = "Close open buffers", mode = "n"},
    -- Indent
    { "<leader>i", group = "Indentation"},
    { "<leader>it", "<cmd>IBLToggle<cr>", desc = "Toggle indentation guides", mode = "n"},
    { "<leader>im", "<cmd>set list!<cr>", desc = "Toggle indentation characters", mode = "n"},
    { "<leader>ia", "<cmd>set list!<cr><cmd>IBLToggle<cr>", desc = "Toggle all", mode = "n"},
    -- Navigation
    { "<leader>c", group = "Quick change directories"},
    { "<leader>cr", "<cmd>cd ~/lms/<cr>", desc = "Change to lms root", mode = "n"},
    { "<leader>cc", "<cmd>cd ~/lms/App/Lms/<cr>", desc = "Change to App/Lms", mode = "n"},
    { "<leader>ct", "<cmd>cd ~/lms/Tests/<cr>", desc = "Change to Tests", mode = "n"},
    -- Commenting
    { "<leader>g", group = "Commenting"},
    { "<leader>gcc", desc = "Toggles the current line using linewise comment", mode = "n"},
    { "<leader>gbc", desc = "Toggles the current line using blockwise comment", mode = "n"},
    { "<leader>gc", desc = "Toggles the region using linewise comment", mode = "v"},
    { "<leader>gb", desc = "Toggles the region using blockwise comment", mode = "v"},
    { "<leader>gb2}", desc = "Toggle until the 2 next blank line", mode = "n"},
    { "<leader>gbaf", desc = "Toggle comment around a function (w/ LSP/treesitter support)", mode = "n"},
    { "<leader>gbac", desc = "Toggle comment around a class (w/ LSP/treesitter support)", mode = "n"},
})

-- prevent using arrow keys or mouse
vim.keymap.set('n', '<Left>', ':echoe "Yo retard use h"<CR>')
vim.keymap.set('n', '<Right>', ':echoe "Yo retard use l"<CR>')
vim.keymap.set('n', '<Up>', ':echoe "Yo retard use k"<CR>')
vim.keymap.set('n', '<Down>', ':echoe "Yo retard use j"<CR>')
vim.opt.mouse = ''

-- dont automatically jump when searching
vim.keymap.set('n', '*', '*```<CR>')

-- show lsp floating tooltip
vim.keymap.set('n', '<c-k>', ':lua vim.lsp.buf.hover()<CR>')


-- tab navigation using <TAB>
vim.keymap.set('n', '<Tab>', ':BufferLineCycleNext<CR>')
vim.keymap.set('n', '<S-Tab>', ':BufferLineCyclePrev<CR>')
-- quick close tab USE <leader>ZQ
--vim.keymap.set('n', '<C-w>', ':tabclose<CR>')

-- jump to definition in new tab
vim.keymap.set('n', '<C-]>', '<cmd>tab split | lua vim.lsp.buf.definition()<CR>', {})

-- change split window
vim.keymap.set('n', '<C-k>', ':wincmd k<CR>')
vim.keymap.set('n', '<C-j>', ':wincmd j<CR>')
vim.keymap.set('n', '<C-h>', ':wincmd h<CR>')
vim.keymap.set('n', '<C-l>', ':wincmd l<CR>')

-- move split to tab
vim.keymap.set('n', '<C-m>', ':tab split<CR>')

-- open context menu
vim.keymap.set("n", "<C-t>", function()
  require("menu").open("default")
end, {})
