# ============================================================
# Gym 09 — Scene Switching Controller
# ============================================================
# This root node handles:
# 1. Starting the right timeline on scene load
#    (intro on first visit, congrats after minigame)
# 2. Catching the [signal arg=start_minigame] to change scenes
# ============================================================
extends Node2D

const MINIGAME_SCENE_PATH := "res://docs/gyms/gym09_scene_switching/gym09_minigame.tscn"

@export var intro_timeline: DialogicTimeline
@export var congrats_timeline: DialogicTimeline


func _ready() -> void:
	# Connect to signal events from timelines
	Dialogic.signal_event.connect(_on_dialogic_signal)

	# Check if we're returning from the minigame
	if Dialogic.VAR.Gym.minigame_done:
		print("[Gym09 Controller] Returning from minigame — starting congrats timeline.")
		Dialogic.VAR.Gym.minigame_done = false
		Dialogic.start(congrats_timeline)
	else:
		print("[Gym09 Controller] First visit — starting intro timeline.")
		Dialogic.start(intro_timeline)


func _on_dialogic_signal(argument: Variant) -> void:
	if argument is String and argument == "start_minigame":
		print("[Gym09 Controller] Signal 'start_minigame' received — switching to minigame scene.")
		# Tell the minigame where to return
		Dialogic.VAR.Gym.minigame_return = "standalone"
		await get_tree().create_timer(0.3).timeout
		get_tree().change_scene_to_file(MINIGAME_SCENE_PATH)
