-- parasite_hornygasflower.lua (4 stages)
-- S2: creatures turn toward you → enemies aggro from further, increased attraction range
-- S3: roots push through muscles → movement slow, vine/root particles, blood→sap
-- S4: pollen from every pore → charm nearby enemies periodically, massive gas/tentacle output
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_HORNY", "-1"))
	GlobalsSetValue("LN_PREV_HP_HORNY", 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_flower")
		remove_parasite_overlay(player_id, "hornygasflower")
		force_release_compulsion(player_id, "parasite_hornygasflower")
		EntityKill(entity)
		ln_print_cure("parasite_hornygasflower", UI_MSG)
		return
	end
end

local stage, did_advance = check_parasite_stage_change(entity, MAX_STAGE, "LN_STAGE_ANN_FLOWER")
update_parasite_overlay(player_id, "hornygasflower", stage)

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

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

-- S2+: creatures turn toward you → increase aggro range of nearby enemies
if stage >= 2 and frame % 90 == 0 then
	local range = stage >= 3 and 150 or 100
	for _, e in ipairs(EntityGetInRadiusWithTag(px, py, range, "enemy") or {}) do
		local ai = EntityGetFirstComponent(e, "AnimalAIComponent")
		if ai then
			-- Make enemies aware of player from further away
			local cur = tonumber(ComponentGetValue2(ai, "attack_dash_distance")) or 40
			if cur < 120 then
				ComponentSetValue2(ai, "attack_dash_distance", math.min(120, cur + 20))
			end
		end
	end
end

-- S3+: roots push through muscles → movement slow, blood→sap material, vine particles
if stage >= 3 then
	if dmg_comp_h then
		ComponentSetValue2(dmg_comp_h, "blood_material", "fungi") -- sap-like green
	end
	-- Root/vine particles
	-- Movement slow from root anchoring
	local has_slow = false
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		if EntityGetName(child) == "ln_flower_slow" then has_slow = true break end
	end
	if not has_slow then
		local eff = EntityCreateNew("ln_flower_slow")
		EntityAddComponent2(eff, "InheritTransformComponent", {})
		EntityAddComponent2(eff, "GameEffectComponent", { effect="MOVEMENT_SLOWER", frames=-1 })
		EntityAddChild(player_id, eff)
	end
end

-- S4: full pollination → charm all nearby enemies, massive gas and tentacle output
if stage >= 4 and frame % 60 == 0 then
	for _, e in ipairs(EntityGetInRadiusWithTag(px, py, 100, "enemy") or {}) do
		local gc = GetGameEffectLoadTo(e, "CHARM", true)
		if gc then ComponentSetValue2(gc, "frames", 600) end
	end
end

-- Gas cloud (scales with stage)
	-- Petal shower (pink particles drifting upward)
	if frame % 5 == 0 then
		GameCreateParticle("plasma_fading_pink", px + Random(-10 - stage*2, 10 + stage*2), py + Random(-3, 3), 1, Random(-2,2), -Random(3,8), false)
	end
local gas_count = 1 + stage
GameCreateParticle("ln_gas_horny", px + Random(-6, 6), py + Random(-4, 2), gas_count, 0, -20, false)

-- Tentacle growth (scales with stage)
local variant = Random(1, 5)
EntityLoad("mods/lewd_noita/files/materials/tentacle_growth_" .. variant .. ".xml", px + Random(-8, 8), py + 4)

-- S3+: additional tentacle growth
if stage >= 3 then
	local v2 = Random(1, 5)
	EntityLoad("mods/lewd_noita/files/materials/tentacle_growth_" .. v2 .. ".xml", px + Random(-12, 12), py + Random(-2, 6))
end

-- S4+: confusion aura on self (overwhelming pheromones)
if stage >= 4 and Random(1, 8) == 1 then
	local has_conf = false
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		local gc = EntityGetFirstComponent(child, "GameEffectComponent")
		if gc and ComponentGetValue2(gc, "effect") == "CONFUSION" then
			ComponentSetValue2(gc, "frames", 300)
			has_conf = true
			break
		end
	end
	if not has_conf then
		local effect = EntityLoad("mods/lewd_noita/files/status_effects/confusion.xml")
		if effect and effect ~= 0 then EntityAddChild(player_id, effect) end
	end
end


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

-- COMPULSION
try_compulsion(player_id, "parasite_hornygasflower", stage, {
	min_stage = 3,
	duration_min = 150, duration_max = 300,
	cooldown_min = 30 * 60, cooldown_max = 70 * 60,
})

-- Inner monologue
local msg_next = tonumber(GlobalsGetValue("LN_MSG_FLOWER", "0"))
if msg_next == 0 then
	SetRandomSeed(frame, entity + 444)
	GlobalsSetValue("LN_MSG_FLOWER", tostring(frame + Random(30 * 60, 150 * 60)))
elseif frame >= msg_next then
	SetRandomSeed(frame, entity + 444)
	ln_print_monologue("parasite_hornygasflower", UI_MSG)
	GlobalsSetValue("LN_MSG_FLOWER", tostring(frame + Random(30 * 60, 150 * 60)))
end
