-- global cum leak table handles different cum leak entities for different kobolds
if cl == nil then
	cl = {}
end

local leak = GetUpdatedEntityID()

if cl[leak] == nil then
	cl[leak] = {}
end

-- run this every 30 frames instead of every frame to show cum increase after cum sound have been played instead of immediately
-- also to make cum leak more predictable
if GameGetFrameNum() % 30 == 0 then
	if GetMaterialInventoryMainMaterial(leak) ~= 0 then
		cl[leak].cum_total = 0
		cl[leak].cum_entries = {}

		for id, amount in ipairs(ComponentGetValue2(EntityGetFirstComponent(leak, "MaterialInventoryComponent"), "count_per_material_type")) do
			if amount > 0 then
				table.insert(cl[leak].cum_entries, {
					cum_name = CellFactory_GetName(id - 1),
					cum_amount = amount,
				})
				cl[leak].cum_total = cl[leak].cum_total + amount
			end
		end

		-- check if parent of cum leak entity (kobold) has parent entity (scene) so leaking cum doesnt happen during scene
		if EntityGetParent(EntityGetParent(leak)) == 0 then
			SetRandomSeed(GameGetFrameNum(), leak)

			if Random(1, 5) == 1 then
				local rnd = Random(1, #cl[leak].cum_entries)
				local cum_name = cl[leak].cum_entries[rnd].cum_name
				local cum_amount = cl[leak].cum_entries[rnd].cum_amount
				local entity_x, entity_y = EntityGetTransform(leak)
				local leak_amount = Random(5, 10)
				GameCreateParticle(cum_name, entity_x, entity_y, math.min(leak_amount, cum_amount), 1, -1, false)
				AddMaterialInventoryMaterial(leak, cum_name, cum_amount - math.min(leak_amount, cum_amount))
				cl[leak].cum_entries[rnd].cum_amount = cl[leak].cum_entries[rnd].cum_amount - math.min(leak_amount, cum_amount)
				cl[leak].cum_total = cl[leak].cum_total - math.min(leak_amount, cum_amount)

				-- Drain womb MIC in sync
				dofile_once("mods/lewd_noita/files/womb/womb_utils.lua")
				local parent = EntityGetParent(leak)
				if parent ~= nil and parent ~= 0 then
					local womb_e = getWombEntity(parent)
					if womb_e ~= nil then
						local womb_amt = getWombMaterialCount(cum_name, womb_e)
						if womb_amt > 0 then
							setWombMaterial(womb_e, cum_name, math.max(0, womb_amt - math.min(leak_amount, womb_amt)))
						end
					end
				end
			end
		end

		-- add slow movement if many cum
		if GameHasFlagRun("PERK_PICKED_LN_CUM_ADDICTION") == false then
			if cl[leak].cum_total > 1000 and EntityGetFirstComponent(leak, "GameEffectComponent") == nil then
				EntityAddComponent2(leak, "GameEffectComponent", {
					effect="MOVEMENT_SLOWER",
					frames=-1,
				})
			elseif cl[leak].cum_total < 1000 and EntityGetFirstComponent(leak, "GameEffectComponent") ~= nil then
				EntityRemoveComponent(leak, EntityGetFirstComponent(leak, "GameEffectComponent"))
			end
		end
	else
		EntityKill(leak)
		cl[leak] = nil
		return
	end
end

-- draw gui only for player
if EntityGetParent(leak) == EntityGetWithTag("player_unit")[1] and cl[leak].cum_total ~= nil and cl[leak].cum_total > 0 then
	local desc = "You're leaking cum."
	local icon = "mods/lewd_noita/files/status_effects/leak_cum.png"

	if cl[leak].cum_total > 1900 then
		desc = "You're so full of cum you can explode."
		icon = "mods/lewd_noita/files/status_effects/leak_cum_explode.png"
	elseif cl[leak].cum_total > 1500 then
		desc = "You're feeling sickly full. Any more cum will hurt you."
		icon = "mods/lewd_noita/files/status_effects/leak_cum_damage.png"
	elseif cl[leak].cum_total > 1000 then
		desc = "You're really full of cum. Movement is difficult."
	elseif cl[leak].cum_total > 900 then
		desc = "You're full of cum. Any more will make movement difficult."
	elseif cl[leak].cum_total > 250 then
		desc = "You can still fit more cum inside of you."
	elseif cl[leak].cum_total > 0 then
		desc = "You're almost empty."
	end

	-- GuiTooltip only shows if you hover over GuiText, not GuiImage, so we add invisible text (spaces) after percent sign
	-- one space after pecent sign corresponds to 3 pixels in gui coordinates
	-- four spaces make it GUI_X + 12
	local cum_percent = math.ceil((cl[leak].cum_total / 1000) * 100) .. "%    "
	local GUI_X = 600
	local GUI_Y = 65

	cum_leaking_gui = cum_leaking_gui or GuiCreate()
	GuiStartFrame(cum_leaking_gui)
	GuiImage(cum_leaking_gui, 3, GUI_X, GUI_Y, icon, 1, 1, 1, 0, 0, "")
	GuiText(cum_leaking_gui, GUI_X + 12 - GuiGetTextDimensions(cum_leaking_gui, cum_percent, 1, 2), GUI_Y + 2, cum_percent)
	GuiTooltip(cum_leaking_gui, desc, "")
end