-- @author SolarEdge 2024 - 2025
BodyCount = {};

if getActivatedMods():contains("\\MoodleFramework") == true then
require "MF_ISMoodle";
MF.createMoodle("BodyCount");
end

--Temptation = {}
local isMF = getActivatedMods():contains("MoodleFramework");

local function NoMoodleManager(player) -- Alternative to update moodle.
	player:getModData().byCount = player:getModData().byCount or {};
	local byCount = player:getModData().byCount
	
	local slimitMax = player:getModData().slimitMax
	player:getModData().sayOnce = false;
	
	if slimitMax == -1 then return; end
	if slimitMax == nil then player:getModData().slimitMax = 1; slimitMax = player:getModData().slimitMax; end
	
	--- working on not repeating phrase when it is same person ---
	if #byCount == slimitMax and player:getModData().sayOnce then
		player:getModData().sayOnce = false;
		player:Say("That's enough for today!");
		--HaloTextHelper.addText(player, getText("Sexual Limit Maxed"), HaloTextHelper.getColorRed());
	elseif #byCount < slimitMax and player:getModData().sayOnce then
		player:getModData().sayOnce = false;
		player:Say("I could use some intimacy again.");
		--HaloTextHelper.addText(player, getText("Sexual Limit Count Decreased"), HaloTextHelper.getColorRed());
	elseif #byCount > slimitMax and player:getModData().sayOnce then
		player:getModData().sayOnce = false;
		player:Say("Why does it have to be like this?");
		--HaloTextHelper.addText(player, getText("Sexual Limit Count Exceeded"), HaloTextHelper.getColorRed());
	end
end

local function MoodleManager(player) -- Updates the moodle.
	player:getModData().byCount = player:getModData().byCount or {};
	local byCount = player:getModData().byCount
	
	local slimitMax = player:getModData().slimitMax
	local moodle = MF.getMoodle("BodyCount");
	
	if slimitMax == -1 then moodle:setValue(0.5); return; end
	if slimitMax == nil then player:getModData().slimitMax = 1; slimitMax = player:getModData().slimitMax; end
	
	if #byCount == slimitMax and moodle:getValue() ~= 0.4 then
		moodle:setValue(0.4)
		player:Say("That's enough for today!");
		--HaloTextHelper.addText(player, getText("Sexual Limit Maxed"), HaloTextHelper.getColorRed());
	elseif #byCount < slimitMax and moodle:getValue() ~= 0.5 then
		moodle:setValue(0.5)
		player:Say("I feel horny.");
		--HaloTextHelper.addText(player, getText("Sexual Limit Count Decreased"), HaloTextHelper.getColorRed());
	elseif #byCount > slimitMax and moodle:getValue() ~= 0.1 then
		moodle:setValue(0.1)
		player:Say("I wish things weren't like this... :(");
		--HaloTextHelper.addText(player, getText("Sexual Limit Count Exceeded"), HaloTextHelper.getColorRed());
	end

end

function Check(player)
	--- check will be called from ZomboWinDefeatTimed, to update this.
	if isMF then
		MoodleManager(player);
	else
		NoMoodleManager(player);
	end
end

function NewID(player, zombie)
	player:getModData().byCount = player:getModData().byCount or {};
	
	for _, e in ipairs(player:getModData().byCount) do
		if e == zombie then
			return false;
		end
	end
	player:getModData().sayOnce = true;
	table.insert(player:getModData().byCount, zombie)
	return true;
end

function RunID(player, zombie)	
	player:getModData().byCount = player:getModData().byCount or {};
	
	for _, e in ipairs(player:getModData().byCount) do
		if e == zombie then
			return false;
		end
	end
	return true;
end

function increaseCount(target, zombie)	----slimit no longer used, use #byCount.
	local slimitMax = target:getModData().slimitMax
	
	if slimitMax == nil then 
		--print("increaseCount: slimitMax is nil");
		target:getModData().slimitMax = 1; 
		slimitMax = target:getModData().slimitMax; 
	end
	
	target:getModData().byCount = target:getModData().byCount or {};
	local byCount = target:getModData().byCount
	
	--if slimitMax ~= -1 then --- promiscuous needs to have a counter too
	if #byCount <= target:getModData().slimitMax then
		NewID(target, zombie);
		--print(getText("sex count: ") .. #byCount .. " /" .. target:getModData().slimitMax)
		if slimitMax ~= -1 then Check(target); end
	end
	
	--- [BUG] There is a bug that causes same partners to be counted more than once.
	if #byCount > target:getModData().slimitMax then 
		NewID(target, zombie);
			
		if slimitMax ~= -1 then
			local bodyDamage = target:getBodyDamage()
			local unhappiness = bodyDamage:getUnhappynessLevel()
			
			--HaloTextHelper.addText(target, getText("Unhappiness increase: having too much sex"), --HaloTextHelper.getColorRed
			target:Say("This is just too much for me...");
			bodyDamage:setUnhappynessLevel(unhappiness + 50)
		end
	end
end