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

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

local isMF = getActivatedMods():contains(MOODLE_FRAMEWORK_ID);

local function NoMoodleManager(player)
	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
	
	if #byCount == slimitMax and player:getModData().sayOnce then
		player:getModData().sayOnce = false;
		player:Say("That's enough for today!");
	elseif #byCount < slimitMax and player:getModData().sayOnce then
		player:getModData().sayOnce = false;
		player:Say("I could use some intimacy again.");
	elseif #byCount > slimitMax and player:getModData().sayOnce then
		player:getModData().sayOnce = false;
		player:Say("Why does it have to be like this?");
	end
end

local function MoodleManager(player)
	player:getModData().byCount = player:getModData().byCount or {};
	local byCount = player:getModData().byCount

	local slimitMax = player:getModData().slimitMax
	local moodle = MF.getMoodle("BodyCount");
	if moodle == nil then NoMoodleManager(player); return; end

	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!");
	elseif #byCount < slimitMax and moodle:getValue() ~= 0.5 then
		moodle:setValue(0.5)
		player:Say("I feel horny.");
	elseif #byCount > slimitMax and moodle:getValue() ~= 0.1 then
		moodle:setValue(0.1)
		player:Say("I wish things weren't like this... :(");
	end

end

function Check(player)
	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)
	local slimitMax = target:getModData().slimitMax
	
	if slimitMax == nil then 
		target:getModData().slimitMax = 1; 
		slimitMax = target:getModData().slimitMax; 
	end
	
	target:getModData().byCount = target:getModData().byCount or {};
	local byCount = target:getModData().byCount
	
	if #byCount <= target:getModData().slimitMax then
		NewID(target, zombie);
		if slimitMax ~= -1 then Check(target); end
	end
	
	if #byCount > target:getModData().slimitMax then 
		NewID(target, zombie);
			
		if slimitMax ~= -1 then
			local bodyDamage = target:getBodyDamage()
			local stats = target:getStats()
			local unhappiness = stats:get(CharacterStat.UNHAPPINESS)
			
			target:Say("This is just too much for me...");
			stats:set(CharacterStat.UNHAPPINESS, unhappiness + 50)
		end
	end
end