# ============================================================
# Gym 09 — Minigame Scene
# ============================================================
# A pure gameplay scene with NO Dialogic UI.
# Press the button 5 times, then return to the Dialogic scene.
# ============================================================
extends Control

@export var required_presses: int = 5

var _press_count: int = 0


func _ready() -> void:
	print("[Gym09 Minigame] Ready! Press the button ", required_presses, " times.")
	_update_label()


func _on_button_pressed() -> void:
	_press_count += 1
	print("[Gym09 Minigame] Press ", _press_count, " of ", required_presses)
	_update_label()

	if _press_count >= required_presses:
		_on_complete()


func _update_label() -> void:
	var remaining := required_presses - _press_count
	if remaining > 0:
		%CounterLabel.text = "Presses remaining: " + str(remaining)
		%Button.text = "Press Me! (" + str(_press_count) + "/" + str(required_presses) + ")"
	else:
		%CounterLabel.text = "Complete!"
		%Button.text = "Returning..."


func _on_complete() -> void:
	print("[Gym09 Minigame] Complete! Setting variable and switching back.")
	# Dialogic is an autoload, so this works even without a Dialogic layout
	Dialogic.VAR.Gym.minigame_done = true

	# Disable the button so player can't spam it
	%Button.disabled = true

	# Brief pause so the player sees "Complete!"
	await get_tree().create_timer(1.0).timeout

	# Determine where to return based on who started us
	var return_path := "res://docs/gyms/gym09_scene_switching/gym09_main.tscn"
	if Dialogic.VAR.Gym.minigame_return == "hub":
		return_path = "res://docs/gyms/cross-timeline-interactions/gym_controller.tscn"

	print("[Gym09 Minigame] Returning to: ", return_path)
	get_tree().change_scene_to_file(return_path)
