vim.g.mapleader = " "
vim.keymap.set("n", "<leader>pv", vim.cmd.Ex)
--vim.keymap.set("n", "<leader>n", vim.cmd.Ex)
--vim.keymap.set("n", "<C-b>", vim.cmd.Ex)

vim.keymap.set("n", "<C-b>", ":NvimTreeToggle<CR>", {
	noremap = true,
})
--[[
vim.keymap.set("n", "<leader>n", ":NvimTreeToggle<CR>", {
	noremap = true,
})
--]]
vim.keymap.set("n", "<leader>n", "<CMD>Oil<CR>", { desc = "Open parent directory" })

-- barbar keymaps
-- Move to previous/next
vim.keymap.set("n", "<Tab>", "<Cmd>BufferNext<CR>")
vim.keymap.set("n", "<S-Tab>", "<Cmd>BufferPrevious<CR>")
-- Re-order to previous/next
vim.keymap.set("n", "<C-,>", "<Cmd>BufferMovePrevious<CR>")
vim.keymap.set("n", "<C-.>", "<Cmd>BufferMoveNext<CR>")
-- Close buffer
vim.keymap.set("n", "<C-w>", "<Cmd>BufferClose<CR>")
-- Pin/Unpin buffer
vim.keymap.set("n", "<C-p>", "<Cmd>BufferPin<CR>")
-- Goto buffer in position...
vim.keymap.set("n", "<C-1>", "<Cmd>BufferGoto 1<CR>")
vim.keymap.set("n", "<C-2>", "<Cmd>BufferGoto 2<CR>")
vim.keymap.set("n", "<C-3>", "<Cmd>BufferGoto 3<CR>")
vim.keymap.set("n", "<C-4>", "<Cmd>BufferGoto 4<CR>")
vim.keymap.set("n", "<C-5>", "<Cmd>BufferGoto 5<CR>")
vim.keymap.set("n", "<C-6>", "<Cmd>BufferGoto 6<CR>")
vim.keymap.set("n", "<C-7>", "<Cmd>BufferGoto 7<CR>")
vim.keymap.set("n", "<C-8>", "<Cmd>BufferGoto 8<CR>")
vim.keymap.set("n", "<C-9>", "<Cmd>BufferGoto 9<CR>")
vim.keymap.set("n", "<C-0>", "<Cmd>BufferLast<CR>")

vim.keymap.set("n", "<leader>a", ":lua vim.lsp.buf.code_action()<CR>")

local builtin = require("telescope.builtin")
vim.keymap.set("n", "<leader>ff", builtin.find_files, {})
vim.keymap.set("n", "<leader>fp", "<Cmd>Telescope flutter commands<CR>", {})
vim.keymap.set("n", "<leader>fg", builtin.live_grep, {})
vim.keymap.set("n", "<leader>fb", builtin.buffers, {})
vim.keymap.set("n", "<leader>fh", builtin.help_tags, {})

vim.keymap.set("n", "<leader>pp", "<Cmd>Telescope projects<CR>", {})

function _ADD_CURR_DIR_TO_PROJECTS()
	local historyfile = require("project_nvim.utils.path").historyfile
	local curr_directory = vim.fn.expand("%:p:h")
	vim.cmd("!echo " .. curr_directory .. " >> " .. historyfile)
end

vim.cmd("command! ProjectAddManually lua _ADD_CURR_DIR_TO_PROJECTS()")

vim.keymap.set("n", "<leader>d", ":lua vim.lsp.buf.hover()<CR>")

-- Clear highlighting on escape in normal mode
vim.keymap.set("n", "<esc>", ":noh<return><esc>")
vim.keymap.set("n", "<esc>^[", "<esc>^[")

-- Easier shortcuts to move the cursor to the end or start of a line
vim.keymap.set("n", "<C-h>", "<Home>", { noremap = true })
vim.keymap.set("i", "<C-h>", "<Home>", { noremap = true })
vim.keymap.set("n", "<C-l>", "<End>", { noremap = true })
vim.keymap.set("i", "<C-l>", "<End>", { noremap = true })
-- Easier move cursor in insert mode
vim.keymap.set("i", "<C-k>", "<Up>", { noremap = true })
vim.keymap.set("i", "<C-j>", "<Down>", { noremap = true })
vim.keymap.set("i", "<C-h>", "<Left>", { noremap = true })
vim.keymap.set("i", "<C-l>", "<Right>", { noremap = true })

-- recommended mappings
-- resizing splits
-- these keymaps will also accept a range,
-- for example `10<A-h>` will `resize_left` by `(10 * config.default_amount)`
vim.keymap.set("n", "<A-h>", require("smart-splits").resize_left)
vim.keymap.set("n", "<A-j>", require("smart-splits").resize_down)
vim.keymap.set("n", "<A-k>", require("smart-splits").resize_up)
vim.keymap.set("n", "<A-l>", require("smart-splits").resize_right)
-- moving between splits
vim.keymap.set("n", "<C-h>", require("smart-splits").move_cursor_left)
vim.keymap.set("n", "<C-j>", require("smart-splits").move_cursor_down)
vim.keymap.set("n", "<C-k>", require("smart-splits").move_cursor_up)
vim.keymap.set("n", "<C-l>", require("smart-splits").move_cursor_right)
vim.keymap.set("n", "<C-\\>", require("smart-splits").move_cursor_previous)
-- swapping buffers between windows
vim.keymap.set("n", "<leader><leader>h", require("smart-splits").swap_buf_left)
vim.keymap.set("n", "<leader><leader>j", require("smart-splits").swap_buf_down)
vim.keymap.set("n", "<leader><leader>k", require("smart-splits").swap_buf_up)
vim.keymap.set("n", "<leader><leader>l", require("smart-splits").swap_buf_right)
