-- wolf_bonding.lua
-- 5-stage wolf submission tracker. At stage 5, permanent leash.

dofile_once("mods/lewd_noita/files/text/ln_text.lua")

local UI_MSG = "mods/lewd_noita/files/sex_logic/ui_msg.png"

function wolf_bond_increment(player_id, wolf_entity_id)
	local count = tonumber(GlobalsGetValue("LN_WOLF_BOND_COUNT", "0")) or 0
	if count >= 5 then return end

	count = count + 1
	GlobalsSetValue("LN_WOLF_BOND_COUNT", tostring(count))

	if count < 5 then
		ln_print_wolf_bond(count, UI_MSG)
	end

	if count >= 5 then
		activate_wolf_leash(player_id, wolf_entity_id)
	end
end

function activate_wolf_leash(player_id, wolf_entity_id)
	GlobalsSetValue("LN_WOLF_BOND_ACTIVE", "1")
	GlobalsSetValue("LN_WOLF_BOND_OWNER", tostring(wolf_entity_id))

	ln_print_wolf_leash(UI_MSG)

	-- HP boost: ~7x vanilla (0.6) so it survives incidental crossfire
	-- but other enemies can still eventually kill it to free the player.
	local wolf_dmg = EntityGetFirstComponent(wolf_entity_id, "DamageModelComponent")
	if wolf_dmg then
		ComponentSetValue2(wolf_dmg, "hp", 4)
		ComponentSetValue2(wolf_dmg, "max_hp", 4)
	end

	-- Set wolf to player herd — player's own projectiles/melee won't
	-- damage it (same-herd immunity), but other enemies still can.
	-- start_sex.lua doesn't check herd_id so scenes still trigger.
	local genome = EntityGetFirstComponent(wolf_entity_id, "GenomeDataComponent")
	if genome then
		ComponentSetValue2(genome, "herd_id", "player")
	end

	-- Attach passivity state machine (controls detection range each frame)
	local passivity = EntityCreateNew("ln_wolf_passivity")
	EntityAddComponent2(passivity, "LuaComponent", {
		script_source_file = "mods/lewd_noita/files/status_effects/wolf_passivity.lua",
		execute_every_n_frame = 1,
	})
	EntityAddChild(wolf_entity_id, passivity)

	-- Spawn per-frame leash driver (tether + chain visual only, no control lock)
	local l = EntityCreateNew("ln_wolf_leash")
	EntityAddComponent2(l, "LuaComponent", {
		script_source_file = "mods/lewd_noita/files/status_effects/wolf_leash_drive.lua",
		execute_every_n_frame = 1,
	})
	EntityAddChild(player_id, l)

	-- Status icon
	local icon = EntityCreateNew("ln_wolf_bond_icon")
	EntityAddComponent2(icon, "InheritTransformComponent", {})
	EntityAddComponent2(icon, "UIIconComponent", {
		icon_sprite_file = "mods/lewd_noita/files/status_effects/binded.png",
		name = "Bound Bitch",
		description = ln_wolf_icon_desc(),
	})
	EntityAddChild(player_id, icon)
end

function release_wolf_leash(player_id)
	for _, child in ipairs(EntityGetAllChildren(player_id) or {}) do
		local n = EntityGetName(child)
		if n == "ln_wolf_leash" or n == "ln_wolf_bond_icon" then
			EntityKill(child)
		end
	end

	-- Clean up the verlet rope entity
	local rope_id = tonumber(GlobalsGetValue("LN_WOLF_ROPE_ID", "0")) or 0
	if rope_id ~= 0 and EntityGetIsAlive(rope_id) then
		EntityKill(rope_id)
	end
	GlobalsSetValue("LN_WOLF_ROPE_ID", "0")

	-- Clean up passivity state globals
	GlobalsSetValue("LN_WOLF_STATE", "wander")
	GlobalsSetValue("LN_WOLF_STATE_END", "0")
	GlobalsSetValue("LN_WOLF_WANDER_DIR", "1")
	GlobalsSetValue("LN_WOLF_WANDER_TURN", "0")
	GlobalsSetValue("LN_WOLF_WAS_PREDATOR", "1")

	GlobalsSetValue("LN_WOLF_BOND_ACTIVE", "0")
	GlobalsSetValue("LN_WOLF_BOND_OWNER", "0")
	GlobalsSetValue("LN_WOLF_BOND_COUNT", "4")

	ln_print_wolf_release(UI_MSG)
end
