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

local findOnTilesAndNeighbors = require("ZomboRut/ContextMenu/ZR_MenuUtil").findOnTilesAndNeighbors
local setIcon = require("ZomboRut/ContextMenu/ZR_MenuUtil").icon

local function _isLiveZombie(m)
	return instanceof(m, "IsoZombie")
		and not m:isDead()
		and not (m.getVariableBoolean and m:getVariableBoolean("Bandit"))
end

local function _findLiveZombie(worldobjects)
	return findOnTilesAndNeighbors(worldobjects, "moving", _isLiveZombie)
end

local function onFillWorldObjectContextMenu(playerIndex, context, worldobjects, test)
	if test == true then return end
	if not (isDebugEnabled and isDebugEnabled()) then return end
	local playerObj = getSpecificPlayer(playerIndex)
	if not playerObj then return end

	if ZR_HeldDragController and ZR_HeldDragController.isActive(playerObj) then
		setIcon(context:addOption("[ZR DEBUG] Held Drag — Stop", worldobjects, function()
			ZR_HeldDragController.stop(playerObj)
		end), "Debug")
		return
	end

	local zombie = _findLiveZombie(worldobjects)
	if not zombie then return end

	setIcon(context:addOption("[ZR DEBUG] Held Drag", worldobjects, function()
		if ZR_HeldDragController and ZR_HeldDragController.start then
			ZR_HeldDragController.start(playerObj, zombie)
		else
			DebugLog.log("[ZR DEBUG] ZR_HeldDragController module missing")
		end
	end), "Debug")
end

Events.OnFillWorldObjectContextMenu.Add(onFillWorldObjectContextMenu)
