--[[
  Overview of ActionEvents hooks for animation sequences:
    • Perform      – after animation completes successfully
    • Start        – when animation begins
    • Stop         – if animation is cancelled
    • Update       – each frame while animation runs
    • WaitToStart  – while animation is pending start
]]

local ZomboLewd = require("ZomboLewd/ZomboLewd")
local ActionEvents = ZomboLewd.AnimationHandler.ActionEvents
local ZomboLewdModOptions = PZAPI.ModOptions:getOptions("ZomboLewdFramework")

-- Adjusts stress, unhappiness, boredom etc during ongoing animations
table.insert(ActionEvents.Update, function(action)
	local character = action.character
	if not action.character:isZombie() then 		
		local otherAction = action.otherAction
		local stats = character:getStats()
		local bodyDamage = character:getBodyDamage()
		local unhappiness = stats:get(CharacterStat.UNHAPPINESS)
		local stress = stats:get(CharacterStat.STRESS)
		local panic = stats:get(CharacterStat.PANIC)
		local boredom = stats:get(CharacterStat.BOREDOM)
		local wetness = stats:get(CharacterStat.WETNESS)

		--- Gender hating logic
		if action.otherActions then
			for _, otherAction in ipairs(action.otherActions) do
				local otherCharacter = otherAction.character

				if character:hasTrait(ZombolewdRegistries.Misogynist) then
					if otherCharacter:isFemale() then
						stress = stress +0.002
						unhappiness = unhappiness + 0.005
					end
				end

				if character:hasTrait(ZombolewdRegistries.Misandrist) then
					if not otherCharacter:isFemale() then
						stress = stress +0.002
						unhappiness = unhappiness + 0.005
					end
				end
			end
		end

		if character:hasTrait(ZombolewdRegistries.Sexaddict) then
			stress = stress -0.002
			unhappiness = unhappiness - 0.005
		end
		if (ZomboLewdModOptions:getOption("box_SFitness"):getValue()) then
			--DebugLog.log("[Zombolewd] Improve Fitness (+0.05) and Strength(+0.02)")
			character:getXp():AddXP(Perks.Fitness, 0.05)
			character:getXp():AddXP(Perks.Strength, 0.02)
		end
		--- Decrease stress (grow up because zombie is near)
		stress = stress -0.002
		panic = panic -0.002
		--- Decrease boredom
		boredom = boredom - 0.002
		--- Increase wetness
		if wetness < 40 then
			wetness = wetness + 0.1
		end	
		stats:set(CharacterStat.UNHAPPINESS, unhappiness)
		stats:set(CharacterStat.STRESS,stress)
		stats:set(CharacterStat.PANIC,panic)
		stats:set(CharacterStat.BOREDOM,boredom)
		stats:set(CharacterStat.WETNESS,wetness)
	end
end)

table.insert(ActionEvents.Perform, function(action)
	DebugLog.log("[Zombolewd] /ApplyBasicTraits.lua/ perform")
	local character = action.character
	local otherAction = action.otherAction
	if not character:isZombie() then 
		local stats = character:getStats()	
		local bodyDamage = character:getBodyDamage()
		local unhappiness = stats:get(CharacterStat.UNHAPPINESS)
		local stress = stats:get(CharacterStat.STRESS)
		local panic = stats:get(CharacterStat.PANIC)		
		if action.otherActions then
			for _, otherAction in ipairs(action.otherActions) do
				local otherCharacter = otherAction.character
				if otherCharacter:isZombie() then
					if action.character:hasTrait(ZombolewdRegistries.Necrophiliac) then
						DebugLog.log("[Zombolewd] Apply Necrophiliac trait")
						unhappiness = unhappiness - 20
						stress = stress -0.01	
						panic = panic -0.005	
					end
					if ZomboLewdModOptions:getOption("box_SCureZ"):getValue() then
						DebugLog.log("[Zombolewd] Try to cure zombification")  
						DebugLog.log(string.format("ZOMBIE_INFECTION level: %s", tostring(stats:get(CharacterStat.ZOMBIE_INFECTION))))   
						DebugLog.log(string.format("ZOMBIE_FEVER level: %s", tostring(stats:get(CharacterStat.ZOMBIE_FEVER))))
						if stats:get(CharacterStat.ZOMBIE_INFECTION) > 0 then							
							-- stats:set(CharacterStat.ZOMBIE_INFECTION, 0) --сбрасывает на 0 но не сохраняет
							stats:reset(CharacterStat.ZOMBIE_INFECTION)
							DebugLog.log(string.format("ZOMBIE_INFECTION new level: %s", tostring(stats:get(CharacterStat.ZOMBIE_INFECTION))))
						end
					end
				end
				if action.character:hasTrait(ZombolewdRegistries.Succuby) then	
					DebugLog.log("[Zombolewd] Apply Succuby trait")		
					otherCharacter:setVariable("MustDie", true)	
					bodyDamage:RestoreToFullHealth()
				end
			end
		end
		stats:set(CharacterStat.UNHAPPINESS, unhappiness)
		stats:set(CharacterStat.STRESS,stress)
	end
end)

-- Incremental unhappiness when idle for sex addicts
local function _onPlayerUpdate(character)
	if not character:isZombie() then 
		local stats = character:getStats()
		local unhappiness = stats:get(CharacterStat.UNHAPPINESS)

		if not character:getModData().zomboLewdSexScene then
			if character:hasTrait(ZombolewdRegistries.Sexaddict) then
				unhappiness = unhappiness + 0.0002
				stats:set(CharacterStat.UNHAPPINESS, unhappiness)
			end			
		end		
	end	
end

--- Hook up event listeners
Events.OnPlayerUpdate.Add(_onPlayerUpdate) 