-- @author SolarEdge 2023

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

-- Traumatized trait settings
--local player:getModData().timedHandler.hoursSinceLastDefeat = 0;
local hoursToNormal = 0;
local durationDecode = 0;

local function initData()
	local player = getPlayer();
	player:getModData().timedHandler = player:getModData().timedHandler or {};
	player:getModData().timedHandler.hoursSinceLastDefeat = player:getModData().timedHandler.hoursSinceLastDefeat or 0;
end

local function traumatizeTimer()
	-- Traumatized Counter
	local player = getPlayer();
	if player == nil then
		return;
	end	
		
	if player:HasTrait("Traumatized") then
		--print("Traumatized - 1!");
		initData();
		player:getModData().timedHandler.hoursSinceLastDefeat = player:getModData().timedHandler.hoursSinceLastDefeat + 1

		durationDecode = (ZomboWinDefeatConfig.ModOptions.options.dropdown5 * 0.1)
		if durationDecode == 0.1 then
			hoursToNormal = 0; player:getModData().timedHandler.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 player:getModData().timedHandler.hoursSinceLastDefeat >= hoursToNormal then
			HaloTextHelper.addTextWithArrow(player, getText("Traumatized"), false, HaloTextHelper.getColorGreen());
			player:getTraits():remove("Traumatized")
			player:getModData().timedHandler.hoursSinceLastDefeat = 0;
			return
		end
	end
end

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

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