-- @author DoomTero 2026
-- @copyright 2026 DoomTero - ZomboRut original work. ref:129bc409
-- @license Proprietary. Not licensed for reuse, redistribution or derivative works without written permission from DoomTero. Ask - permission is obtainable. See ZomboRut/LICENSE.txt

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
		local phase = ZR_Surrender.getPhase(player)
		if phase == "surrender" or phase == "collapse" then
			ZR_Surrender.standUp(player)
			return
		end
	end

	ZR_Surrender.start(player)
end

Events.OnKeyPressed.Add(onKeyPressed)
