-- parasite_ant.lua (5 stages)
-- S2: new chambers hollowed → acid micro-damage, workers never rest
-- S3: soldier ants patrol veins → stinging bites, acid particle trail
-- S4: acid burns tissue, colony is city → max HP reduced, heavy acid trail
-- S5: you are the colony → ants charmed, acid immunity granted
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 = 15 * 60
local SPAWN_INTERVAL_MAX = 45 * 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_ANT", "-1"))
	GlobalsSetValue("LN_PREV_HP_ANT", 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_ant")
		remove_parasite_overlay(player_id, "ant")
		force_release_compulsion(player_id, "parasite_ant")
		EntityKill(entity)
		ln_print_cure("parasite_ant", UI_MSG)
		return
	end
end

local stage, did_advance = check_parasite_stage_change(entity, MAX_STAGE, "LN_STAGE_ANN_ANT")
update_parasite_overlay(player_id, "ant", stage)

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

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

-- S2+: workers hollow chambers → periodic acid micro-damage
if stage >= 2 then
	if Random(1, stage >= 4 and 6 or 15) == 1 then
		local max_hp = tonumber(ComponentGetValue2(dmg_comp_h, "max_hp")) or 4
		EntityInflictDamage(player_id, max_hp * 0.01, "DAMAGE_CURSE", "ant acid", "NORMAL", 0, 0)
	end
end

-- S3+: soldier ants patrol → acid particle trail, stinging bite bursts
	-- Marching column particles (organized ant lines)
	local march_offset = (frame % 20) - 10
if stage >= 3 then
	-- Soldier ant sting bursts
	if Random(1, 10) == 1 then
		GameCreateParticle("acid", px + Random(-2,2), py + Random(-4,2), 3, Random(-3,3), Random(-3,3), false)
	end
end

-- S4+: acid carves tissue → max HP slowly reduced, heavy acid trail
if stage >= 4 then
	if frame % 300 == 0 and dmg_comp_h then
		local max_hp = tonumber(ComponentGetValue2(dmg_comp_h, "max_hp")) or 4
		local original_max = tonumber(GlobalsGetValue("LN_ANT_ORIG_MAXHP", "0"))
		if original_max == 0 then
			GlobalsSetValue("LN_ANT_ORIG_MAXHP", tostring(max_hp))
			original_max = max_hp
		end
		if max_hp > original_max * 0.5 then
			ComponentSetValue2(dmg_comp_h, "max_hp", max_hp - original_max * 0.005)
		end
	end
end

-- S5: you ARE the colony → ants charmed, grant acid immunity
if stage >= 5 then
	-- Acid immunity
	if not GameHasFlagRun("LN_ANT_ACID_IMMUNE") then
		GameAddFlagRun("LN_ANT_ACID_IMMUNE")
		-- Apply permanent acid protection
		local prot = EntityCreateNew("ln_ant_acid_prot")
		EntityAddComponent2(prot, "InheritTransformComponent", {})
		EntityAddComponent2(prot, "GameEffectComponent", { effect="PROTECTION_RADIOACTIVITY", frames=-1 })
		EntityAddChild(player_id, prot)
	end
	-- Charm nearby ants
	if frame % 90 == 0 then
		for _, e in ipairs(EntityGetInRadiusWithTag(px, py, 120, "enemy") or {}) do
			local fname = EntityGetFilename(e) or ""
			if fname:find("ant") then
				GetGameEffectLoadTo(e, "CHARM", true)
			end
		end
	end
end


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

-- COMPULSION
try_compulsion(player_id, "parasite_ant", stage, {
	min_stage = 3,
	duration_min = 120, duration_max = 240,
	cooldown_min = 40 * 60, cooldown_max = 85 * 60,
})

-- Inner monologue
local msg_next = tonumber(GlobalsGetValue("LN_MSG_ANT", "0"))
if msg_next == 0 then
	SetRandomSeed(frame, entity + 222)
	GlobalsSetValue("LN_MSG_ANT", tostring(frame + Random(30 * 60, 150 * 60)))
elseif frame >= msg_next then
	SetRandomSeed(frame, entity + 222)
	ln_print_monologue("parasite_ant", UI_MSG)
	GlobalsSetValue("LN_MSG_ANT", 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") == "data/entities/animals/ant.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_ant") })
	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("data/entities/animals/ant.xml")
if stage >= 3 and Random(1, stage >= 5 and 1 or 3) == 1 then
	create_pregnancy("data/entities/animals/ant.xml")
	ln_print_spawn("parasite_ant", "twin", UI_MSG)
else
	ln_print_spawn("parasite_ant", "single", UI_MSG)
end

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