-- @author DoomTero 2026
-- @copyright 2026 DoomTero - ZomboRut original work. ref:88de6667
-- @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 setIcon = require("ZomboRut/ContextMenu/ZR_MenuUtil").icon

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

return function(ContextMenu, playerObj, context, worldobjects, getRoot)
	if not playerObj then return end

	if not isPlayerTile(playerObj, worldobjects) then return end

	if playerObj:isDraggingCorpse() then return end

	if ZR_Surrender.isActive(playerObj) then
		local phase = ZR_Surrender.getPhase(playerObj)
		if phase == "surrender" or phase == "collapse" then
			setIcon(getRoot():addOption(getText("ContextMenu_ZR_StandUp"), playerObj, function()
				ZR_Surrender.standUp(playerObj)
			end), "StandUp")
			return
		end
		setIcon(getRoot():addOption(getText("ContextMenu_ZR_GiveUp"), playerObj, function()
			ZR_Surrender.start(playerObj)
		end), "GiveUp")
		return
	end

	setIcon(getRoot():addOption(getText("ContextMenu_ZR_GiveUp"), playerObj, function()
		ZR_Surrender.start(playerObj)
	end), "GiveUp")
end
