-- parasite_fungus.lua (4 stages)
-- S2: tendrils through muscles, colors brighter → stronger trip, movement slower, blood→fungiblood
-- S3: mushrooms press from within, spores pour → massive fungi clouds, fungi entity spawning
-- S4: reality is whatever it decides → permanent warp, teleport-jitter, total sensory distortion
dofile_once("mods/lewd_noita/files/status_effects/parasite_utils.lua")
dofile_once("mods/lewd_noita/files/text/ln_text.lua")
dofile_once("mods/lewd_noita/files/status_effects/parasite_compulsion.lua")


local MAX_STAGE = 4
local entity = GetUpdatedEntityID()
local player_id = EntityGetParent(entity)
if player_id == nil or player_id == 0 then return end
local UI_MSG = "mods/lewd_noita/files/sex_logic/ui_msg.png"

local dmg_comp_h = EntityGetFirstComponent(player_id, "DamageModelComponent")
if dmg_comp_h ~= nil then
	local hp = tonumber(ComponentGetValue2(dmg_comp_h, "hp")) or 0
	local max_hp = tonumber(ComponentGetValue2(dmg_comp_h, "max_hp")) or 0
	local prev_hp = tonumber(GlobalsGetValue("LN_PREV_HP_FUNGUS", "-1"))
	GlobalsSetValue("LN_PREV_HP_FUNGUS", tostring(hp))
	if prev_hp >= 0 and max_hp > 0 and hp >= max_hp and prev_hp < max_hp then
		if dmg_comp_h then ComponentSetValue2(dmg_comp_h, "blood_material", "blood") end
		remove_parasite_emitter(player_id, "ln_em_fungus")
		remove_parasite_overlay(player_id, "fungus")
		force_release_compulsion(player_id, "parasite_fungus")
		EntityKill(entity)
		ln_print_cure("parasite_fungus", UI_MSG)
		return
	end
end

local stage, did_advance = check_parasite_stage_change(entity, MAX_STAGE, "LN_STAGE_ANN_FUNGUS")
update_parasite_overlay(player_id, "fungus", stage)

if did_advance then
	ln_print_stage("parasite_fungus", stage, UI_MSG)
end

local frame = GameGetFrameNum()
local px, py = EntityGetTransform(player_id)
SetRandomSeed(frame, entity)

-- Womb spore accumulation scales with stage
dofile_once("mods/lewd_noita/files/womb/womb_utils.lua")
local womb_e = getWombEntity(player_id)
if womb_e ~= nil then
	addWombMaterial(womb_e, "fungi", 2 + stage * 2)
end

-- S2+: tendrils through muscles → blood becomes fungal, movement slow, enhanced trip
if stage >= 2 and dmg_comp_h then
	ComponentSetValue2(dmg_comp_h, "blood_material", "fungi")
	-- Apply movement slow (tendrils binding muscles)
	local has_slow = false
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		if EntityGetName(child) == "ln_fungus_slow" then has_slow = true break end
	end
	if not has_slow then
		local eff = EntityCreateNew("ln_fungus_slow")
		EntityAddComponent2(eff, "InheritTransformComponent", {})
		EntityAddComponent2(eff, "GameEffectComponent", { effect="MOVEMENT_SLOWER", frames=-1 })
		EntityAddChild(player_id, eff)
	end
end

-- S3+: mushrooms press from within → massive spore clouds, spawn fungi entities
if stage >= 3 then
	-- Spore cloud explosion
	GameCreateParticle("fungi", px + Random(-12, 12), py + Random(-8, 4), 4 + stage, Random(-5,5), Random(-10,-2), false)
	-- Periodically spawn fungus creatures
	if frame % (300) == 0 then
		local f = EntityLoad("data/entities/animals/fungus.xml", px + Random(-15, 15), py - 5)
		if f and f ~= 0 then
			GetGameEffectLoadTo(f, "CHARM", true)
		end
	end
end

-- S4: reality distortion → random teleport jitter, permanent worm vision + warp
if stage >= 4 then
	-- Teleport jitter (reality is whatever it decides)
	if Random(1, 30) == 1 then
		EntitySetTransform(player_id, px + Random(-8, 8), py + Random(-8, 8))
	end
	-- Permanent worm vision (sensory overload)
	local has_worm = false
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		local gc = EntityGetFirstComponent(child, "GameEffectComponent")
		if gc ~= nil and ComponentGetValue2(gc, "effect") == "WORM_ATTRACTOR" then
			ComponentSetValue2(gc, "frames", 300)
			has_worm = true
			break
		end
	end
	if not has_worm then
		local effect = EntityLoad("mods/lewd_noita/files/status_effects/worm_vision.xml")
		if effect and effect ~= 0 then EntityAddChild(player_id, effect) end
	end
	-- Poison damage from internal overgrowth
	if Random(1, 10) == 1 then
		local max_hp = tonumber(ComponentGetValue2(dmg_comp_h, "max_hp")) or 4
		EntityInflictDamage(player_id, max_hp * 0.02, "DAMAGE_CURSE", "fungal overgrowth", "NORMAL", 0, 0)
	end
end

-- Trip effect (scales: longer duration at higher stages)
local trip_dur = 180 + stage * 60
local has_trip = false
for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
	local gc = EntityGetFirstComponent(child, "GameEffectComponent")
	if gc ~= nil and ComponentGetValue2(gc, "effect") == "TRIP" then
		ComponentSetValue2(gc, "frames", trip_dur)
		has_trip = true
		break
	end
end
if not has_trip then
	local effect = EntityLoad("mods/lewd_noita/files/status_effects/trip.xml")
	if effect and effect ~= 0 then EntityAddChild(player_id, effect) end
end

-- Fungus particle trail (scales with stage)
	-- Psychedelic spore ring (color-shifting orbit)
	local t = frame * 0.04
	local mats = {"fungi", "plasma_fading_pink", "poison"}
	for i = 0, 2 do
		local angle = t + i * 2.094
		local dist = 8 + stage * 3
		local mat = mats[(math.floor(frame / 30) + i) % 3 + 1]
		GameCreateParticle(mat, px + math.cos(angle) * dist, py + math.sin(angle) * dist, 1, 0, -2, false)
	end


-- Particle emitter (stage 2+)
if stage >= 2 then
	attach_parasite_emitter(player_id, "ln_em_fungus", "mods/lewd_noita/files/particles/emitter_spore.xml")
end

-- COMPULSION
try_compulsion(player_id, "parasite_fungus", stage, {
	min_stage = 3,
	duration_min = 120, duration_max = 250,
	cooldown_min = 45 * 60, cooldown_max = 90 * 60,
})

-- Inner monologue
local next_msg = tonumber(GlobalsGetValue("LN_MSG_FUNGUS", "0"))
if next_msg == 0 then
	SetRandomSeed(frame, entity + 777)
	GlobalsSetValue("LN_MSG_FUNGUS", tostring(frame + Random(30 * 60, 150 * 60)))
elseif frame >= next_msg then
	SetRandomSeed(frame, entity + 777)
	ln_print_monologue("parasite_fungus", UI_MSG)
	GlobalsSetValue("LN_MSG_FUNGUS", tostring(frame + Random(30 * 60, 150 * 60)))
end
