'''
特效播放管理器
可以通过预设的特效表和对应 Node 的 Signal
播放对应的特效
'''
extends Node
class_name ReedVFXManager

@export var bindings: Array[VFXSignalBinding] = []
@export var enable_debug: bool = true


func _ready() -> void:
	_bind_all()


func _bind_all() -> void:
	for binding in bindings:
		if not has_node(binding.target_node):
			push_warning("VFXManager: target node not found: %s" % binding.target_node)
			continue

		var target := get_node(binding.target_node)
		if not target.has_signal(binding.signal_name):
			push_warning("VFXManager: signal not found: %s" % binding.signal_name)
			continue

		# 用 lambda 包一层
		var cb := func(ctx):
			_on_vfx_signal(binding, ctx)

		target.connect(binding.signal_name, cb)

func _on_vfx_signal(binding: VFXSignalBinding, ctx: Dictionary) -> void:
	if enable_debug and binding.debug_print:
		print_debug(binding.get_debug_message())

	match binding.spawn_space:
		VFXSignalBinding.SpawnSpace.TARGET_NODE:
			_spawn_at_target(binding)

		VFXSignalBinding.SpawnSpace.WORLD_POSITION:
			if ctx.has("world_pos"):
				_spawn_at_position(binding, ctx["world_pos"])


func _spawn_at_target(binding: VFXSignalBinding) -> void:
	var target := get_node(binding.target_node)
	if not binding.vfx_scene:
		return
	
	#var vfx := binding.vfx_scene.instantiate() as Node2D
	#add_child(vfx)
#
	#vfx.global_position = target.global_position + binding.offset


func _spawn_at_position(binding: VFXSignalBinding, pos: Vector2) -> void:
	ReedVFX.spawn_vfx_at_pos(binding.vfx_scene,pos + binding.offset)
