extends Control

@export_file("*.tscn") var scene_to_trigger: String

@onready var further_connexion: Timer = $FurtherConnexion
@onready var text_start: Timer = $TextStart
@onready var time_between_words: Timer = $TimeBetweenWords
@onready var time_before_show_buttons: Timer = $TimeBeforeShowButtons

@onready var message: Label = %Message
@onready var accept_button_1: Button = %AcceptButton1
@onready var accept_button_2: Button = %AcceptButton2

@onready var failed_audio: AudioStreamPlayer2D = $FailedAudio
@onready var aha_audio: AudioStreamPlayer2D = $AHAAudio

var growing_text: String = ""
var text_split: PackedStringArray
var text_fine_split: PackedStringArray
var text_fully_on_screen = false
var text_showing = false

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	text_split = message.text.split("---")
	text_fine_split = text_split[2].split(" ")
	message.text = ""
	
	accept_button_1.visible = false
	accept_button_2.visible = false

func start_mission() -> void:
	SceneLoader.load_scene(scene_to_trigger)

func _on_start_connexion_timeout() -> void:
	message.text = text_split[0]
	further_connexion.start()

func _on_further_connexion_timeout() -> void:
	message.text = text_split[1]
	text_start.start()

func _on_text_start_timeout() -> void:
	message.text = ""
	time_before_show_buttons.start()
	failed_audio.play()
	time_between_words.start()

func _on_time_between_words_timeout() -> void:
	if text_fine_split.is_empty():
		time_between_words.stop()
		time_before_show_buttons.start()
		return
		
	var current_text = message.text
	current_text += text_fine_split[0]
	current_text += " "
	message.text = current_text
	
	text_fine_split.remove_at(0)
	time_between_words.start()
	

func _on_time_before_show_buttons_timeout() -> void:
	accept_button_1.visible = true
	accept_button_2.visible = true
