local conf = require('core.conf')

local function map(mode, bind, cmd, opts)
  opts = opts or {}
  opts['noremap'] = true
  opts['silent'] = true

  if type(bind) == 'table' then
    for i in pairs(bind) do
      vim.keymap.set(mode, bind[i], cmd, opts)
    end
  elseif type(bind) == 'string' then
    vim.keymap.set(mode, bind, cmd, opts)
  end
end

-- vim binds ------------------------------------------------------------------
g.mapleader = ' ' -- set leader key

map('x', '<leader>p', [["_dP]], { desc = 'Greatest remap of all time.' })
map('n', '<esc>', ':nohlsearch<Bar>:echo<CR>', { desc = 'Clear search.' })
map('t', '<esc>', '<C-\\><C-n>', { desc = 'make <esc> work in terminals.' })
-- move selected text up/down
map('v', '<S-k>', ":m '<-2<CR>gv=gv", { desc = 'Move selected text up.' })
map('v', '<S-j>', ":m '>+1<CR>gv=gv", { desc = 'Move selected text down.' })

-- the cursor STAYS IN THE MIDDLE
map('n', '<S-j>', 'mzJ`z<cmd>delm z<CR>') -- when combining lines
map('n', 'n', 'nzzzv') -- when searching
map('n', 'N', 'Nzzzv')
map('n', '<C-d>', '<C-d>zz') -- half page jumping
map('n', '<C-u>', '<C-u>zz')

map('n', '<leader>x', '<cmd>!chmod +x "%"<CR>') -- execute order 111

-- add some keybinds to the file view (netrw)
a.nvim_create_autocmd('FileType', {
  pattern = 'netrw',
  callback = function()
    local function bind(lhs, rhs)
      vim.keymap.set('n', lhs, rhs, { remap = true, buffer = true })
    end
    bind('h', '-^') -- Go up a directory
    bind('l', '<CR>') -- Go down a directory / open a file
    bind('.', 'gh') -- Toggle hidden files
    bind('P', '<C-w>z') -- Close preview window
    bind('<esc>', '<cmd>q<CR>') -- Close netrw
  end
})

-- tabs
map('n', '[]', '<cmd>tabnew<CR>')
map('n', '][', '<cmd>tabc<CR>')
map('n', '[[', '<cmd>tabp<CR>')
map('n', ']]', '<cmd>tabN<CR>')

-- config binds ---------------------------------------------------------------
map('n', '<leader>m', conf.configmenu, { desc = 'Neovim config manager menu', })

-- plugin binds ---------------------------------------------------------------

-- pretty lsp view
map('n', 'gd', '<CMD>Glance definitions<CR>')
map('n', 'gr', '<CMD>Glance references<CR>')
map('n', 'gy', '<CMD>Glance type_definitions<CR>')
map('n', 'gi', '<CMD>Glance implementations<CR>')

if pcall(require, "treesj") then
  local treesj = require('treesj') -- treesj
  map('n', '<leader>j', treesj.toggle)
end

if pcall(require, "telescope") then
  local telebuilt = require('telescope.builtin') -- telescope
  -- local telexten = require('telescope').extensions
  map('n', '<leader>sf', telebuilt.find_files, { desc = 'Find files.' })
  map('n', '<leader>so', telebuilt.oldfiles, { desc = 'Find old files.' })
  map('n', '<leader>sg', telebuilt.git_files, { desc = 'Find git files.' })
  -- search urls in buffer
  map('n', '<leader>su', '<Cmd>UrlView<CR>', { desc = 'Find urls in buffer.' })
  -- search lsp symbols
  map('n', '<leader>ss', telebuilt.lsp_document_symbols,
    { desc = 'Find LSP Symbols.' })
  -- search for keybinds
  map('n', '<leader>sk', telebuilt.keymaps,
    { desc = 'Find nvim Keymaps.' })
  -- search for highlights
  map('n', '<leader>sh', telebuilt.highlights,
    { desc = 'Find nvim Highlights.' })
  -- search for autocommands
  map('n', '<leader>sa', telebuilt.autocommands,
    { desc = 'Find nvim Autocommands.' })
  -- search for vim options
  map('n', '<leader>sv', telebuilt.vim_options, { desc = 'Find vim options.' })
  -- search for string in project
  map('n', '<leader>sp', telebuilt.live_grep, { desc = 'Find string in project.' })

  -- Code Actions (requires telescope)
  if pcall(require, "actions-preview") then
    map({ "n", "v" }, "<leader>ca", require("actions-preview").code_actions, {
      desc = 'preview code actions'
    })
  end
end

-- harpoon
if (pcall(require, 'harpoon')) then
  local harpoon = require("harpoon")

  map("n", "<leader>a", function()
    harpoon:list():append()
    vim.notify('added new file to quickmarks', vim.log.levels.INFO, {
      title = misc.appid,
    })
  end)
  map('n', '<C-q>', function() harpoon:list():select(1) end)
  map('n', '<C-g>', function() harpoon:list():select(2) end)
  map('n', '<C-h>', function() harpoon:list():select(3) end)
  map('n', '<C-i>', function() harpoon:list():select(4) end)

  map("n", "<C-S-P>", function() harpoon:list():prev() end)
  map("n", "<C-S-N>", function() harpoon:list():next() end)

  map("n", "<C-e>", function() require("core.harpoon").switcher() end)
end

map('n', '<leader>u', '<cmd>UndotreeToggle<CR>', { desc = 'Open undo tree.' })
map('n', '<leader>f', '<cmd>SFMToggle<CR>', { desc = 'Open file tree view.' })
map('n', '<leader>b', '<cmd>JABSOpen<CR>', { desc = 'Switch between buffers.' })

if pcall(require, "smart-splits") then
  local smartsplits = require('smart-splits') -- resizing buffers (toggleable)
  map('n', '<leader>r', smartsplits.start_resize_mode)
end

-- neogen
if pcall(require, "neogen") then
  map('n', '<leader>df', require("neogen").generate, {
    desc = 'Generate anotations',
  })
end

-- venn
function _G.Toggle_venn()
  local mapb = vim.api.nvim_buf_set_keymap
  local venn_enabled = vim.inspect(vim.b.venn_enabled)
  if venn_enabled == "nil" then
    vim.b.venn_enabled = true
    vim.cmd([[setlocal ve=all]])
    -- draw a line on HJKL keystokes
    mapb(0, "n", "J", "<C-v>j:VBox<CR>", { noremap = true })
    mapb(0, "n", "K", "<C-v>k:VBox<CR>", { noremap = true })
    mapb(0, "n", "L", "<C-v>l:VBox<CR>", { noremap = true })
    mapb(0, "n", "H", "<C-v>h:VBox<CR>", { noremap = true })
    -- draw a box by pressing "f" with visual selection
    mapb(0, "v", "f", ":VBox<CR>", { noremap = true })
  else
    vim.cmd[[setlocal ve=]]
    vim.cmd[[mapclear <buffer>]]
    vim.b.venn_enabled = nil
  end
end
-- toggle keymappings for venn using <leader>v
map('n', '<leader>v', ":lua Toggle_venn()<CR>")