-- @author SolarEdge 2023

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

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

local function initData()
	local player = getPlayer();
	local optionMax = (ZomboWinDefeatConfig.ModOptions.options.dropdown16)
	local slimitMax = 0;
	local limitMax = 0;
	player:getModData().timedHandler = player:getModData().timedHandler or {};
	player:getModData().timedHandler.hoursSinceLastDefeat = player:getModData().timedHandler.hoursSinceLastDefeat or 0;
	player:getModData().slimitMax = player:getModData().slimitMax or 1;
	
	if slimitMax ~= nil then 
		--- codes here ---
		if optionMax == 2 then
			limitMax = 1;
		elseif optionMax == 3 then
			limitMax = 2;
		end
		if player:HasTrait("swinger") then limitMax = (limitMax * 2); end
		if player:HasTrait("promiscuous") then limitMax = -1; end
		if player:HasTrait("Rapist") then limitMax = -1; end
		
		if optionMax == 1 then limitMax = -1; end
		
		player:getModData().slimitMax = limitMax; 
		Check(player);
	else
		--print("initData() has nil slimitMax.");
	end
end


local function limitTimer(player)
	local hoursToWait = 12
	local slimitMax = player:getModData().slimitMax

	player:getModData().byCount = player:getModData().byCount or {};
	local byCount = player:getModData().byCount
	
	if ZomboWinDefeatConfig.ModOptions.options.dropdown18 == 1 then
		hoursToWait = 6;
	elseif ZomboWinDefeatConfig.ModOptions.options.dropdown18 == 2 then
		hoursToWait = 12;
	elseif ZomboWinDefeatConfig.ModOptions.options.dropdown18 == 3 then
		hoursToWait = 8;
	else
		hoursToWait = 24;
	end
		
	local function updateHistory(player)
		if #byCount > 0 then
			for i = #byCount, 1, -1 do -- Iterate backwards to safely remove items
				local oHistory = byCount[i]
				if oHistory and instanceof(oHistory, "IsoZombie") then
					oHistory:getModData().sFoodsNew = oHistory:getModData().sFoodsNew or 0;
					oHistory:getModData().sFoodsNew = oHistory:getModData().sFoodsNew + 1;
					if oHistory:getModData().sFoodsNew >= hoursToWait then
						oHistory:getModData().sFoodsNew = 0;
						oHistory:getModData().sFoods = 0;
						table.remove(byCount, i);
						HaloTextHelper.addTextWithArrow(player, getText("Sexual Limit Count: ") .. #byCount .. getText(" of ") .. player:getModData().slimitMax, false, HaloTextHelper.getColorGreen());
					end
				end
			end
		end
	end
	
	updateHistory(player);
	Check(player);
end

local function traumatizeTimer()
	-- Traumatized Counter
	local player = getPlayer();
	if player == nil then
		return;
	end	
	
	initData();
	limitTimer(player);
		
	if player:HasTrait("Traumatized") then
		--print("Traumatized - 1!");
		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(4); -- 3-6 hours
		elseif durationDecode == 0.3 then 
			hoursToNormal = 6 + ZombRand(7); -- 6-12 hours
		elseif durationDecode == 0.4 then 
			hoursToNormal = 24 + ZombRand(25); -- 1-2 days
		elseif durationDecode == 0.5 then 
			hoursToNormal = 72 + ZombRand(49); -- 3-5 days
		elseif durationDecode == 0.6 then 
			hoursToNormal = 120 + ZombRand(49); -- 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);	--enable after debugging
--Events.EveryTenMinutes.Add(traumatizeTimer)	--disable after debugging
--Events.OnPlayerUpdate.Add(TestingFunction) 