-- Don't copy "c" changes to primary register
vim.keymap.set("n", "c", '"_c', { desc = "Prevent copying to primary register" })

-- Easily open splits
vim.keymap.set("n", "<leader>hs", "<cmd>split<cr>", { desc = "Open horizontal spilt" })
vim.keymap.set("n", "<leader>vs", "<cmd>vsplit<cr>", { desc = "Open vertical spilt" })

-- Easily navigate splits
vim.keymap.set("n", "<A-h>", "<C-w>h", { desc = "Move focus to split left" })
vim.keymap.set("n", "<A-j>", "<C-w>j", { desc = "Move focus to split down" })
vim.keymap.set("n", "<A-k>", "<C-w>k", { desc = "Move focus to split up" })
vim.keymap.set("n", "<A-l>", "<C-w>l", { desc = "Move focus to split right" })

-- Copy the entire file
vim.keymap.set("n", "<leader>y", "ggyG<C-o>", { desc = "Copy the entire file to default register" })

-- Manage sessions
vim.keymap.set("n", "<F5>", function()
	vim.cmd("mksession! ~/.cache//nvim/session/manual_session.vim")
end, { desc = "Manually create session" })
vim.keymap.set("n", "<F6>", function()
	vim.cmd("source ~/.cache/nvim/session/manual_session.vim")
end, { desc = "Restore manually created session" })
vim.keymap.set("n", "<F7>", function()
	vim.cmd("source ~/.cache/nvim/session/shutdown_session.vim")
end, { desc = "Restore session automatically created on VimLeave" })

-- Navigating with guides
vim.keymap.set(
	"n",
	"<leader><leader>",
	'<Esc>/<++><Enter>"_c4l',
	{ desc = "Find next <++> in file and enter insert mode" }
)
vim.keymap.set(
	"v",
	"<leader><leader>",
	'<Esc>/<++><Enter>"_c4l',
	{ desc = "Find next <++> in file and enter insert mode" }
)

-- Spell-check
vim.keymap.set("n", "<leader>o", "<cmd>setlocal spell! spelllang=en_au<CR>", { desc = "Toggle spell check" })

-- Map Q to nop
vim.keymap.set("n", "Q", "<nop>", { desc = "Remap Q to <nop> because Q is the worst" })

-- Keep highlight when indenting
vim.keymap.set("v", "<", "<gv", { desc = "Keep highlight when indenting" })
vim.keymap.set("v", ">", ">gv", { desc = "Keep highlight when indenting" })

-- Easily navigate tabs
vim.keymap.set("n", "<A-1>", "1gt", { desc = "Navigate to tab 2" })
vim.keymap.set("n", "<A-2>", "2gt", { desc = "Navigate to tab 3" })
vim.keymap.set("n", "<A-3>", "3gt", { desc = "Navigate to tab 4" })
vim.keymap.set("n", "<A-4>", "4gt", { desc = "Navigate to tab 5" })
vim.keymap.set("n", "<A-5>", "5gt", { desc = "Navigate to tab 6" })
vim.keymap.set("n", "<A-6>", "6gt", { desc = "Navigate to tab 7" })
vim.keymap.set("n", "<A-7>", "7gt", { desc = "Navigate to tab 8" })
vim.keymap.set("n", "<A-8>", "8gt", { desc = "Navigate to tab 9" })
vim.keymap.set("n", "<A-9>", "9gt", { desc = "Navigate to tab 10" })

-- TODO: Move these into fugitive config
-- vim.keymap.set('n', '<leader>gp', '<cmd>Git push<CR>', options)
-- vim.keymap.set('n', '<leader>gP', '<cmd>Git pull<CR>', options)
-- vim.keymap.set('n', '<leader>ga', '<cmd>Git add .<CR>', options)
-- vim.keymap.set('n', '<leader>gA', '<cmd>Git add<CR>', options)
-- vim.keymap.set('n', '<leader>gc', '<cmd>Git commit<CR>', options)
-- vim.keymap.set('n', '<leader>gC', '<cmd>Git commit -a<CR>', options)

vim.keymap.set("x", "<leader>p", '"_dP', { desc = "Delete into black hole register" })
vim.keymap.set("n", "<leader>d", '"_d', { desc = "Delete into black hole register" })
vim.keymap.set("v", "<leader>d", '"_d', { desc = "Delete into black hole register" })

-- Keep page scroll centered
vim.keymap.set("n", "<C-d>", "<C-d>zz", { desc = "Scroll down half page" })
vim.keymap.set("n", "<C-u>", "<C-u>zz", { desc = "Scroll up half page" })

-- Keep cursor centered
vim.keymap.set("n", "n", "nzzzv", { desc = "Next search result" })
vim.keymap.set("n", "N", "Nzzzv", { desc = "Previous search result" })

-- Keep cursor centered when jumping through quickfix list
vim.keymap.set("n", "<C-k>", "<cmd>cnext<CR>zz", { desc = "Next item in quickfix list" })
vim.keymap.set("n", "<C-j>", "<cmd>cprev<CR>zz", { desc = "Previous item in quickfix list" })
vim.keymap.set("n", "<leader>k", "<cmd>lnext<CR>zz", { desc = "Next item in quickfix list" })
vim.keymap.set("n", "<leader>j", "<cmd>cprev<CR>zz", { desc = "Previous item in quickfix list" })

-- Easily run global search and replace
vim.keymap.set(
	"n",
	"<leader>rr",
	[[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]],
	{ desc = "Search and replace for word under cursor" }
)
