local M = {}

local function toggle_win_opt(name)
  local newval = not vim.api.nvim_get_option_value(name, { win = 0 })
  vim.api.nvim_set_option_value(
    name,
    newval,
    { win = 0 }
  )
  if newval then
    vim.notify(name .. ' is on')
  else
    vim.notify(name .. ' is off')
  end
end

local function toggle_current_win_diagnostic()
  local d = vim.diagnostic
  if d.is_disabled(0, nil) then
    d.enable(0, nil)
    vim.notify('Enabling diagnostic')
  else
    d.disable(0, nil)
    vim.notify('Disabling diagnostic')
  end
end

function M.setup()
  vim.keymap.set('n', '<S-Down>', '<C-E>')
  vim.keymap.set('n', '<S-Up>', '<C-Y>')

  vim.keymap.set('n', '[e', function() vim.diagnostic.jump({count=-1, float = true, severity = vim.diagnostic.severity.ERROR }) end,
    { desc = 'Go to next error' })
  vim.keymap.set('n', ']e', function() vim.diagnostic.jump({count = 1, float = true, severity = vim.diagnostic.severity.ERROR }) end,
    { desc = 'Go to prev error' })
  vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, { desc = 'Diagnostic to loclist' })

  vim.keymap.set({ 'n', 'x', 'o' }, 'n', "'Nn'[v:searchforward]", { expr = true, desc = 'Next search result' })
  vim.keymap.set({ 'n', 'x', 'o' }, 'N', "'nN'[v:searchforward]", { expr = true, desc = 'Prev search result' })


  vim.keymap.set('n', '<leader>ow', function() toggle_win_opt('wrap') end, { desc = 'Toggle wrap' })
  vim.keymap.set('n', '<leader>on', function() toggle_win_opt('number') end, { desc = 'Toggle number' })
  vim.keymap.set('n', '<leader>or', function() toggle_win_opt('relativenumber') end, { desc = 'Toggle relativenumber' })
  vim.keymap.set('n', '<leader>oR', require('configs.options').setup, { desc = 'RESET' })
  vim.keymap.set('n', '<leader>od', toggle_current_win_diagnostic, { desc = 'Toggle diagnostic' })
  vim.keymap.set('n', '<leader>of', function()
    if (vim.api.nvim_get_option_value('foldmethod', { win = 0 }) ~= 'marker') then
      vim.api.nvim_set_option_value('foldmethod', 'marker', { win = 0 })
      vim.api.nvim_set_option_value('foldlevel', 0, { win = 0 })
    else
      vim.api.nvim_set_option_value('foldmethod', 'expr', { win = 0 })
      vim.api.nvim_set_option_value('foldlevel', 99, { win = 0 })
    end
    vim.notify('Fdm: ' .. vim.api.nvim_get_option_value('foldmethod', { win = 0 }))
  end, { desc = 'Toggle foldmethod marker/expr' })

  vim.keymap.set('n', '<leader>di', '<cmd>Inspect<CR>', { desc = 'Inspect' })
  vim.keymap.set('n', '<leader>dt', '<cmd>InspectTree<CR>', { desc = 'InspectTree' })

  vim.keymap.set('x', '<leader>eB', "c<c-r>=system('base64 -w0', @\")<cr><esc>", { desc = 'Encode to base64' })
  vim.keymap.set('x', '<leader>eb', "c<c-r>=system('base64 -d -w0', @\")<cr><esc>", { desc = 'Decode from base64' })
  vim.keymap.set('x', '<leader>er', "c<c-r>=system([ 'drill', '-Q',  @\"])<cr><esc>", { desc = 'Resolve DNS' })
  vim.keymap.set('x', '<leader>eR', "c<c-r>=system([ 'drill', '-Q', '-x',  @\"])<cr><esc>", { desc = 'Resolve reverse DNS' })
end

return M
