extends Node

@export var gates: Array[NodePath]

# Private Methods
func _ready() -> void:
	for node_path in gates:
		_handle_spike_gate(node_path)


func _handle_spike_gate(node_path: NodePath) -> bool:
	var node := get_node(node_path)
	node_path = node.get_path()
	if not node.name.begins_with("Spike Gate"):
		return false
	
	# Connect Signal
	node.connect("Opened", func(): _on_spike_gate_opened(node))
	
	if not PersistenceManager.HasData(node_path):
		return true
	
	var is_open: bool = PersistenceManager.GetData(node_path)
	if not is_open:
		return true
	
	var collision_shape: CollisionShape2D = node.get_node("CollisionShape2D")
	var animated_sprite: AnimatedSprite2D = node.get_node("AnimatedSprite2D")
	
	node.set_deferred("is_open", true)
	collision_shape.set_deferred("disabled", true)
	animated_sprite.call_deferred("play", "open")
	
	return true


func _on_spike_gate_opened(node: Node) -> void:
	var path := node.get_path()
	PersistenceManager.UpdateData(path, true)
