--[[
	Depending on the chosen ActionEvent, you can hook up to certain parts of the animation player, regardless if
	your own mod has initiated the animations first. ActionEvents are overarching functions that deals with
	more general logic and allows you to code in logic even if the animations are not owned by you. 

	ActionEvents.Perform - Runs whenever the animation successfully completes
	ActionEvents.Start - Runs whenever the animation starts
	ActionEvents.Stop - Runs whenever the animation is cancelled
	ActionEvents.Update - Runs whenever the animation is ongoing
	ActionEvents.WaitToStart - Runs whenever the animation is waiting to start

	-- @author QueuedResonance 2022
]]

local ZomboWin = require("ZomboWin/ZomboWin");
local ActionEvents = ZomboWin.AnimationHandler.ActionEvents;

local ZomboWinDefeat = require("ZomboWinDefeat/ZomboWinDefeat")
local ZomboWinDefeatConfig = ZomboWinDefeatConfig

-- Drinker traits settings
local isSober = false;					--noob drinker bool, is set to 'true' when alcohol <= 0.
local isSoberMultiplier = 2.0;			--noob drinker multiplier. Takes first alcohol change and multiply by (this), then sets 'isSober' 'false'.
local lightDrinkerMultiplier = 0.0004;		--light drinker, not a real multiplier! Adds (this) while alcohol > 0.
local heavyDrinkerMultiplier = 0.006;		--heavy drinker, not a real multiplier! Adds -(this) while alcohol > 0.

-- Innocent trait settings
local bNoPantsPanic = .09;
local bNoShirtsPanic = .09;

local clothingHandler = {};
clothingHandler.checkShirts = {"Dress","ShortSleeveShirt","Tshirt","Shirt","Sweater","SweaterHat","JacketHat","Jacket","BathRobe","FullTop","FullSuit","FullSuitHead",};
clothingHandler.checkPants = {"Skirt","Pants","Dress","BathRobe",};
clothingHandler.defeatImminent = {"Underwear","UnderwearBottom","UnderwearTop","BodyCostume","TankTop","Legs5","Skirt","Pants","Legs1","Dress","Torso1Legs1","ShortSleeveShirt","Tshirt","Shirt","Sweater","SweaterHat","JacketHat","Jacket","BathRobe","FullTop","TorsoExtra","FullSuit","Mask","Hat","MaskEyes","MaskFull","FullHat","FullSuitHead",}

-- Addiction
table.insert(ActionEvents.Update, function(action)
	local character = action.character
	local otherAction = action.otherAction

	local stats = character:getStats()
	local bodyDamage = character:getBodyDamage()
	local unhappiness = bodyDamage:getUnhappynessLevel()
	local boredom = bodyDamage:getBoredomLevel()
	local wetness = bodyDamage:getWetness()
	
	--- Gender hating logic
	--[[
	if otherAction then
		local otherCharacter = otherAction.character
		if character:HasTrait("Misogynist") then
			if otherCharacter:isFemale() then
				stats:setStress(stats:getStress() + 0.002)
				bodyDamage:setUnhappynessLevel(unhappiness + 0.005)
			end
		end

		if character:HasTrait("Misandrist") then
			if not otherCharacter:isFemale() then
				stats:setStress(stats:getStress() + 0.002)
				bodyDamage:setUnhappynessLevel(unhappiness + 0.005)
			end
		end
	end
	]]
	if character:HasTrait("Sexaddict") then
		stats:setStress(stats:getStress() - 0.002)
		bodyDamage:setUnhappynessLevel(unhappiness - 0.005)
	end

	--- Decrease boredom
	bodyDamage:setBoredomLevel(boredom - 0.005)
	--- Increase wetness ... maybe sweat, maybe something else is wet, who knows?
	if wetness < 40 then
		bodyDamage:setWetness(wetness + 0.1);
	end
	
end)

table.insert(ActionEvents.Perform, function(action)
    local character = action.character
    local stats = character:getStats()

    if character:HasTrait("Everydrop") then
        getSoundManager():PlayWorldSound('FeGulp01', true, character:getCurrentSquare(), 0, 4, 1, false)
        stats:setHunger(stats:getHunger() - 0.040)
    end
end)

local function _onPlayerUpdate(character)
	local stats = character:getStats();
	local bodyDamage = character:getBodyDamage();
	local unhappiness = bodyDamage:getUnhappynessLevel();
	local boredom = bodyDamage:getBoredomLevel();
	local stress = stats:getStress();
	local drunk = stats:getDrunkenness();
	local panic = stats:getPanic();
	local sickness = bodyDamage:getFoodSicknessLevel()
	
	if character:HasTrait("Traumatized") then
		if drunk < 10 then
			if unhappiness <= 60 then
				bodyDamage:setUnhappynessLevel(unhappiness + 0.045);
			elseif unhappiness > 60 and unhappiness < 100 then
				bodyDamage:setUnhappynessLevel(unhappiness + 0.0003); -- @60 slower
			end
			
			if stress <= 0.50 then
				stats:setStress(stress + 0.00045);
			elseif stress > 0.50 and stress < 1.0 then
				stats:setStress(stress + 0.00002); -- @50 slower
			end
			
		elseif drunk > 30 and drunk <= 50 then
			if unhappiness > 0 then
				bodyDamage:setUnhappynessLevel(unhappiness - 0.06);
			end
			if stress > 0 then
				stats:setStress(stress - 0.0006);
			end
		elseif drunk > 50 then
			if unhappiness > 0 then
				bodyDamage:setUnhappynessLevel(unhappiness - 0.12);
			end
			if stress > 0 then
				stats:setStress(stress - 0.0012);
			end
		end
	end
	
	-- Drinker traits
	
		-- Drinker noob
	if character:HasTrait("drinkerNoob") then
		if drunk > 0 and isSober then
			isSober = false;
			stats:setDrunkenness(drunk * isSoberMultiplier);
			if drunk > 100 then
				stats:setDrunkenness(100);
			end
		end
		
		if drunk > 50 and drunk <= 70 and sickness < 50 then
			bodyDamage:setFoodSicknessLevel(sickness + 0.0032)
		elseif drunk > 70 and sickness < 50 then
			bodyDamage:setFoodSicknessLevel(sickness + 0.0064)
		end
		-- can't debate if having a catchall is better than a condition check. Hmmmmm........
		--[[if sickness > 50 then
			bodyDamage:setFoodSicknessLevel(50)
		end]]
		-- Figured it out!
		
		
		-- this must stay down here
		if drunk == 0 then
			isSober = true;
		else
			isSober = false;
		end
	end
	
		-- Drinker lightweight
	if character:HasTrait("drinkerLight") then
		if drunk > 0 then
			stats:setDrunkenness(drunk + lightDrinkerMultiplier);
		end
		if drunk > 50 and drunk <= 70 and sickness < 50 then
			bodyDamage:setFoodSicknessLevel(sickness + 0.0064)
		elseif drunk > 70 and sickness < 50 then
			bodyDamage:setFoodSicknessLevel(sickness + 0.0128)
		end
		--[[if sickness > 50 then
			bodyDamage:setFoodSicknessLevel(50)
		end]]
	end
	
		-- Drinker heavyweight ... Note: You need to set min. cap limiters.
	if character:HasTrait("drinkerHeavy") then
		if drunk > 0 then
			stats:setDrunkenness(drunk - heavyDrinkerMultiplier);
		end
		if drunk > 30 and drunk <= 70 then
			bodyDamage:setFoodSicknessLevel(sickness - 0.0032) --its a game, it doesn't need to make sense.
			stats:setStress(stats:getStress() - 0.0006)
			bodyDamage:setUnhappynessLevel(unhappiness - 0.06)
		elseif drunk > 70 then
			bodyDamage:setFoodSicknessLevel(sickness - 0.0016) --but heres the kicker, being too drunk is bad.
			stats:setStress(stats:getStress() - 0.0003)
			bodyDamage:setUnhappynessLevel(unhappiness - 0.03)
		end
	end
	
	-- Innocent Naked, outdoors
	if character:HasTrait("Innocent") then
		--print("Character has Innocent trait.");
		if character:isOutside() then
			
			--print("Chracter is outside.");
			local player = getPlayer();
			local bNoPants = true;
			local bNoShirts = true;
			local bNoNothing = true;
			local wornItems = player:getWornItems();
			if wornItems then
				--print("Character is wearing something.");
			end
			if wornItems then
		--- Loop through the character's currently equipped items for shirts
				for i = wornItems:size(), 1, -1 do
					local wornItem = wornItems: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 shirts clothing list
							local isWearingShirt = false
							
							for v = 1, #clothingHandler.checkShirts do
								if bNoShirts then
									local bodyPart = clothingHandler.checkShirts[v]

									if location == bodyPart then
										bNoShirts = false;
										isWearingShirt = true;
									end
								end
							end
							
							if isWearingShirt then
								--print("Character is wearing a shirt.");
								break;
							end
						end
					end
				end
			end			
			if wornItems then
		--- Loop through the character's currently equipped items for pants
				for i = wornItems:size(), 1, -1 do
					local wornItem = wornItems: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
			if wornItems then
		--- Loop through the character's currently equipped items for anything!!!
				for i = wornItems:size(), 1, -1 do
					local wornItem = wornItems: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 isWearingNothing = false
							
							for v = 1, #clothingHandler.defeatImminent do
								if bNoNothing then
									local bodyPart = clothingHandler.defeatImminent[v]

									if location == bodyPart then
										bNoNothing = false;
										isWearingNothing = true;
									end
								end
							end
							
							if isWearingNothing then
								--print("Character is wearing at least something.");
								break;
							end
						end
					end
				end
			end
			-- Induce panic, no pants
			if bNoPants then
				if panic < 6 then
				stats:setPanic(6);
				end
				if panic < 30 then
					--print("No pants, panic increasing.");
					stats:setPanic(panic + bNoPantsPanic);
				end
			end
			-- Induce panic, no shirts; only females
			if bNoShirts and player:isFemale() then
				if panic < 6 then
				stats:setPanic(6);
				end
				if panic < 30 then
					--print("No shirts and female, panic increasing.");
					stats:setPanic(panic + bNoShirtsPanic);
				end
			end
			-- Induce panic, no anything; defeat imminent!
			if bNoNothing then
				if panic < 99 then
				stats:setPanic(100);
				end
			end
		end
	end
	
	-- Sex Addict
	if not character:getModData().ZomboWinSexScene then
		if character:HasTrait("Sexaddict") then
			bodyDamage:setUnhappynessLevel(unhappiness + 0.0002)
		end
	end
end

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

return clothingHandler;