-- @author SolarEdge 2024

local function _withCute(zombie)
	local player = getPlayer();
	if not player:HasTrait("unblessing") then return; end
	
	local isMainHeroFemale = player:isFemale()
	local zombieIsFemale = zombie:isFemale()
	
	if isMainHeroFemale and zombieIsFemale then return; end
end

local function _withSexperiment(zombie)
	local player = getPlayer();
	if not player:HasTrait("unblessing") then return; end
	--if not zombie:isUseless() then return; end
	
	if zombie:isUseless() and zombie:getModData().isProvoked and not zombie:getModData().isWatching and not zombie:getModData().isSubdued then 
		--[[print("A zombie has been provoked.");]] 
		HaloTextHelper.addText(player, getText("Zombie provoked!"), HaloTextHelper.getColorRed());
		zombie:setUseless(false); 
		return; 
	end
	--local isProvoked = isProvoked or false;
	local isMainHeroFemale = player:isFemale()
	local zombieIsFemale = zombie:isFemale()
	
	local function setUnprovoked(zombie)
		if not zombie:isUseless() and not zombie:getModData().isProvoked then
			--print("Zombie set unprovoked.");
			zombie:setUseless(true);
		end
	end
	
	local function isProvoked(zombie)
		zombie:getModData().isProvoked = zombie:getModData().isProvoked;
	end
	
	if not zombie:isUseless() and not isProvoked(zombie) then
		if isMainHeroFemale and zombieIsFemale then setUnprovoked(zombie); end
		if not isMainHeroFemale and not zombieIsFemale then setUnprovoked(zombie); end
	end
end

local function _withSexOHZ(source, target)
	local idPlayer = source ~= nil and target ~= nil and source:isZombie() ~= target:isZombie();
	if idPlayer then 
		local zombie = source;
		if target:isZombie() then
			zombie = target;
			if not zombie:isUseless() or zombie:getModData().isWatching or zombie:getModData().isSubdued then return; end
			--if not character:HasTrait("unblessing") then return; end
			zombie:getModData().isProvoked = true;
			--print("S: On hit detected.");
		end
	end
end

local function _withSexOOC(source, target)
	local idPlayer = source ~= nil and target ~= nil and source:isZombie() ~= target:isZombie();
	if idPlayer then 
		local zombie = source;
		if target:isZombie() then
		zombie = target;
			if not zombie:isUseless() then return; end
			--if not character:HasTrait("unblessing") then return; end
			zombie:getModData().isProvoked = true;
			--print("S: OOC detected.");
		end
	end
end

--- Hook up event listeners
--Events.OnZombieUpdate.Add(_withCute)
Events.OnZombieUpdate.Add(_withSexperiment)
Events.OnWeaponHitCharacter.Add(_withSexOHZ)
Events.OnCharacterCollide.Add(_withSexOOC)