-- @author DoomTero 2026

require "ZomboRut/ZR_Surrender"
local ZomboRutScene = require "ZomboRut/ZomboRutScene"

local KEY_TABLE = {
	[1]  = Keyboard.KEY_G,
	[2]  = Keyboard.KEY_F,
	[3]  = Keyboard.KEY_V,
	[4]  = Keyboard.KEY_B,
	[5]  = Keyboard.KEY_X,
	[6]  = Keyboard.KEY_Q,
	[7]  = Keyboard.KEY_E,
	[8]  = Keyboard.KEY_R,
	[9]  = Keyboard.KEY_T,
	[10] = Keyboard.KEY_Y,
	[11] = Keyboard.KEY_U,
	[12] = Keyboard.KEY_H,
	[13] = Keyboard.KEY_J,
	[14] = Keyboard.KEY_K,
	[15] = Keyboard.KEY_L,
}

local function getConfiguredKey()
	local idx = 14
	if SandboxVars and SandboxVars.ZomboRut
		and SandboxVars.ZomboRut.surrenderHotkey
	then
		idx = SandboxVars.ZomboRut.surrenderHotkey
	end
	return KEY_TABLE[idx] or Keyboard.KEY_K
end

local function onKeyPressed(key)
	if SandboxVars and SandboxVars.ZomboRut
		and SandboxVars.ZomboRut.enableMod == false
	then
		return
	end

	if key ~= getConfiguredKey() then return end

	local player = getPlayer()
	if not player or not player:isAlive() then return end

	if ISChat and ISChat.instance and ISChat.instance.focused then return end

	if player:isDraggingCorpse() then
		if SandboxVars and SandboxVars.ZomboRut
			and SandboxVars.ZomboRut.cancelSceneOnHotkey == true
			and ZomboRutScene and ZomboRutScene.endScene
		then
			ZomboRutScene.endScene(player, "PlayerCancel")
		end
		return
	end

	if ZR_Surrender.isActive(player) then
		if ZR_Surrender.getPhase(player) == "surrender" then
			ZR_Surrender.standUp(player)
			return
		end
	end

	ZR_Surrender.start(player)
end

Events.OnKeyPressed.Add(onKeyPressed)
