local map = vim.keymap.set

map("i", "<C-b>", "<ESC>^i", { desc = "Move Beginning of line" })
map("i", "<C-e>", "<End>", { desc = "Move End of line" })
map("i", "<C-h>", "<Left>", { desc = "Move Left" })
map("i", "<C-l>", "<Right>", { desc = "Move Right" })
map("i", "<C-j>", "<Down>", { desc = "Move Down" })
map("i", "<C-k>", "<Up>", { desc = "Move Up" })

map("n", "<Esc>", "<cmd>noh<CR>", { desc = "General Clear highlights" })

map("n", "<C-s>", "<cmd>w<CR>", { desc = "File Save" })
map("n", "<C-c>", "<cmd>%y+<CR>", { desc = "File Copy whole" })

map("n", "<leader>n", "<cmd>set nu!<CR>", { desc = "Toggle Line number" })
map("n", "<leader>rn", "<cmd>set rnu!<CR>", { desc = "Toggle Relative number" })
map("n", "<leader>ch", "<cmd>NvCheatsheet<CR>", { desc = "Toggle NvCheatsheet" })

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

-- lsp mappings
map("n", "<A-CR>", "<cmd>Lspsaga code_action<CR>", { desc = "Lsp code action" })
map("v", "<A-CR>", "<cmd>Lspsaga code_action<CR>", { desc = "Lsp code action" })
map("n", "<A-p>", "<cmd>Lspsaga peek_definition<CR>", { desc = "Lsp peek definition" })
map("n", "<A-P>", "<cmd>Lspsaga peek_type_definition<CR>", { desc = "Lsp peek type definition" })
map("v", "<A-p>", "<cmd>Lspsaga peek_definition<CR>", { desc = "Lsp peek definition" })
map("v", "<A-P>", "<cmd>Lspsaga peek_type_definition<CR>", { desc = "Lsp peek type definition" })
map("n", "<A-g>", "<cmd>Lspsaga goto_definition<CR>", { desc = "Lsp goto definition" })
map("n", "<A-G>", "<cmd>Lspsaga goto_type_definition<CR>", { desc = "Lsp goto type definition" })
map("v", "<A-g>", "<cmd>Lspsaga goto_definition<CR>", { desc = "Lsp goto definition" })
map("v", "<A-G>", "<cmd>Lspsaga goto_type_definition<CR>", { desc = "Lsp goto type definition" })
map("n", "<A-r>", "<cmd>Lspsaga rename ++project<CR>", { desc = "Lsp rename" })
map("v", "<A-r>", "<cmd>Lspsaga rename ++project<CR>", { desc = "Lsp rename" })
map("n", "<A-d>", "<cmd>Lspsaga diagnostic_jump_next<CR>", { desc = "Lsp diagnostic next" })
map("v", "<A-d>", "<cmd>Lspsaga diagnostic_jump_next<CR>", { desc = "Lsp diagnostic next" })
map("n", "<A-D>", "<cmd>Lspsaga diagnostic_jump_prev<CR>", { desc = "Lsp diagnostic previous" })
map("v", "<A-D>", "<cmd>Lspsaga diagnostic_jump_prev<CR>", { desc = "Lsp diagnostic previous" })

-- tabufline
map("n", "<leader>b", "<cmd>enew<CR>", { desc = "Buffer New" })

map("n", "<tab>", function()
  require("nvchad.tabufline").next()
end, { desc = "Buffer Goto next" })

map("n", "<S-tab>", function()
  require("nvchad.tabufline").prev()
end, { desc = "Buffer Goto prev" })

map("n", "<leader>x", function()
  require("nvchad.tabufline").close_buffer()
end, { desc = "Buffer Close" })

-- Comment
map("n", "<leader>/", "gcc", { desc = "Comment toggle", remap = true })
map("v", "<leader>/", "gc", { desc = "Comment toggle", remap = true })

-- Spider nvim movement
map("n", "w", "<cmd>lua require('spider').motion('w')<CR>", {desc = "Spider Move to next word"})
map("n", "b", "<cmd>lua require('spider').motion('b')<CR>", {desc = "Spider Move to previous word"})
map("n", "e", "<cmd>lua require('spider').motion('e')<CR>", {desc = "Spider Move to end of word"})
map("v", "w", "<cmd>lua require('spider').motion('w')<CR>", {desc = "Spider Move to next word"})
map("v", "b", "<cmd>lua require('spider').motion('b')<CR>", {desc = "Spider Move to previous word"})
map("v", "e", "<cmd>lua require('spider').motion('e')<CR>", {desc = "Spider Move to end of word"})

-- nvimtree
map("n", "<C-n>", "<cmd>NvimTreeToggle<CR>", { desc = "Nvimtree Toggle window" })
map("n", "<leader>e", "<cmd>NvimTreeFocus<CR>", { desc = "Nvimtree Focus window" })

-- telescope
map("n", "<leader>fw", "<cmd>Telescope live_grep<CR>", { desc = "Telescope Live grep" })
map("n", "<leader>fb", "<cmd>Telescope buffers<CR>", { desc = "Telescope Find buffers" })
map("n", "<leader>fh", "<cmd>Telescope help_tags<CR>", { desc = "Telescope Help page" })

map("n", "<leader>fo", "<cmd>Telescope oldfiles<CR>", { desc = "Telescope Find oldfiles" })
map("n", "<leader>fz", "<cmd>Telescope current_buffer_fuzzy_find<CR>", { desc = "Telescope Find in current buffer" })
map("n", "<leader>cm", "<cmd>Telescope git_commits<CR>", { desc = "Telescope Git commits" })
map("n", "<leader>gt", "<cmd>Telescope git_status<CR>", { desc = "Telescope Git status" })
map("n", "<leader>pt", "<cmd>Telescope terms<CR>", { desc = "Telescope Pick hidden term" })
map("n", "<leader>th", "<cmd>Telescope themes<CR>", { desc = "Telescope Nvchad themes" })
map("n", "<leader>ff", "<cmd>Telescope find_files<cr>", { desc = "Telescope Find files" })
map(
  "n",
  "<leader>fa",
  "<cmd>Telescope find_files follow=true no_ignore=true hidden=true<CR>",
  { desc = "Telescope Find all files" }
)

-- terminal
map("t", "<C-x>", "<C-\\><C-N>", { desc = "Terminal Escape terminal mode" })

-- new terminals
map("n", "<leader>h", function()
  require("nvchad.term").new { pos = "sp", size = 0.3 }
end, { desc = "Terminal New horizontal term" })

map("n", "<leader>v", function()
  require("nvchad.term").new { pos = "vsp", size = 0.3 }
end, { desc = "Terminal New vertical window" })

-- toggleable
map({ "n", "t" }, "<A-v>", function()
  require("nvchad.term").toggle { pos = "vsp", id = "vtoggleTerm", size = 0.3 }
end, { desc = "Terminal Toggleable vertical term" })

map({ "n", "t" }, "<A-h>", function()
  require("nvchad.term").toggle { pos = "sp", id = "htoggleTerm", size = 0.3 }
end, { desc = "Terminal New horizontal term" })

map({ "n", "t" }, "<A-i>", function()
  require("nvchad.term").toggle { pos = "float", id = "floatTerm" }
end, { desc = "Terminal Toggle Floating term" })

map("t", "<ESC>", function()
  local win = vim.api.nvim_get_current_win()
  vim.api.nvim_win_close(win, true)
end, { desc = "Terminal Close term in terminal mode" })

-- whichkey
map("n", "<leader>wK", "<cmd>WhichKey <CR>", { desc = "Whichkey all keymaps" })

map("n", "<leader>wk", function()
  vim.cmd("WhichKey " .. vim.fn.input "WhichKey: ")
end, { desc = "Whichkey query lookup" })

-- blankline
map("n", "<leader>cc", function()
  local config = { scope = {} }
  config.scope.exclude = { language = {}, node_type = {} }
  config.scope.include = { node_type = {} }
  local node = require("ibl.scope").get(vim.api.nvim_get_current_buf(), config)

  if node then
    local start_row, _, end_row, _ = node:range()
    if start_row ~= end_row then
      vim.api.nvim_win_set_cursor(vim.api.nvim_get_current_win(), { start_row + 1, 0 })
      vim.api.nvim_feedkeys("_", "n", true)
    end
  end
end, { desc = "Blankline Jump to current context" })

-- copilotchat
map("n", "<leader>cch", function()
    local actions = require("CopilotChat.actions")
    require("CopilotChat.integrations.telescope").pick(actions.help_actions())
end, { desc = "CopilotChat - Help actions" })
map("n", "<leader>ccp", function()
    local actions = require("CopilotChat.actions")
    require("CopilotChat.integrations.telescope").pick(actions.prompt_actions())
end, { desc = "CopilotChat - Prompt actions" })
map("v", "<leader>cch", function()
    local actions = require("CopilotChat.actions")
    require("CopilotChat.integrations.telescope").pick(actions.help_actions())
end, { desc = "CopilotChat - Help actions" })
map("v", "<leader>ccp", function()
    local actions = require("CopilotChat.actions")
    require("CopilotChat.integrations.telescope").pick(actions.prompt_actions())
end, { desc = "CopilotChat - Prompt actions" })




-- Custom mappings
map("n", "<space>", ":", { desc = "Enter command mode" })
map({ "n", "v" }, "j", "h", { desc = "Move left" })
map({ "n", "v" }, "k", "j", { desc = "Move down" })
map({ "n", "v" }, "l", "k", { desc = "Move up" })
map({ "n", "v" }, "m", "l", { desc = "Move right" })
map("n", "<C-j>", "<C-w>h", { desc = "Move the cursor to the left window" })
map("n", "<C-k>", "<C-w>j", { desc = "Move the cursor to the bottom window" })
map("n", "<C-l>", "<C-w>k", { desc = "Move the cursor to the upper window" })
map("n", "<C-m>", "<C-w>l", { desc = "Move the cursor to the right window" })
map("n", "<C-up>", "<C-w>+", { desc = "Enlarge window upwards" })
map("n", "<C-down>", "<C-w>-", { desc = "Enlarge window downwards" })
map("n", "<C-left>", "<C-w>>", { desc = "Enlarge window to the left" })
map("n", "<C-right>", "<C-w><", { desc = "Enlarge window to the rigth" })
map("n", "o", "o<ESC>")
map("n", "O", "O<ESC>")
map("n", "n", "nzz")
map("n", "N", "Nzz")
map({ "n", "v" }, "y", "\"+y")
map("n", "yy", "\"+yy")
map({ "n", "v" }, "Y", "\"+yg_")
map({ "n", "v" }, "p", "\"+p")
map({ "n", "v" }, "P", "\"+P")
map("n", "<leader>a", function()
    require("treesj").toggle()
end, { desc = "Extends or retracts an instruction block" })
map("n", "<leader>gb", "<cmd>Gitsigns blame<CR>", { desc = "Git blame" })
map("v", "<leader>gb", "<cmd>Gitsigns blame<CR>", { desc = "Git blame" })
map("i", "jj", "<ESC>", { desc = "Go into normal mode" })
map("i", "<C-d>", "<ESC>ddi", { desc = "Delete a line" })

-- Disable mappings
-- local nomap = vim.keymap.del

-- nomap("n", "h")
-- nomap("n", "j")
-- nomap("n", "k")
-- nomap("n", "l")
-- nomap("n", "<C-h>")
-- nomap("n", "<C-j>")
-- nomap("n", "<C-k>")
-- nomap("n", "<C-l>")
-- nomap("n", "<leader>")

-- nomap("v", "h")
-- nomap("v", "j")
-- nomap("v", "k")
-- nomap("v", "l")
