require "ISUI/ISWorldObjectContextMenu"
require "W900_HydrantWater"

local W900HydrantContext = {}

function W900HydrantContext.findHydrant(worldobjects)
	if not worldobjects then return nil end

	for _, obj in ipairs(worldobjects) do
		if W900HydrantWater.isHydrantObject(obj) then
			return obj
		end

		local square = nil
		if obj.getSquare then
			square = obj:getSquare()
		end
		if square and square:getObjects() then
			for i=0, square:getObjects():size()-1 do
				local squareObj = square:getObjects():get(i)
				if W900HydrantWater.isHydrantObject(squareObj) then
					return squareObj
				end
			end
		end
	end

	return nil
end

function W900HydrantContext.onFillWorldObjectContextMenu(player, context, worldobjects, test)
	local hydrant = W900HydrantContext.findHydrant(worldobjects)
	if not hydrant then return end

	if test then return true end

	local label = W900HydrantWater.getAvailabilityLabel(hydrant)
	if not label then return end

	local option = context:addOption(label, nil, nil)
	option.notAvailable = true
end

Events.OnFillWorldObjectContextMenu.Add(W900HydrantContext.onFillWorldObjectContextMenu)

return W900HydrantContext