local cwdopt = '{cwd = require("userfuncs").find_project_root()}'

local noremap = {noremap = true}
local snoremap = { noremap = true, silent = true }

-- up and down between wrapped lines
vim.api.nvim_set_keymap('', 'k', 'gk', snoremap)
vim.api.nvim_set_keymap('', 'j', 'gj', snoremap)
vim.api.nvim_set_keymap('', '<Up>', 'gk', snoremap)
vim.api.nvim_set_keymap('', '<Down>', 'gj', snoremap)
vim.api.nvim_set_keymap('i', '<Up>', '<c-\\><c-o>gk', snoremap)
vim.api.nvim_set_keymap('i', '<Down>', '<c-\\><c-o>gj', snoremap)

-- file navigation
vim.api.nvim_set_keymap('n', '-', '<cmd>:Telescope file_browser path=%:p:h select_buffer=true<cr>', noremap)
vim.api.nvim_set_keymap('n', '<F1>', '<cmd>b#<cr>', noremap)
vim.api.nvim_set_keymap('i', '<F1>', '<cmd>b#<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>-', '<cmd>lua require("telescope.builtin").find_files('..cwdopt..')<cr>', noremap)
vim.api.nvim_set_keymap('n', '_', '<cmd>lua require("telescope.builtin").buffers('..cwdopt..')<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>gg', '<cmd>lua require("telescope.builtin").live_grep('..cwdopt..')<cr>', noremap)

-- lsp stuff
vim.api.nvim_set_keymap('n', '<leader>fu', '<cmd>Telescope lsp_references<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>gd', '<cmd>Telescope lsp_definitions<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>dn', '<cmd>lua vim.diagnostic.goto_next()<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>dN', '<cmd>lua vim.diagnostic.goto_prev()<cr>', noremap)
vim.api.nvim_set_keymap('n', '<leader>xx', '<cmd>lua vim.lsp.buf.code_action()<cr>', noremap)

-- dadbod stuff
vim.cmd([[
augroup dadbod_keybindings
  autocmd!
  autocmd FileType sql nnoremap <buffer> <F5> :'{,'}DB<cr>
  autocmd FileType sql vnoremap <buffer> <F5> :DB<cr>
  autocmd FileType sql inoremap <buffer> <F5> <C-o>:'{,'}DB<cr>
augroup end
]])

-- misc
-- f2 gets highlight details of thing under cursor
vim.api.nvim_set_keymap('n', '<F2>', '<cmd>echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . "> trans<" . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<cr>', noremap)
-- f3 for synonym list under cursor
vim.api.nvim_set_keymap('n', '<F3>', '<cmd>lua require("telescope").extensions.dict.synonyms()<cr>', noremap)
-- replace z= with telescope spell_suggest
vim.api.nvim_set_keymap('n', 'z=', '<cmd>Telescope spell_suggest<cr>', noremap)

-- f12 for zen mode
vim.api.nvim_set_keymap('n', '<F12>', '<cmd>TZAtaraxis<cr>', noremap)

vim.api.nvim_set_keymap('', '<S-ScrollWheelUp>', '<ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-ScrollWheelDown>', '<ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-2-ScrollWheelUp>', '<2-ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-2-ScrollWheelDown>', '<2-ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-3-ScrollWheelUp>', '<3-ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-3-ScrollWheelDown>', '<3-ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-4-ScrollWheelUp>', '<4-ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-4-ScrollWheelDown>', '<4-ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-PageUp>', '<ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-PageDown>', '<ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-2-PageUp>', '<2-ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-2-PageDown>', '<2-ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-3-PageUp>', '<3-ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-3-PageDown>', '<3-ScrollWheelRight>', noremap)
vim.api.nvim_set_keymap('', '<S-4-PageUp>', '<4-ScrollWheelLeft>', noremap)
vim.api.nvim_set_keymap('', '<S-4-PageDown>', '<4-ScrollWheelRight>', noremap)
