-- parasite_firebug.lua (5 stages)
-- S2: smoke leaks from body
-- S3: smoke aura around player
-- S4: furnace body - ignite nearby enemies, thicker smoke
-- S5: nest mother - firebugs/maggots charmed, body exists to birth
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 GOLD_CHANCE = 10

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_FIREBUG", "-1"))
	GlobalsSetValue("LN_PREV_HP_FIREBUG", tostring(hp))
	if prev_hp >= 0 and max_hp > 0 and hp >= max_hp and prev_hp < max_hp then
		-- Undo blood change on cure
		if dmg_comp_h then ComponentSetValue2(dmg_comp_h, "blood_material", "blood") end
		remove_parasite_emitter(player_id, "ln_em_firebug")
		remove_parasite_overlay(player_id, "firebug")
		force_release_compulsion(player_id, "parasite_firebug")
		EntityKill(entity)
		ln_print_cure("parasite_firebug", UI_MSG)
		return
	end
end

local stage, did_advance = check_parasite_stage_change(entity, MAX_STAGE, "LN_STAGE_ANN_FIREBUG")
update_parasite_overlay(player_id, "firebug", stage)

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

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

-- S2+: faint smoke leaks from the body
if stage >= 2 and frame % 4 == 0 then
	GameCreateParticle("smoke", px + Random(-5, 5), py + Random(-3, 3), 1, Random(-2, 2), -Random(1, 3), false)
end

-- S3+: orbiting smoke aura
if stage >= 3 then
	local t = frame * 0.06
	for i = 0, 3 do
		local angle = t + i * 1.571
		local dist = 10 + stage * 2
		GameCreateParticle("smoke", px + math.cos(angle) * dist, py + math.sin(angle) * dist, 1, 0, -2, false)
	end
end

-- S4+: thicker smoke breath; ignite nearby enemies
if stage >= 4 then
	if frame % 3 == 0 then
		GameCreateParticle("smoke", px + Random(-3, 3), py + Random(-6, -2), 2, Random(-3, 3), -Random(5, 10), false)
	end
	-- Ignite nearby enemies
	if frame % 60 == 0 then
		for _, e in ipairs(EntityGetInRadiusWithTag(px, py, stage >= 5 and 80 or 50, "enemy") or {}) do
			local edc = EntityGetFirstComponent(e, "DamageModelComponent")
			if edc then
				ComponentSetValue2(edc, "is_on_fire", true)
			end
		end
	end
end

-- S5: nest mother - charm all firebug-family enemies nearby
if stage >= 5 and frame % 90 == 0 then
	for _, e in ipairs(EntityGetInRadiusWithTag(px, py, 120, "enemy") or {}) do
		local fname = EntityGetFilename(e) or ""
		if fname:find("firebug") or fname:find("maggot_2") or fname:find("maggot_3") then
			GetGameEffectLoadTo(e, "CHARM", true)
		end
	end
end


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

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

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

-- S4+: periodic heat damage
if stage >= 4 then
	SetRandomSeed(frame, entity + 999)
	if Random(1, stage >= 5 and 8 or 15) == 1 then
		local max_hp = tonumber(ComponentGetValue2(dmg_comp_h, "max_hp")) or 4
		EntityInflictDamage(player_id, max_hp * 0.02, "DAMAGE_FIRE", "parasite heat", "NORMAL", 0, 0)
	end
end

if EntityGetWithName("ln_sex_cooldown") ~= 0 then return end
if EntityGetWithTag("birth_scene") ~= nil and #EntityGetWithTag("birth_scene") > 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/maggot_2.xml" or ComponentGetValue2(pvsc, "value_string") == "mods/lewd_noita/files/enemies/maggot_3.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 spawn_file
if Random(1, GOLD_CHANCE) == 1 then
	spawn_file = "mods/lewd_noita/files/enemies/maggot_3.xml"
else
	spawn_file = "mods/lewd_noita/files/enemies/maggot_2.xml"
end

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_firebug") })
	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(spawn_file)
if stage >= 3 and Random(1, stage >= 5 and 1 or 3) == 1 then
	create_pregnancy(spawn_file)
	ln_print_spawn("parasite_firebug", "twin", UI_MSG)
else
	ln_print_spawn("parasite_firebug", "single", UI_MSG)
end

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