local kmGroup = vim.api.nvim_create_augroup("KeymappingsGroup", { clear = true })
local kmgAutocmd = vim.api.nvim_create_autocmd

------------------------------------------------------------------------------------------
-- core

-- explore the directory of the current file (using netrw)
vim.keymap.set("n", "<leader>n", vim.cmd.Ex)

-- move visually-selected lines up or down
vim.keymap.set("v", "J", ":m '>+1<CR>gv")
vim.keymap.set("v", "K", ":m '<-2<CR>gv")

-- add extra vertical padding for the cursor after half-page jumps
vim.keymap.set("n", "<C-d>", "<C-d>4<C-e>")
vim.keymap.set("n", "<C-u>", "<C-u>4<C-y>")

-- open folds when iterating search results
vim.keymap.set("n", "n", "nzv")
vim.keymap.set("n", "N", "Nzv")

-- maintain cursor position after paragraph formatting
vim.keymap.set("n", "=ap", "mF=ap'F")

-- replace selected text, keep main register
vim.keymap.set("x", "<leader>P", [["_dP]])

-- shortcuts for using + register (system clipboard)
vim.keymap.set({ "n", "v" }, "<leader>y", [["+y]])
vim.keymap.set("n", "<leader>Y", [["+Y]])
vim.keymap.set({ "n", "v" }, "<leader>d", [["+d]])
vim.keymap.set("n", "<leader>D", [["+D]])
vim.keymap.set("n", "<leader>p", [["+p]])

-- search-and-replace shortcuts
vim.keymap.set("n", "<leader>rw", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])
vim.keymap.set("n", "<leader>ra", [[:%s//gc<Left><Left><Left>]])

-- toggle expandtab and show message
vim.keymap.set("n", "<leader>tab", function() ToggleTabsSpaces() end)

-- open scratch/tmp buffers which won't prompt for file-save
vim.keymap.set("n", "<leader>ss", function() TmpBuff("enew") end)
vim.keymap.set("n", "<leader>sl", function() TmpBuff("vnew") end)
vim.keymap.set("n", "<leader>sj", function() TmpBuff("new") end)

-- quicker switching between panes/splits
vim.keymap.set("n", "<C-h>", [[<C-w>h]])
vim.keymap.set("n", "<C-j>", [[<C-w>j]])
vim.keymap.set("n", "<C-k>", [[<C-w>k]])
vim.keymap.set("n", "<C-l>", [[<C-w>l]])
kmgAutocmd('FileType', {
    group = kmGroup,
    pattern = "netrw",
    callback = function()
        vim.keymap.set("n", "<C-h>", [[<C-w>h]], { remap = true, buffer = true })
        vim.keymap.set("n", "<C-j>", [[<C-w>j]], { remap = true, buffer = true })
        vim.keymap.set("n", "<C-k>", [[<C-w>k]], { remap = true, buffer = true })
        vim.keymap.set("n", "<C-l>", [[<C-w>l]], { remap = true, buffer = true })
    end
})

------------------------------------------------------------------------------------------
-- visual/ui-related toggles and shortcuts

vim.keymap.set("n", "<leader>vl", function() ToggleCursorLine() end)
vim.keymap.set("n", "<leader>vc", function() ToggleColorColumn('90') end)
vim.keymap.set("n", "<leader>vn", function() ToggleLineNumbers() end)
vim.keymap.set("n", "<leader>vw", function() ToggleWritingMode() end)

------------------------------------------------------------------------------------------
-- quickfix and location lists
-- ref: :h quickfix or :h location-list
-- NOTE: look at :h setqflist and :h vim.diagnostic.setqflist()

vim.keymap.set("n", "<leader>qo", "<cmd>copen<CR>")
vim.keymap.set("n", "<leader>qc", "<cmd>cclose<CR>")
vim.keymap.set("n", "<leader>qn", "<cmd>cnext<CR>")
vim.keymap.set("n", "<leader>qp", "<cmd>cprev<CR>")
vim.keymap.set("n", "<leader>ln", "<cmd>lnext<CR>")
vim.keymap.set("n", "<leader>lp", "<cmd>lprev<CR>")

------------------------------------------------------------------------------------------
-- debugger and debugging ui

local dap = require("dap")
vim.keymap.set("n", "<leader>bc", dap.continue, { desc = "debug: continue" })
vim.keymap.set("n", "<leader>bi", dap.step_into, { desc = "debug: step into" })
vim.keymap.set("n", "<leader>bo", dap.step_over, { desc = "debug: step over" })
vim.keymap.set("n", "<leader>bu", dap.step_out, { desc = "debug: step out" })
vim.keymap.set("n", "<leader>blb", dap.list_breakpoints, { desc = "debug: list breakpoints" })
vim.keymap.set("n", "<leader>bt", dap.toggle_breakpoint, { desc = "debug: toggle breakpoint" })

vim.keymap.set("n", "<leader>bB", function()
    dap.set_breakpoint(vim.fn.input("breakpoint condition: "))
end, { desc = "debug: set conditional breakpoint" })

vim.keymap.set("n", "<leader>bv", function()
    require("nvim-dap-virtual-text").toggle()
end, { desc = "debug: toggle virtual text" })

vim.keymap.set("n", "<leader>bq", function()
    dap.terminate() require("dapui").close() require("nvim-dap-virtual-text").disable()
end, { desc = "debug: quit debugger" })

-- TODO: decide if i want these or others, add if so
-- vim.keymap.set("n", "<leader>zxcv", dap.repl.open, { desc = "open REPL" })
-- vim.keymap.set("n", "<leader>zxcv", dap.run_last, { desc = "run last" })
-- vim.keymap.set("n", "<leader>zxcv", dap.set_exception_breakpoints({ "all" }), { desc = "set exception breakpoints" })
-- local dapWidgets = require('dap.ui.widgets')
-- vim.keymap.set({'n', 'v'}, '<leader>zxcv', dapWidgets.hover)
-- vim.keymap.set({'n', 'v'}, '<leader>zxcv', dapWidgets.preview)
-- vim.keymap.set('n', '<leader>zxcv', function() dapWidgets.centered_float(dapWidgets.frames) end)
-- vim.keymap.set('n', '<leader>zxcv', function() dapWidgets.centered_float(dapWidgets.scopes) end)

------------------------------------------------------------------------------------------
-- lsp

kmgAutocmd('LspAttach', {
    group = kmGroup,
    callback = function(e)
        local opts = { buffer = e.buf }
        vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
        vim.keymap.set("n", "<leader>lh", function() vim.lsp.buf.hover() end, opts)
        vim.keymap.set("i", "<C-h>", function() vim.lsp.buf.signature_help() end, opts)
        vim.keymap.set("n", "<leader>lv", function() vim.diagnostic.open_float() end, opts)
        vim.keymap.set("n", "<leader>lq", function() vim.diagnostic.setqflist() end, opts)
        -- TODO: learn what the below commands are and if i want to set keymaps for them
        -- vim.keymap.set("n", "<leader>lws", function() vim.lsp.buf.workspace_symbol() end, opts)
        -- vim.keymap.set("n", "<leader>lca", function() vim.lsp.buf.code_action() end, opts)
        -- vim.keymap.set("n", "<leader>lrl", function() vim.lsp.buf.references() end, opts)
        -- vim.keymap.set("n", "<leader>lrn", function() vim.lsp.buf.rename() end, opts)
    end
})

------------------------------------------------------------------------------------------
-- plugins

-- telescope
local tscBuiltin = require('telescope.builtin')
local custom_grep_str_w_regex = function()
    tscBuiltin.grep_string({ search = vim.fn.input("grep > "), use_regex = true, additional_args = "-i", })
end
vim.keymap.set('n', '<leader>ft', tscBuiltin.live_grep, { desc = 'tscope find text, live_grep' })
vim.keymap.set('n', '<leader>fT', custom_grep_str_w_regex, { desc = 'tscope find text, static grep w/regex' })
vim.keymap.set('n', '<leader>ff', tscBuiltin.find_files, { desc = 'tscope find files' })
vim.keymap.set('n', '<leader>fg', tscBuiltin.git_files, { desc = 'tscope find git-tracked files' })
vim.keymap.set('n', '<leader>fb', tscBuiltin.buffers, { desc = 'tscope buffers' })
vim.keymap.set('n', '<leader>fh', tscBuiltin.help_tags, { desc = 'tscope help tags' })
-- TODO: maybe add commands:
--     - find files including git-ignored (that is, have both a yes and no option)
--     - grep files including git-ignored (that is, have both a yes and no option)
--     - grep which supports fuzzy-find, unless performance is horrendous

-- harpoon
local harpoon = require("harpoon")
vim.keymap.set("n", "<leader>hl", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end)
vim.keymap.set("n", "<leader>ha", function() harpoon:list():add() end)
vim.keymap.set("n", "<leader>hA", function() harpoon:list():prepend() end)
vim.keymap.set("n", "<leader>hn", function() harpoon:list():next() end)
vim.keymap.set("n", "<leader>hp", function() harpoon:list():prev() end)
for i = 1, 10, 1 do
    vim.keymap.set("n", "<leader>" .. (i % 10), function() harpoon:list():select(i) end)
    vim.keymap.set("n", "<leader>h" .. (i % 10), function() harpoon:list():replace_at(i) end)
end

--- fugitive (git integration)
vim.keymap.set("n", "<leader>gg", ":Git ") -- shortcut, arbitrary git commands
vim.keymap.set("n", "<leader>ga", function() vim.cmd.Git({ "add %"}) end)
vim.keymap.set("n", "<leader>gl", function() vim.cmd.Git({ "log"}) end)
vim.keymap.set("n", "<leader>gs", function() vim.cmd.Git({ "-p status" }) end)
vim.keymap.set("n", "<leader>gc", function() vim.cmd.Git({ "commit -a" }) end)

-- undotree
vim.keymap.set("n", "<leader>u", vim.cmd.UndotreeToggle)

-- conform (formatter)
vim.keymap.set("n", "<leader>fmt", function()
    require("conform").format({ bufnr = 0 })
end)

-- obisdian / notes
vim.keymap.set("n", "<leader>oo", function() vim.cmd("Obsidian open") end)
vim.keymap.set("n", "<leader>ot", "o- [ ] ")

