-- @author SolarEdge 2023

local timedHandler = {}

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

-- Traumatized trait settings
local hoursSinceLastDefeat = 0;
local hoursToNormal = 0;
local durationDecode = 0;

local function traumatizeTimer()
	-- Traumatized Counter
	local character = getPlayer();
	if character == nil then
		return
	end	
		
	if character:HasTrait("Traumatized") then
		hoursSinceLastDefeat = (hoursSinceLastDefeat + 1)

		durationDecode = (ZomboWinDefeatConfig.ModOptions.options.dropdown5 * 0.1)
		if durationDecode == 0.1 then
			hoursToNormal = 0; hoursSinceLastDefeat = 0;-- None
		elseif durationDecode == 0.2 then 
			hoursToNormal = 3 + ZombRand(3); -- 3-6 hours
		elseif durationDecode == 0.3 then 
			hoursToNormal = 6 + ZombRand(6); -- 6-12 hours
		elseif durationDecode == 0.4 then 
			hoursToNormal = 24 + ZombRand(24); -- 1-2 days
		elseif durationDecode == 0.5 then 
			hoursToNormal = 72 + ZombRand(48); -- 3-5 days
		elseif durationDecode == 0.6 then 
			hoursToNormal = 120 + ZombRand(48); -- 5-7 days
		end
		
		if hoursSinceLastDefeat >= hoursToNormal then
			HaloTextHelper.addTextWithArrow(character, getText("Traumatized"), false, HaloTextHelper.getColorGreen());
			character:getTraits():remove("Traumatized")
			hoursSinceLastDefeat = 0;
			return
		end
	end
end

local function TestingFunction()
	print("Calling testing function!!")
	if hoursSinceLastDefeat ~= nil then
		if hoursSinceLastDefeat >= 0 then
			local hoursString = tostring(hoursSinceLastDefeat);
			print(hoursString);
		end
	elseif hoursSinceLastDefeat == nil then
		print("hoursSinceLastDefeat is nil.");
	end
end

Events.EveryHours.Add(traumatizeTimer);		--will make controller when more functions are added.
--Events.OnPlayerUpdate.Add(TestingFunction) 

return timedHandler;