local f = require("util.functions")
local u = require("util")
local c = u.create_cmd
local cmd, api, fn, d = vim.cmd, vim.api, vim.fn, vim.diagnostic

c("BufOnlyWindowOnly", function()
  if #api.nvim_list_wins() > 1 then
    cmd.only()
  end
  cmd("silent! BufOnly")
end)
c("WinOnly", function()
  f.win_only()
end)
c("WinOnlyFocusLeft", function()
  f.win_only(function()
    cmd("vsplit")
  end)
end)
c("WinOnlyFocusRight", function()
  f.win_only(function()
    cmd("vsplit")
    cmd.wincmd("h")
  end)
end)
c("WinOnlyFocusDown", function()
  f.win_only(function()
    cmd("split")
    cmd.wincmd("j")
  end)
end)
c("WinOnlyFocusUp", function()
  f.win_only(function()
    cmd("split")
    cmd.wincmd("k")
  end)
end)
c("CommentYankPaste", f.comment_yank_paste)
c("HelpWord", f.help_word)
c("LoadPreviousBuffer", function()
  if #fn.expand("#") > 0 then
    cmd.edit(fn.expand("#"))
  end
end)
c("ReloadLua", u.reload_lua)
c("SpectreOpen", function()
  cmd([[lua require("spectre").open()]])
end)
c("SpectreOpenWord", function()
  cmd([[lua require("spectre").open_visual({select_word = true})]])
end)
c("SpectreOpenCwd", function()
  cmd([[lua require("spectre").open_file_search()]])
end)
c("SpotifyNext", function()
  f.run_system_command({
    cmd = [[spotify_player playback next && spotify_player get key playback | jq -r '.item | "\(.artists | map(.name) | join(", ")) - \(.name)"']],
    notify = true,
    notify_config = { title = "Spotify", render = "compact" },
  })
end)
c("SpotifyPrev", function()
  f.run_system_command({
    cmd = [[spotify_player playback previous && spotify_player get key playback | jq -r '.item | "\(.artists | map(.name) | join(", ")) - \(.name)"']],
    notify = true,
    notify_config = { title = "Spotify", render = "compact" },
  })
end)
c("TagStackDown", function()
  f.tagstack_navigate({ direction = "down" })
end)
c("TagStackUp", function()
  f.tagstack_navigate({ direction = "up" })
end)
c("ToggleCmdHeight", f.toggle_cmdheight)
c("UpdateAndSyncAll", function()
  local cmds = {
    "MasonUpdate",
    "TSUpdate",
    "Lazy sync",
  }
  for _, cm in ipairs(cmds) do
    cmd(cm)
    vim.print(string.format("Running: %s", cm))
  end
end)
c("FoldParagraph", function()
  local foldclosed = fn.foldclosed(fn.line("."))
  if foldclosed == -1 then
    cmd([[silent! normal! zfip]])
  else
    cmd([[silent! normal! zo]])
  end
end)
c("HoverHandler", require("util").hover_handler)
c("TerminalSendCmd", function()
  f.terminal_send_cmd("ls")
end)
c("TabNext", function()
  f.tabnavigate({ navto = "next" })
end)
c("TabPrevious", function()
  f.tabnavigate({ navto = "prev" })
end)
c("DisableLSPFormatting", function()
  f.lsp_formatting({ enable = false })
end)
c("EnableLSPFormatting", function()
  f.lsp_formatting({ enable = true })
end)
c("DiagnosticsJumpPrev", function()
  f.diagnostics_jump({
    count = -1,
    float = true,
    severity = d.severity.HINT,
    scan_direction = -1,
  })
end)
c("DiagnosticsJumpNext", function()
  f.diagnostics_jump({
    count = 1,
    float = true,
    severity = d.severity.HINT,
    scan_direction = -1,
  })
end)
c("DiagnosticsErrorJumpPrev", function()
  f.diagnostics_jump({
    count = -1,
    float = true,
    severity = d.severity.ERROR,
    scan_direction = 1,
  })
end)
c("DiagnosticsErrorJumpNext", function()
  f.diagnostics_jump({
    count = 1,
    float = true,
    severity = d.severity.ERROR,
    scan_direction = 1,
  })
end)
c("QuickFixClear", function()
  vim.fn.setqflist({}, "r")
end)
c("TermOpenTab", function()
  f.term_open("tab term")
end)
c("TermOpenBottom", function()
  f.term_open("botright term")
end)
c("TermOpenBottomResized", function()
  f.term_open({
    "botright term",
    "botright resize 15%",
  })
end)
c("TermOpenBottomVert", function()
  f.term_open("horizontal term")
end)
c("TermOpenRight", function()
  f.term_open("vertical term")
end)
c("TermOpenRightResized", function()
  f.term_open({
    "vertical term",
    "vertical resize 50%",
  })
end)
c("MakeOpenQF", f.make_open_qf)
c("OneStepForVimKindLaunch", function()
  require("osv").launch({ port = 8086 })
end)

c("TestingFunction", f.testing_function)
