-- @author QueuedResonance 2022

local Animations = {}

local ZomboRutActType = ZomboRutActType
local ZomboRutAnimationData = ZomboRutAnimationData

local string = string
local pairs = pairs
local type = type

local animationPairs = {
	Consent_BJ_M = "Consent_BJ_F",
	Consent_Cowgirl_M = "Consent_Cowgirl_F",
	Consent_Doggy_M = "Consent_Doggy_F",
	Consent_Missionary_M = "Consent_Missionary_F",
	Consent_OralDomination_M = "Consent_OralDomination_F",
	Consent_SideFuck_M = "Consent_SideFuck_F",
	Consent_Tower_M = "Consent_Tower_F",
	Defeat_Doggy_M = "Defeat_Doggy_F",
	Defeat_DoggyDomination_M = "Defeat_DoggyDomination_F",
	Defeat_KneeBJ_M = "Defeat_KneeBJ_F",
	Defeat_MissionaryFear_M = "Defeat_MissionaryFear_F",
	Defeat_Stand_Fuck_M = "Defeat_Stand_Fuck_F",
	male1 = "female1",
	male2 = "female2",
	male4 = "female4",
	male6 = "female6",
	male7 = "female7",
	blabla_PreDefeat_f2b_01_M = "blabla_PreDefeat_f2b_01_F",
	blabla_PreDefeat_f2f_01_M = "blabla_PreDefeat_f2f_01_F",
	
	Consent_BJ_F = "Consent_BJ_M",
	Consent_Cowgirl_F = "Consent_Cowgirl_M",
	Consent_Doggy_F = "Consent_Doggy_M",
	Consent_Missionary_F = "Consent_Missionary_M",
	Consent_OralDomination_F = "Consent_OralDomination_M",
	Consent_SideFuck_F = "Consent_SideFuck_M",
	Consent_Tower_F = "Consent_Tower_M",
	Defeat_Doggy_F = "Defeat_Doggy_M",
	Defeat_DoggyDomination_F = "Defeat_DoggyDomination_M",
	Defeat_KneeBJ_F = "Defeat_KneeBJ_M",
	Defeat_MissionaryFear_F = "Defeat_MissionaryFear_M",
	Defeat_Stand_Fuck_F = "Defeat_Stand_Fuck_M",
	female1 = "male1",
	female2 = "male2",
	female4 = "male4",
	female6 = "male6",
	female7 = "male7",
	blabla_PreDefeat_f2b_01_F = "blabla_PreDefeat_f2b_01_M",
	blabla_PreDefeat_f2f_01_F = "blabla_PreDefeat_f2f_01_M"
}

function Animations:tableFind(list, element)
	for i = 1, #list do
		local item = list[i]
		if item == element then
			return item
		end
	end
end

function Animations:getAnimations(actorCount, males, females, tagsToSearch, tagsBlacklist, allTagsRequired)
	males = males or -1
	females = females or -1
	tagsToSearch = tagsToSearch or {}
	tagsBlacklist = tagsBlacklist or {}
	allTagsRequired = allTagsRequired or true

	local viableAnimations = {}

	for i = 1, #ZomboRutAnimationData do
		local animation = ZomboRutAnimationData[i]
		local tags = animation.tags
		local tagsFound = 0
		local canAdd = true

		if #animation.actors == actorCount then
			for _, blacklistedTag in ipairs(tagsBlacklist) do
				if Animations:tableFind(tags, blacklistedTag) then
					canAdd = false
					break
				end
			end

			for _, searchTag in ipairs(tagsToSearch) do
				if Animations:tableFind(tags, searchTag) then
					tagsFound = tagsFound + 1
				end
			end

			if allTagsRequired and tagsFound < #tagsToSearch then
				canAdd = false
			end

			local maleCount = 0
			local femaleCount = 0
			for _, actor in ipairs(animation.actors) do
				if actor.gender == "Male" then
					maleCount = maleCount + 1
				elseif actor.gender == "Female" then
					femaleCount = femaleCount + 1
				end
			end

			if males >= 0 and maleCount ~= males then
				canAdd = false
			end

			if females >= 0 and femaleCount ~= females then
				canAdd = false
			end

			if canAdd then
				table.insert(viableAnimations, animation)
			end
		end
	end

	return viableAnimations
end

function Animations:getZLAnimations(list, isFemale, isConsensual, isZombie)
	local viableAnimations = {}

	for _, animation in ipairs(list) do
		local data = ZomboRutAnimationData[animation]

		if data then
			local canAdd = true

			if isFemale then
				if not data.Animations.Female then
					canAdd = false
				end
			else
				if not data.Animations.Male then
					canAdd = false
				end
			end

			if isZombie ~= nil and isZombie ~= data.IsZombieAllowed then
				canAdd = false
			end

			if isConsensual ~= nil and isConsensual ~= data.IsConsensual then
				canAdd = false
			end

			if canAdd then
				table.insert(viableAnimations, {Key = animation, Data = data})
			end
		end
	end

	return viableAnimations
end

return Animations