-- @author DoomTero 2026

local ZomboRut = require("ZomboRut/ZomboRut")
require("ZomboRut/ZomboRutBodyCount")

local hoursToNormal = 0;
local durationDecode = 0;

local function initData()
	local player = getPlayer();
	local optionMax = (SandboxVars.ZomboRut.banditLimit)
	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 
		if optionMax == 2 then
			limitMax = 1;
		elseif optionMax == 3 then
			limitMax = 2;
		elseif optionMax == 4 then
			limitMax = 4;
		end
		if ZR_Trait.has(player, "swinger") then limitMax = (limitMax * 2); end
		if ZR_Trait.has(player, "promiscuous") then limitMax = -1; end
		if ZR_Trait.has(player, "Rapist") then limitMax = -1; end
		
		if optionMax == 1 then limitMax = -1; end
		
		player:getModData().slimitMax = limitMax; 
		Check(player);
	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 SandboxVars.ZomboRut.banditRewardNew == 1 then
		hoursToWait = 6;
	elseif SandboxVars.ZomboRut.banditRewardNew == 2 then
		hoursToWait = 12;
	elseif SandboxVars.ZomboRut.banditRewardNew == 3 then
		hoursToWait = 18;
	else
		hoursToWait = 24;
	end
		
	local function updateHistory(player)
		if #byCount > 0 then
			for i = #byCount, 1, -1 do
				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);
					end
				end
			end
		end
	end
	
	updateHistory(player);
	Check(player);
end

local function traumatizeTimer()
	local player = getPlayer();
	if player == nil then
		return;
	end	
	
	initData();
	limitTimer(player);
		
	if ZR_Trait.has(player, "Traumatized") then
		player:getModData().timedHandler.hoursSinceLastDefeat = player:getModData().timedHandler.hoursSinceLastDefeat + 1

		durationDecode = (SandboxVars.ZomboRut.tLength * 0.1);
		if durationDecode == 0.1 then
			hoursToNormal = 0; player:getModData().timedHandler.hoursSinceLastDefeat = 0;
		elseif durationDecode == 0.2 then 
			hoursToNormal = 3 + ZombRand(4);
		elseif durationDecode == 0.3 then 
			hoursToNormal = 6 + ZombRand(7);
		elseif durationDecode == 0.4 then 
			hoursToNormal = 24 + ZombRand(25);
		elseif durationDecode == 0.5 then 
			hoursToNormal = 72 + ZombRand(49);
		elseif durationDecode == 0.6 then 
			hoursToNormal = 120 + ZombRand(49);
		end
		
		if player:getModData().timedHandler.hoursSinceLastDefeat >= hoursToNormal then
			HaloTextHelper.addTextWithArrow(player, getText("Traumatized"), false, HaloTextHelper.getColorGreen());
			player:getCharacterTraits():remove(ZR_Trait.Traumatized)
			player:getModData().timedHandler.hoursSinceLastDefeat = 0;
			return
		end
	end
end

Events.OnGameStart.Add(initData)
Events.EveryHours.Add(traumatizeTimer)
