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

require "ISUI/ISContextMenu"
require "ISUI/ISToolTip"
require "ISUI/ISWorldObjectContextMenu"

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

local ContextMenu = {}

local MENU_MODULES = {
	"ZLMain",
	"ZR_SceneMenu",
	"ZR_AskForSexMenu",
	"ZR_SubdueMenu",
	"ZR_SubduedSceneMenu",
	"ZR_UnsubdueMenu",
	"ZR_SurrenderMenu",
}
local loaded   = {}
local isLoaded = false

local function ensureLoaded()
	if isLoaded then return end
	for i = 1, #MENU_MODULES do
		loaded[i] = require("ZomboRut/ContextMenu/" .. MENU_MODULES[i])
	end
	isLoaded = true
end

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

	local player = getSpecificPlayer(playerIndex)
	if not player or player:isAsleep() or player:getVehicle() then return end

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

	ensureLoaded()

	local rootSub
	local function getRoot()
		if rootSub then return rootSub end
		local rootOption = context:addOption(getText("ContextMenu_Main"), worldobjects, nil)
		setIcon(rootOption, "Main")
		rootSub = ISContextMenu:getNew(context)
		context:addSubMenu(rootOption, rootSub)
		return rootSub
	end

	for i = 1, #loaded do
		loaded[i](ContextMenu, player, context, worldobjects, getRoot)
	end
end

Events.OnFillWorldObjectContextMenu.Add(build)

return ContextMenu
