-- parasite_spider.lua (5 stages)
-- S2: more silk anchors → web trail, movement slow
-- S3: threads connecting everything → web everywhere, faster spawns
-- S4: spiderlings test their legs → spawned spiders are charmed briefly
-- S5: brood mother matured → spiders permanently charmed, never stop hatching
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_SPIDER", "-1"))
	GlobalsSetValue("LN_PREV_HP_SPIDER", 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_spider")
		remove_parasite_overlay(player_id, "spider")
		force_release_compulsion(player_id, "parasite_spider")
		EntityKill(entity)
		ln_print_cure("parasite_spider", UI_MSG)
		return
	end
end

local stage, did_advance = check_parasite_stage_change(entity, MAX_STAGE, "LN_STAGE_ANN_SPIDER")
update_parasite_overlay(player_id, "spider", stage)

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

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

-- S2+: silk anchor web trail
	-- Radial silk strands
	if frame % 4 == 0 then
		local angle = (frame * 0.08) + (frame % (stage + 2)) * 0.785
		local dist = 4 + (frame % 10)
	end
if stage >= 2 then
end

-- S3+: threads everywhere - wider web spread, movement penalty
if stage >= 3 then
	-- Reduce jump velocity (silk binds legs)
	local plat = EntityGetFirstComponent(player_id, "CharacterPlatformingComponent")
	if plat then
		local cur_jump = tonumber(ComponentGetValue2(plat, "jump_velocity_y")) or 300
		if cur_jump > 200 then
			ComponentSetValue2(plat, "jump_velocity_y", math.max(200, cur_jump - 0.5))
		end
	end
end

-- S4+: spawned spiders nearby become charmed (spiderlings recognize mother)
if stage >= 4 and frame % 60 == 0 then
	for _, e in ipairs(EntityGetInRadiusWithTag(px, py, 80, "enemy") or {}) do
		local fname = EntityGetFilename(e) or ""
		if fname:find("spider") or fname:find("maggot_4") then
			local gc = GetGameEffectLoadTo(e, "CHARM", true)
			if gc and stage < 5 then
				-- S4: temporary charm (spiderlings testing)
				ComponentSetValue2(gc, "frames", 600)
			end
			-- S5: permanent charm (true is already infinite)
		end
	end
end


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

-- COMPULSION
try_compulsion(player_id, "parasite_spider", stage, {
	min_stage = 4,
	duration_min = 150, duration_max = 270,
	cooldown_min = 50 * 60, cooldown_max = 100 * 60,
})

-- Inner monologue
local msg_next = tonumber(GlobalsGetValue("LN_MSG_SPIDER", "0"))
if msg_next == 0 then
	SetRandomSeed(frame, entity + 555)
	GlobalsSetValue("LN_MSG_SPIDER", tostring(frame + Random(30 * 60, 150 * 60)))
elseif frame >= msg_next then
	SetRandomSeed(frame, entity + 555)
	ln_print_monologue("parasite_spider", UI_MSG)
	GlobalsSetValue("LN_MSG_SPIDER", 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/maggot_4.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 = "mods/lewd_noita/files/enemies/maggot_4.xml"

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_spider") })
	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_spider", "twin", UI_MSG)
else
	ln_print_spawn("parasite_spider", "single", UI_MSG)
end

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