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

-- local tree = require "nvim-tree.api"

function FormatBuffer(command)
	-- Save the current cursor position
	local cursor_position = vim.fn.getpos(".")

	-- Run the formatter command on the current buffer
	vim.cmd(":%! " .. command)

	-- Restore the cursor position
	vim.fn.setpos(".", cursor_position)
end

function ToggleTree(source)
	require("neo-tree.command").execute({
		toggle = true,
		source = source,
		position = "left",
	})
end

wk.add({
	{
		mode = "n",
		-- telescope
		{
			"<leader>ff",
			function()
				telescope.find_files()
			end,
			desc = "Find file",
		},
		{
			"<leader>fg",
			function()
				telescope.live_grep()
			end,
			desc = "Live grep",
		},
		{
			"<leader>fb",
			function()
				telescope.buffers()
			end,
			desc = "List buffers",
		},
		{
			"<leader>fh",
			function()
				telescope.help_tags()
			end,
			desc = "Help",
		},
		{
			"<leader>fs",
			function()
				telescope.lsp_document_symbols()
			end,
			desc = "Find symbols",
		},
		{
			"zz",
			function()
				telescope.spell_suggest()
			end,
			desc = "Suggest spell",
		},

		{
			"<leader>ft",
			function()
				FormatBuffer("typstfmt")
			end,
			desc = "Typst fmt",
		},

		-- Centering cursor
		{ "n", "nzzzv", desc = "Center cursor when n (next) during search" },
		{ "N", "Nzzzv", desc = "Center cursor when n (next) during search" },

		-- tab
		{ "<tab>", ":BufferLineCycleNext <CR>", desc = "Next tab" },
		{ "<S-tab>", ":BufferLineCyclePrev<CR>", desc = "Previous tab" },

		-- Comment
		{ "<leader>/", "gcc", desc = "comment toggle", remap = true },

		-- Format
		{
			"<leader>fm",
			function()
				require("conform").format({ lsp_fallback = true })
			end,
			desc = "format files",
		},

		-- nvim-tree
		-- { "<C-n>", function() tree.tree.toggle() end, desc = "Toggle file tree" },
		{
			"<C-n>",
			function()
				ToggleTree("filesystem")
			end,
			desc = "Toggle file tree",
		},

		-- Typst format
		{
			"<leader>ft",
			function()
				FormatBuffer("typstfmt")
			end,
			desc = "Typst formater",
		},

		-- Lsp
		{
			"K",
			function()
				vim.lsp.buf.hover()
			end,
			desc = "LSP toggle",
		},
		{
			"<leader>ca",
			function()
				vim.lsp.buf.code_action()
			end,
			desc = "LSP code action",
		},
		{
			"<leader>cd",
			function()
				vim.diagnostic.setqflist()
			end,
			desc = "LSP code diagnostic",
		},

		-- Gitsigns
		{
			"<leader>nc",
			function()
				gitsigns.nav_hunk("next")
			end,
			desc = "Next change",
		},
		{
			"<leader>pc",
			function()
				gitsigns.nav_hunk("prev")
			end,
			desc = "Previous change",
		},

		-- increment/decrement numbers
		{ "<leader>+", "<C-a>", desc = "Increment number" }, -- increment
		{ "<leader>-", "<C-x>", desc = "Decrement number" }, -- decrement

		{ "<leader>to", "<cmd>tabnew<CR>", desc = "Open new tab" }, -- open new tab
		{ "<leader>tx", "<cmd>tabclose<CR>", desc = "Close current tab" }, -- close current tab
		{ "<leader>tn", "<cmd>tabn<CR>", desc = "Go to next tab" }, --  go to next tab
		{ "<leader>tp", "<cmd>tabp<CR>", desc = "Go to previous tab" }, --  go to previous tab
		{ "<leader>tf", "<cmd>tabnew %<CR>", desc = "Open current buffer in new tab" }, --  move current buffer to new tab
	},
	{
		mode = "v",
		{ "<S-j>", ":m '>+1<CR>gv=gv", desc = "Move selection down" },
		{ "<S-k>", ":m '<-2<CR>gv=gv", desc = "Move selection up" },

		-- Comment
		{ "<leader>/", "gc", desc = "comment toggle", remap = true },
	},
})
