-- parasite_fly.lua (5 stages)
-- S2: wings brushing insides → poison particle trail
-- S3: filled every hollow space, buzz when breathe → movement slow, constant buzz particles
-- S4: hive mind → enemy radar perk granted, poison cloud
-- S5: symbiosis → fly/flyswarm charmed, passive HP regen
dofile_once("mods/lewd_noita/files/status_effects/parasite_utils.lua")
dofile_once("mods/lewd_noita/files/status_effects/parasite_compulsion.lua")
dofile_once("mods/lewd_noita/files/text/ln_text.lua")


local MAX_STAGE = 5
local SPAWN_INTERVAL_MIN = 20 * 60
local SPAWN_INTERVAL_MAX = 60 * 60

local entity = GetUpdatedEntityID()
local vsc = get_parasite_timer_vsc(entity)
if vsc == nil then return end

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_FLY", "-1"))
	GlobalsSetValue("LN_PREV_HP_FLY", tostring(hp))
	if prev_hp >= 0 and max_hp > 0 and hp >= max_hp and prev_hp < max_hp then
		remove_parasite_emitter(player_id, "ln_em_fly")
		remove_parasite_overlay(player_id, "fly")
		force_release_compulsion(player_id, "parasite_fly")
		EntityKill(entity)
		ln_print_cure("parasite_fly", UI_MSG)
		return
	end
end

local stage, did_advance = check_parasite_stage_change(entity, MAX_STAGE, "LN_STAGE_ANN_FLY")
update_parasite_overlay(player_id, "fly", stage)

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

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

-- S2+: poison particle trail from wings brushing innards
	-- Buzzing particle swirl
	local t = frame * 0.1
	for i = 0, stage do
		local angle = t + i * 1.257
		local dist = 5 + stage * 2
		GameCreateParticle("spark_green", px + math.cos(angle) * dist, py + math.sin(angle) * dist, 1, math.cos(angle + 1.571) * 3, math.sin(angle + 1.571) * 3, false)
	end
if stage >= 2 then
	GameCreateParticle("poison", px + Random(-4, 4), py + Random(-2, 3), stage - 1, Random(-1,1), -Random(1,3), false)
end

-- S3+: movement slow (hollow spaces filled), constant buzz-like particle haze
if stage >= 3 then
	GameCreateParticle("spark_green", px + Random(-8, 8), py + Random(-6, 2), 2, Random(-3,3), Random(-3,3), false)
	-- Apply movement slow if not present
	local has_slow = false
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		if EntityGetName(child) == "ln_fly_slow" then has_slow = true break end
	end
	if not has_slow then
		local eff = EntityCreateNew("ln_fly_slow")
		EntityAddComponent2(eff, "InheritTransformComponent", {})
		EntityAddComponent2(eff, "GameEffectComponent", { effect="MOVEMENT_SLOWER", frames=-1 })
		EntityAddChild(player_id, eff)
	end
end

-- S4+: hive mind grants enemy radar (you sense through the swarm)
if stage >= 4 then
	if not GameHasFlagRun("LN_FLY_RADAR") then
		GameAddFlagRun("LN_FLY_RADAR")
		-- Grants enemy radar by mimicking the perk's flag
		GameAddFlagRun("PERK_PICKED_RADAR_ENEMY")
		ln_print_text_pair(LN_TEXT.parasite_fly.hive_radar, UI_MSG)
	end
end

-- S5: symbiosis - charm fly-family enemies, passive HP regen
if stage >= 5 then
	-- HP regen: heal 0.2% max HP per tick
	if frame % 120 == 0 and dmg_comp_h then
		local hp = tonumber(ComponentGetValue2(dmg_comp_h, "hp")) or 0
		local max_hp = tonumber(ComponentGetValue2(dmg_comp_h, "max_hp")) or 4
		if hp < max_hp then
			ComponentSetValue2(dmg_comp_h, "hp", math.min(hp + max_hp * 0.002, max_hp))
		end
	end
	-- Charm nearby flies/flyswarms
	if frame % 90 == 0 then
		for _, e in ipairs(EntityGetInRadiusWithTag(px, py, 120, "enemy") or {}) do
			local fname = EntityGetFilename(e) or ""
			if fname:find("fly") or fname:find("flyswarm") then
				GetGameEffectLoadTo(e, "CHARM", true)
			end
		end
	end
end


-- Particle emitter (stage 3+)
if stage >= 3 then
	attach_parasite_emitter(player_id, "ln_em_fly", "mods/lewd_noita/files/particles/emitter_toxic.xml")
end

-- COMPULSION
try_compulsion(player_id, "parasite_fly", stage, {
	min_stage = 3,
	duration_min = 90, duration_max = 200,
	cooldown_min = 40 * 60, cooldown_max = 80 * 60,
})

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

if EntityGetWithName("ln_sex_cooldown") ~= 0 then return end

local already_pregnant_this_source = false
for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
	if EntityGetName(child) == "ln_pregnancy" then
		local pvsc = EntityGetFirstComponent(child, "VariableStorageComponent")
		if pvsc ~= nil and ComponentGetValue2(pvsc, "value_string") == "mods/lewd_noita/files/enemies/flyswarm.xml" then
			already_pregnant_this_source = true
			break
		end
	end
end
if already_pregnant_this_source then return end

local dmg_comp = EntityGetFirstComponent(player_id, "DamageModelComponent")
if dmg_comp ~= nil and ComponentGetValue2(dmg_comp, "is_on_fire") == true then return end

local last_spawn = tonumber(ComponentGetValue2(vsc, "value_string")) or 0
local next_interval = tonumber(ComponentGetValue2(vsc, "value_int")) or 0
local mult = get_spawn_interval_mult(stage)

if next_interval == 0 then
	SetRandomSeed(frame, entity)
	next_interval = math.floor(Random(SPAWN_INTERVAL_MIN, SPAWN_INTERVAL_MAX) * mult)
	ComponentSetValue2(vsc, "value_int", next_interval)
	ComponentSetValue2(vsc, "value_string", tostring(frame))
	return
end
if frame - last_spawn < next_interval then return end

SetRandomSeed(frame, entity)
local function create_pregnancy(file)
	local pregnancy = EntityCreateNew("ln_pregnancy")
	EntityAddComponent2(pregnancy, "InheritTransformComponent", {})
	EntityAddComponent2(pregnancy, "GameEffectComponent", { effect="CUSTOM", custom_effect_id="EFFECT_PREGNANCY" })
	EntityAddComponent2(pregnancy, "UIIconComponent", { icon_sprite_file="mods/lewd_noita/files/status_effects/pregnant.png", name="Pregnant", description=ln_spawn_pregnancy_desc("parasite_fly") })
	EntityAddComponent2(pregnancy, "VariableStorageComponent", { value_string=file, value_int=GameGetFrameNum() })
	EntityAddComponent2(pregnancy, "LuaComponent", { script_source_file="mods/lewd_noita/files/sex_logic/start_birth.lua", execute_every_n_frame=Random(60, 90) * 60, remove_after_executed=true })
	EntityAddChild(player_id, pregnancy)
end

create_pregnancy("mods/lewd_noita/files/enemies/flyswarm.xml")
if stage >= 3 and Random(1, stage >= 5 and 1 or 3) == 1 then
	create_pregnancy("mods/lewd_noita/files/enemies/flyswarm.xml")
	ln_print_spawn("parasite_fly", "twin", UI_MSG)
else
	ln_print_spawn("parasite_fly", "single", UI_MSG)
end

ComponentSetValue2(vsc, "value_string", tostring(frame))
ComponentSetValue2(vsc, "value_int", math.floor(Random(SPAWN_INTERVAL_MIN, SPAWN_INTERVAL_MAX) * mult))
