--- Handles asking for sex with players and NPCs
-- @author QueuedResonance 2022

local ZomboWinActType = ZomboWinActType

local ISWorldObjectContextMenu = ISWorldObjectContextMenu
local ISContextMenu = ISContextMenu
local ISToolTip = ISToolTip

local getText = getText
local string = string

local ListedPositions = { --- Add more if there will be more context menu options
	"Oral",
	"Intercourse",
	"Consent",
}

local clothingHandler = {};
clothingHandler.checkPants = {"Underwear","UnderwearBottom","Pants","FullSuit",};

--- Activates when the player clicks ask for seks on the chosen target
-- @worldobjects table of world objects nearby the player
-- @param ContextMenu object injected from ZomboWinContextMenu, can access ZomboWin functionalities with this
-- @param IsoPlayer object of the player
-- @param IsoPlayer object of the asked target
local function onAskForSex(worldobjects, contextMenu, position, requestor, target)
	local isMainHeroFemale = requestor:isFemale()
	local isTargetFemale = target:isFemale()
	local sPosition = ListedPositions[position]
	local maleCount, femaleCount = 0, 0

	if isMainHeroFemale and isTargetFemale then
		--- Lesbian
		maleCount = 0
		femaleCount = 2
	elseif isMainHeroFemale == false and isTargetFemale == false then
		--- Gay
		maleCount = 2
		femaleCount = 0
	else
		--- Straight
		maleCount = 1
		femaleCount = 1
	end

	--- Choose random animation as a test
	--temp check to see if m2m since no aminations 
	if maleCount ~= 2 and femaleCount ~= 2 then
	local animationList = contextMenu.Client.AnimationUtils:getAnimations(2, maleCount, femaleCount, {sPosition})
	local index = ZombRand(1, #animationList + 1)
	local chosenAnimation = animationList[index]

	contextMenu.Client.AnimationHandler.Play(worldobjects, {requestor, target}, chosenAnimation)
	end
end

--- Creates a ask for seks context menu
-- @param ContextMenu object injected from ZomboWinContextMenu, can access ZomboWin functionalities with this
-- @param IsoPlayer object
-- @context Context menu object to be filled for
-- @worldobjects table of world objects nearby the player
return function(ContextMenu, playerObj, context, worldobjects)
	if not ZomboWinConfig.ModOptions.options.box1 then return end
	local iIndex = 0

	--I think this checks for no pants on our player. Will test later.
	local bNoPants = true
	local player = playerObj:getPlayerNum()

	--- Activate native fetch function to determine if our mouse selected a square containing an IsoPlayer
	ISWorldObjectContextMenu.clearFetch()
	for _, v in ipairs(worldobjects) do
		ISWorldObjectContextMenu.fetch(v, player, true)
	end

		--print("clicked detected"); -- detects universal right click, unfortunately not my intended function. Need right click on player to activate masturbation only.
	--- Check if we have moused over a IsoPlayer
	if clickedPlayer then	-- only workes for others, not yourself.
		--print("isoplayer detected");
		local pp = getPlayer();
		if pp ~= clickedPlayer then ----- this will differentiate clicking self or clicking another player -----
			-- This will check other persons clothing.
			local iTheirClothes = clickedPlayer:getWornItems()
			if iTheirClothes then
			--- Loop through the character's currently equipped items for pants
				for i = iTheirClothes:size(), 1, -1 do
					local wornItem = iTheirClothes:get(i - 1) --- Get the current worn item

					if wornItem then
						local item = wornItem:getItem() --- Receive item data
						local location = wornItem:getLocation() --- Receive location of where this item is located on the body
						if item:IsClothing() then --- Make sure it is a clothing piece
							--- Iterate through the pants clothing list
							local isWearingPants = false
							
							for v = 1, #clothingHandler.checkPants do
								if bNoPants then
									local bodyPart = clothingHandler.checkPants[v]

									if location == bodyPart then
										bNoPants = false;
										isWearingPants = true;
									end
								end
							end
							
							if isWearingPants then
								--print("Character is wearing a pants.");
								break;
							end
						end
					end
				end
			end
			
			-- This will check your persons clothing.
			local myClothes = pp:getWornItems();
			if myClothes then
			--- Loop through the character's currently equipped items for pants
				for i = myClothes:size(), 1, -1 do
					local wornItem = myClothes:get(i - 1) --- Get the current worn item

					if wornItem then
						local item = wornItem:getItem() --- Receive item data
						local location = wornItem:getLocation() --- Receive location of where this item is located on the body
						if item:IsClothing() then --- Make sure it is a clothing piece
							--- Iterate through the pants clothing list
							local isWearingPants = false
							
							for v = 1, #clothingHandler.checkPants do
								if bNoPants then
									local bodyPart = clothingHandler.checkPants[v]

									if location == bodyPart then
										bNoPants = false;
										isWearingPants = true;
									end
								end
							end
							
							if isWearingPants then
								--print("Character is wearing a pants.");
								break;
							end
						end
					end
				end
			end
		else
			print("Solo play detected!");
			-- Editor Note --
			--Should not be treated as an "on click" function because ZLForce is called when the context menu is already opened, meaning the click already occured.
			--Meaning this method cannot be used as a masterbation option, assuming you want the option to ONLY show when the player right clicks their own character.
		end
		
		if bNoPants == true then	----- Ultimate Catch-all: if this returns true then a player is wearing pants -----	
			local SexOption = context:addOption(getText("ContextMenu_Force"), worldobjects)	
			local SexSubMenu = ISContextMenu:getNew(context)	
			context:addSubMenu(SexOption, SexSubMenu)	
			
			for iPosition = 1, #ListedPositions do	
				local text = getText(string.format("ContextMenu_Force_%s", ListedPositions[iPosition]))
				if text then	
					--- Create the new sub option onAskForSex(worldobjects, contextMenu, position ,requestor, target)
					local animationOption = SexSubMenu:addOption(text, worldobjects, onAskForSex, ContextMenu, iPosition, playerObj, clickedPlayer)
					-- animationOption.toolTip = toolTip
				else
					print(string.format("Missing Text: %s", anim))
				end
			end		
		else 
			local SexOption = context:addOption(getText("ContextMenu_Force"), worldobjects)		
			local tooltip = ISToolTip:new();
			tooltip:initialise();
			tooltip:setVisible(false);
			if bNoPants == false then
				SexOption.notAvailable = true;
				description = " <RED>" .. getText("ContextMenu_Force_Pants");
				tooltip.description = description
				SexOption.toolTip = tooltip
			end
		end		
	end
end