local M = {}

local resize = require("my.resize").resize
local utils = require "my.utils"
local nmap, imap, vmap, tmap = utils.nmap, utils.imap, utils.vmap, utils.tmap

-- Window splits
nmap("<C-J>", function()
    utils.winmove "j"
end)
nmap("<C-K>", function()
    utils.winmove "k"
end)
nmap("<C-L>", function()
    utils.winmove "l"
end)
nmap("<C-H>", function()
    utils.winmove "h"
end)
nmap("<C-Q>", vim.cmd.quit)

-- Resize splits
nmap("<A-Up>", function()
    resize(false, -2)
end)
nmap("<A-Down>", function()
    resize(false, 2)
end)
nmap("<A-Left>", function()
    resize(true, -2)
end)
nmap("<A-Right>", function()
    resize(true, 2)
end)

-- Easy move lines
nmap("<A-j>", ":m+<CR>==")
nmap("<A-k>", ":m-2<CR>==")
vmap("<A-j>", ":m '>+1<CR>gv=gv")
vmap("<A-k>", ":m '<-2<CR>gv=gv")

nmap("<Esc>", vim.cmd.nohl)
nmap("<Tab>", "<C-^>")
nmap("<C-i>", "<Tab>") -- Re-bind for jump list
-- nmap("<Leader>n", "<Cmd>windo set nu! relativenumber!<CR>")
-- nmap("<Leader>t", "<Cmd>windo set expandtab!<CR>")
-- nmap("<Leader>.", "<Cmd>w<CR>")
-- nmap("S", require("my.nodesplit").split)
nmap("[d", vim.diagnostic.goto_prev, "previous diagnostic")
nmap("]d", vim.diagnostic.goto_next, "next diagnostic")
nmap("[q", vim.cmd.cprevious, "quickfix previous")
nmap("]q", vim.cmd.cnext, "quickfix next")
nmap("<Leader>q", vim.diagnostic.setqflist, "set diagnostic to quickfix list")
-- nmap("<F2>", utils.reload, "reload configuration")
nmap("[t", vim.cmd.tabp, "previous tab")
nmap("]t", vim.cmd.tabn, "next tab")

-- imap("<Tab>", require("my.tab").forwards)
-- imap("<S-Tab>", require("my.tab").backwards)
imap("<C-E>", "<CR><ESC>O")
imap("jk", "<Esc>")

vmap("<Leader>p", '"_dP')
vmap(">", ">gv")
vmap("<", "<gv")

M.lsp_keymaps = function(client, bufnr)
    nmap("<Leader>r", vim.lsp.buf.rename, "Rename", bufnr)
    nmap("<Leader>cf", vim.lsp.buf.format, "Format document", bufnr)
    nmap("<Leader>ca", vim.lsp.buf.code_action, "Code actions", bufnr)
    nmap("<Leader>ci", vim.lsp.buf.implementation, "Go to implementation", bufnr)
    nmap("<Leader>D", vim.lsp.buf.type_definition, "Go to type definition", bufnr)
    nmap("gr", "<Cmd>Telescope lsp_references<CR>", "View references", bufnr)

    if client.server_capabilities.hoverProvider then
        nmap("K", vim.lsp.buf.hover, "LSP hover", bufnr)
    end
end

M.telescope_keymaps = function(fns)
    nmap("<C-p>", fns.project, "Find project files")
    nmap("<Leader>fn", fns.dotfiles, "Dotfiles")
    nmap("<Leader><C-p>", fns.builtin.git_files, "Find git files")
    nmap("<Leader>fg", fns.builtin.live_grep, "Live grep")
    nmap("<Leader>gs", fns.builtin.git_status, "Git status")
end

M.gitsigns_keymaps = function(bufnr)
    local gs = require "gitsigns"

    nmap("]c", function()
        if vim.wo.diff then
            return "]c"
        end
        vim.schedule(function()
            gs.next_hunk()
        end)
        return "<Ignore>"
    end, "next hunk", bufnr)

    nmap("[c", function()
        if vim.wo.diff then
            return "[c"
        end
        vim.schedule(function()
            gs.prev_hunk()
        end)
        return "<Ignore>"
    end, "previous hunk", bufnr)
end

return M