---@type MappingsTable

local M = {}

-- NOTE: The mappings here are case sensitive!!! Removing a specific default
-- mapping requires the same case here!
M.disabled = {
  n = {
    ["<C-a>"] = "",
    ["<C-s>"] = "",
    ["<C-c>"] = "",
    ["<leader>n"] = "",
    ["<leader>rn"] = "",
    ["<Up>"] = "",
    ["<Down>"] = "",
    ["<leader>x"] = "",
    ["<leader>/"] = "",
    ["<leader>ra"] = "",
    ["[d"] = "",
    ["]d"] = "",
    ["[c"] = "",
    ["]c"] = "",
    ["<leader>h"] = "",
    ["<leader>ff"] = "",
    ["<leader>fm"] = "",
    ["<leader>fa"] = "",
    ["<leader>fw"] = "",
    ["<leader>fb"] = "",
    ["<leader>th"] = "",
    ["<leader>fh"] = "",
    ["<leader>fo"] = "",
    ["<leader>fz"] = "",
    ["<leader>cm"] = "",
    ["<leader>gt"] = "",
    ["<leader>pt"] = "",
    ["<leader>ma"] = "",
    ["<leader>v"] = "",
    ["<leader>rh"] = "",
    ["<leader>ph"] = "",
    ["<leader>gb"] = "",
    ["<leader>td"] = "",
    ["<leader>wk"] = "",
    ["#"] = "",
  },
  t = {
    ["<C-t>"] = "",
  },
  v = {
    ["#"] = "",
    ["?"] = "",
    ["<leader>/"] = "",
  },
}

M.tabufline = {
  plugin = true,

  n = {
    -- cycle through buffers
    ["<Tab>"] = {
      function()
        require("nvchad.tabufline").tabuflineNext()
      end,
      "Goto next buffer",
    },

    ["<S-tab>"] = {
      function()
        require("nvchad.tabufline").tabuflinePrev()
      end,
      "Goto prev buffer",
    },

    -- close buffer + hide terminal buffer
    ["<A-c>"] = {
      function()
        require("nvchad.tabufline").close_buffer()
      end,
      "Close buffer",
    },
  },
}

M.comment = {
  plugin = true,

  -- toggle comment in both modes
  n = {
    ["<leader>v"] = {
      function()
        require("Comment.api").toggle.linewise.current()
      end,
      "Toggle comment",
    },
  },

  v = {
    ["<leader>v"] = {
      "<esc><cmd>lua require('Comment.api').toggle.linewise(vim.fn.visualmode())<cr>",
      "Toggle comment",
    },
  },
}

M.leap = {
  plugin = true,
  n = {
    ["s"] = {
      "<plug>(leap-forward)",
      "leap forward",
    },
    ["S"] = {
      "<plug>(leap-backward)",
      "leap backward",
    },
    ["gs"] = {
      "<plug>(leap-from-window)",
      "leap from window",
    },
  },
  x = {
    ["s"] = {
      "<plug>(leap-forward)",
      "leap forward",
    },
    ["S"] = {
      "<plug>(leap-backward)",
      "leap forward",
    },
    ["gs"] = {
      "<plug>(leap-from-window)",
      "leap from window",
    },
  },
  o = {
    ["s"] = {
      "<plug>(leap-forward)",
      "leap forward",
    },
    ["S"] = {
      "<plug>(leap-backward)",
      "leap forward",
    },
    ["gs"] = {
      "<plug>(leap-from-window)",
      "leap from window",
    },
  },
}

M.lazygit = {
  plugin = true,
  n = {
    ["<leader>gg"] = { "<cmd> LazyGit <cr>", "Open LazyGit" },
  },
}

M.lspconfig = {
  plugin = true,
  n = {
    ["[d"] = {
      function()
        vim.diagnostic.goto_prev { float = { border = "rounded" } }
      end,
      "Goto prev",
    },

    ["]d"] = {
      function()
        vim.diagnostic.goto_next { float = { border = "rounded" } }
      end,
      "Goto next",
    },
    ["<leader>cr"] = {
      function()
        require("nvchad.renamer").open()
      end,
      "LSP rename",
    },
  },
}

M.clipboard = {
  plugin = false,
  n = {
    ["<leader>y"] = { '"+y', "yank to system" },
    ["<leader>Y"] = { '"+Y', "yank to system" },
    ["<leader>yy"] = { '"+y', "yank to system" },
  },
  v = {
    ["<leader>y"] = { '"+y', "yank to system" },
    ["<leader>Y"] = { '"+Y', "yank to system" },
    ["<leader>yy"] = { '"+y', "yank to system" },
  },
}

M.telescope = {
  plugin = true,
  n = {
    -- find
    ["<localleader>ff"] = { "<cmd> Telescope find_files <cr>", "Find files" },
    ["<localleader>fa"] = { "<cmd> Telescope find_files follow=true no_ignore=true hidden=true <cr>", "Find all" },
    ["<localleader>fw"] = { "<cmd> Telescope live_grep <cr>", "Live grep" },
    ["<localleader>fb"] = { "<cmd> Telescope buffers <cr>", "Find buffers" },
    ["<localleader>fh"] = { "<cmd> Telescope help_tags <cr>", "Help page" },
    ["<localleader>fo"] = { "<cmd> Telescope oldfiles <cr>", "Find oldfiles" },
    ["<localleader>fz"] = { "<cmd> Telescope current_buffer_fuzzy_find <cr>", "Find in current buffer" },

    -- directory
    ["<localleader>cd"] = { "<cmd> Telescope zoxide list<cr>", "telescope zoxide cd" },

    -- git
    ["<localleader>cm"] = { "<cmd> Telescope git_commits <cr>", "Git commits" },
    ["<localleader>gt"] = { "<cmd> Telescope git_status <cr>", "Git status" },

    -- pick a hidden term
    ["<localleader>pt"] = { "<cmd> Telescope terms <cr>", "Pick hidden term" },

    -- theme switcher
    ["<localleader>th"] = { "<cmd> Telescope themes <cr>", "Nvchad themes" },

    ["<localleader>ma"] = { "<cmd> Telescope marks <cr>", "telescope bookmarks" },

    -- lsp stuff
    ["<localleader>cw"] = { "<cmd> Telescope lsp_dynamic_workspace_symbols <cr>", "telescope dynamic workspace symbols" },
    ["<localleader>cf"] = { "<cmd> Telescope lsp_document_symbols <cr>", "telescope document symbols" },
    ["<localleader>ci"] = { "<cmd> Telescope diagnostics <cr>", "telescope diagnostics" },
  },
}

M.toggleterm = {
  plugin = true,
  n = {
    ["<F12>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-i>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-t>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=tab"<cr>',
      "toggle terminal",
    },
    ["<A-h>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=horizontal"<cr>',
      "toggle terminal",
    },
    ["<A-v>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=vertical"<cr>',
      "toggle terminal",
    },
  },
  i = {
    ["<F12>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-i>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-t>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=tab"<cr>',
      "toggle terminal",
    },
    ["<A-h>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=horizontal"<cr>',
      "toggle terminal",
    },
    ["<A-v>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=vertical"<cr>',
      "toggle terminal",
    },
  },
  t = {
    ["<F12>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-i>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-t>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=tab"<cr>',
      "toggle terminal",
    },
    ["<A-h>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=horizontal"<cr>',
      "toggle terminal",
    },
    ["<A-v>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=vertical"<cr>',
      "toggle terminal",
    },
  },
  x = {
    ["<F12>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-i>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=float"<cr>',
      "toggle terminal",
    },
    ["<A-t>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=tab"<cr>',
      "toggle terminal",
    },
    ["<A-h>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=horizontal"<cr>',
      "toggle terminal",
    },
    ["<A-v>"] = {
      '<cmd>exe v:count1 "ToggleTerm direction=vertical"<cr>',
      "toggle terminal",
    },
  },
}

M.nvimtree = {
  plugin = true,
  n = {
    -- toggle
    ["<C-n>"] = { "<cmd> NvimTreeToggle <cr>", "Toggle nvimtree" },
    ["<F5>"] = { "<cmd> NvimTreeToggle <cr>", "Toggle nvimtree" },
    ["<leader>tf"] = { "<cmd> NvimTreeToggle <cr>", "Toggle nvimtree" },

    -- focus
    ["<localleader>e"] = { "<cmd> NvimTreeFocus <cr>", "Focus nvimtree" },
  },
}

M.movements = {
  plugin = false,
  i = {
    --big move
    ["<A-k>"] = { "<C-u>", "big step down" },
    ["<A-j>"] = { "<C-d>", "big step down" },

    -- go to  beginning and end
    ["<C-b>"] = { "<home>", "Beginning of line" },
    ["<C-e>"] = { "<end>", "End of line" },

    -- navigate within insert mode
    ["<C-h>"] = { "<left>", "Move left" },
    ["<C-l>"] = { "<right>", "Move right" },
    ["<C-j>"] = { "<down>", "Move down" },
    ["<C-k>"] = { "<up>", "Move up" },
  },
  n = {
    --big move
    ["<A-k>"] = { "<C-u>", "big step down" },
    ["<A-j>"] = { "<C-d>", "big step down" },

    -- go to  beginning and end
    ["H"] = { "<home>", "Beginning of line" },
    ["L"] = { "<end>", "End of line" },

    -- go to mark with "#"
    ["#"] = { "'", "go to mark" },

    -- spell
    ["]s"] = { "]s", "go to next spelling mark" },
    ["[s"] = { "[s", "go to last spelling mark" },

    -- just scroll a line
    ["zk"] = { "<C-y>", "scroll up a line" },
    ["zj"] = { "<C-e>", "scroll down a line" },
  },
  v = {
    --big move
    ["<A-k>"] = { "<C-u>", "big step down" },
    ["<A-j>"] = { "<C-d>", "big step down" },

    -- go to  beginning and end
    ["H"] = { "<home>", "Beginning of line" },
    ["L"] = { "<end>", "End of line" },
  },
  t = {
    ["<C-w>"] = {
      vim.api.nvim_replace_termcodes("<C-\\><C-N>", true, true, true),
      "Escape terminal mode",
    },
  },
}

M.edit = {
  plugin = false,
  n = {
    -- easy newline
    ["OO"] = { "O<esc>", "insert a line above" },
    ["oo"] = { "o<esc>", "insert a line below" },

    -- substitute (change hovered/selection with insert mode)
    -- normally, this is s, but that is leaps binding now
    ["<leader>s"] = { "s", "replace with insert mode" },

    -- split and join lines
    ["J"] = { "j" },
    ["<C-s-j>"] = { "<cmd>join<cr>", "join lines" },
    ["<C-s-s>"] = { "i<cr><esc>l", "split line" },

    -- do something useful with the arrows
    ["<Up>"] = { "<cmd> move-2<cr>", "Move line up" },
    ["<Down>"] = { "<cmd> move+<cr>", "Move line down" },
    ["<Left>"] = { "<<", "Less indentation" },
    ["<Right>"] = { ">>", "More indentation" },

    --  format with conform
    ["<leader>ff"] = {
      function()
        -- vim.lsp.buf.format()
        require("conform").format()
      end,
      "format buffer",
    },

    --  remove trailing whitespace
    ["<leader>fw"] = {
      function()
        require("mini.trailspace").trim()
      end,
      "remove whitespace",
    },

    -- spell
    ["z<space>"] = { "<cmd>Telescope spell_suggest<CR>", "Spell suggest" },
    ["z="] = { "<cmd>Telescope spell_suggest<CR>", "Spell suggest" },
  },
  v = {
    -- NOTE: I would love to know why these commands work, they seem really
    -- crazy.
    ["<Up>"] = { ":move'<-2<CR>gv=gv", "Move lines up", opts = { silent = true } },
    ["<Down>"] = { ":move'>+<CR>gv=gv", "Move lines down", opts = { silent = true } },
    ["<Left>"] = { "<gv", "Less indentation" },
    ["<Right>"] = { ">gv", "More indentation" },
  },
  x = {},
  t = {
    --big move
    ["<A-k>"] = { "<C-u>", "big step down" },
    ["<A-j>"] = { "<C-d>", "big step down" },
  },
}

M.ui = {
  plugin = false,
  n = {
    -- toggle wrap
    ["<leader>tw"] = {
      function()
        vim.opt_local.wrap = not vim.wo.wrap
        vim.opt_local.breakindent = not vim.wo.breakindent

        if vim.wo.colorcolumn == "" then
          vim.opt_local.colorcolumn = tostring(vim.bo.textwidth)
        else
          vim.opt_local.colorcolumn = ""
        end
      end,
      "toggle wrap",
    },

    -- toggle some features
    ["<leader>tn"] = { "<cmd>setlocal nonumber!<cr>", "toggle line numbers" },
    ["<leader>trn"] = { "<cmd>setlocal nornu!<cr>", "toggle relative line numbers" },
    ["<leader>ts"] = { "<cmd>setlocal spell!<cr>", "toggle spell check" },
    ["<leader>ti"] = {
      function()
        require("lsp-inlayhints").toggle()
      end,
      "toggle inlay hints",
    },
    ["<leader>th"] = {
      function()
        require("base46").toggle_theme()
      end,
      "toggle theme",
    },
    ["<leader>te"] = {
      function()
        require("base46").toggle_transparency()
      end,
      "toggle transparency",
    },

    -- open windows
    ['<leader>"'] = { "<cmd>vsplit<cr>", "open a new window to the side" },
    ["<leader>%"] = { "<cmd>split<cr>", "open a new window on the totop" },

    -- resize windows
    ["<C-Up>"] = { "<cmd>resize +1<cr>", "resize window" },
    ["<C-Down>"] = { "<cmd>resize -1<cr>", "resize window" },
    ["<C-Left>"] = { "<cmd>vertical resize +1<cr>", "resize window" },
    ["<C-Right>"] = { "<cmd>vertical resize -1<cr>", "resize window" },

    -- tabs
    ["<leader>wn"] = { "<cmd>tabnew<cr>", "open a new tab" },
    ["<leader>wc"] = { "<cmd>tabclose<cr>", "close current tab" },
    ["<leader>wk"] = { "<cmd>tabnext<cr>", "go to next tab" },
    ["<leader>wj"] = { "<cmd>tabprevious<cr>", "go to prev tab" },

    -- folds
    ["<S-Return>"] = { "zMzv", "focus on this fold" },
  },
}

M.goto_preview = {
  plugin = true,
  n = {
    ["gpd"] = {
      function()
        require("goto-preview").goto_preview_definition()
      end,
      "go to definition preview",
      noremap = true,
    },
    ["gpD"] = {
      function()
        require("goto-preview").goto_preview_declaration()
      end,
      "go to declaration preview",
      noremap = true,
    },
    ["gpi"] = {
      function()
        require("goto-preview").goto_preview_implementation()
      end,
      "go to implementation preview",
      noremap = true,
    },
    ["gpt"] = {
      function()
        require("goto-preview").goto_preview_type_definition()
      end,
      "go to type preview",
      noremap = true,
    },
    ["gpr"] = {
      function()
        require("goto-preview").goto_preview_type_references()
      end,
      "go to references preview",
      noremap = true,
    },
    ["gpc"] = {
      function()
        require("goto-preview").close_all_win()
      end,
      "close previews",
      noremap = true,
    },
  },
}

M.debug = {
  plugin = true,
  n = {
    ["<leader>db"] = {
      function()
        require("dap").toggle_breakpoint()
      end,
      "toggle breakpoint",
      noremap = true,
    },
    ["<leader>dc"] = {
      function()
        require("dap").continue()
      end,
      "debug continue",
      noremap = true,
    },
    ["<leader>ds"] = {
      function()
        require("dap").step_over()
      end,
      "debug step over",
      noremap = true,
    },
    ["<leader>di"] = {
      function()
        require("dap").step_into()
      end,
      "debug step into",
      noremap = true,
    },
    ["<leader>dr"] = {
      function()
        require("dap").repl.open()
      end,
      "debug open repl",
      noremap = true,
    },
    ["<leader>dws"] = {
      function()
        local widgets = require "dap.ui.widgets"
        local my_sidebar = widgets.sidebar(widgets.scopes)
        my_sidebar.open()
      end,
      "debug window scopes",
      noremap = true,
    },
    ["<leader>dwt"] = {
      function()
        local widgets = require "dap.ui.widgets"
        local my_sidebar = widgets.sidebar(widgets.threads)
        my_sidebar.open()
      end,
      "debug window threads",
      noremap = true,
    },
    ["<leader>dwe"] = {
      function()
        local widgets = require "dap.ui.widgets"
        local my_sidebar = widgets.sidebar(widgets.expression)
        my_sidebar.open()
      end,
      "debug window expressions",
      noremap = true,
    },
    ["<leader>dwi"] = {
      function()
        local widgets = require "dap.ui.widgets"
        local my_sidebar = widgets.sidebar(widgets.sessions)
        my_sidebar.open()
      end,
      "debug window sessions",
      noremap = true,
    },
    ["<leader>dwf"] = {
      function()
        local widgets = require "dap.ui.widgets"
        local my_sidebar = widgets.sidebar(widgets.frames)
        my_sidebar.open()
      end,
      "debug window frames",
      noremap = true,
    },
    ["<leader>dK"] = {
      function()
        require("dap.ui.widgets").hover()
      end,
      "debug hover",
      noremap = true,
    },
    ["<leader>du"] = {
      function()
        require("dapui").toggle()
      end,
      "debug ui toggle",
      noremap = true,
    },
  },
}

return M
