-- @author DoomTero 2026

require "ZomboRut/ZR_Surrender"

local function isPlayerTile(playerObj, worldobjects)
	if not playerObj or not worldobjects then return false end
	local psq = playerObj:getCurrentSquare()
	if not psq then return false end
	for i = 1, #worldobjects do
		local obj = worldobjects[i]
		if obj and obj.getSquare and obj:getSquare() == psq then
			return true
		end
	end
	return false
end

local function onFillWorldObjectContextMenu(playerIndex, context, worldobjects, test)
	if test == true then return end

	local playerObj = getSpecificPlayer(playerIndex)
	if not playerObj then return end

	if SandboxVars and SandboxVars.ZomboRut
		and SandboxVars.ZomboRut.enableMod == false
	then
		return
	end

	if not isPlayerTile(playerObj, worldobjects) then return end

	if playerObj:isDraggingCorpse() then return end

	if ZR_Surrender.isActive(playerObj) then
		if ZR_Surrender.getPhase(playerObj) == "surrender" then
			context:addOption("ZR: Stand Up", playerObj, function()
				ZR_Surrender.standUp(playerObj)
			end)
			return
		end
		context:addOption("ZR: Give Up", playerObj, function()
			ZR_Surrender.start(playerObj)
		end)
		return
	end

	context:addOption("ZR: Give Up", playerObj, function()
		ZR_Surrender.start(playerObj)
	end)
end

Events.OnFillWorldObjectContextMenu.Add(onFillWorldObjectContextMenu)
