hs.loadSpoon("SpoonInstall")
spoon.SpoonInstall.use_syncinstall = true
spoon.SpoonInstall:andUse("WindowScreenLeftAndRight")
spoon.SpoonInstall:andUse("WindowHalfsAndThirds")
spoon.SpoonInstall:andUse("Commander")
spoon.SpoonInstall:andUse("ReloadConfiguration")

-- Keep a running, and preferred terminal as the _last_ value here
TERMINAL_NAMES = { "wezterm-gui", "WezTerm" }

spoon.WindowScreenLeftAndRight:bindHotkeys(spoon.WindowScreenLeftAndRight.defaultHotkeys)

spoon.WindowHalfsAndThirds:bindHotkeys(spoon.WindowHalfsAndThirds.defaultHotkeys)

spoon.ReloadConfiguration:start()

pcall(require, "initlocal")

hs.hotkey.bind({ "cmd", "alt", "ctrl" }, "SPACE", function()
	spoon.Commander.show()
end)

hs.hotkey.bind({ "ctrl" }, "t", function()
	terminal = nil
	for i = 1, #TERMINAL_NAMES do
		-- Can't use .get here, as there's an open bug where it just… returns a window instead sometimes
		if hs.application.find(TERMINAL_NAMES[i], true) then
			terminal = hs.application.find(TERMINAL_NAMES[i], true)
			break
		end
	end
	local currentApp = hs.window.focusedWindow():application()
	print(currentApp)
	if terminal then
		if currentApp:name() == terminal:name() then
			hs.eventtap.keyStroke({ "ctrl" }, "t", 1000, currentApp)
		else
			terminal:activate()
		end
	else
		hs.application.launchOrFocus(TERMINAL_NAMES[#TERMINAL_NAMES])
	end
end)

hs.hotkey.bind({ "cmd", "alt" }, ",", function()
	hs.application.launchOrFocus("System Preferences")
end)

hs.hotkey.bind({ "cmd", "shift" }, "l", function()
	hs.application.launchOrFocus("Launchpad")
end)