class_name Intro
extends Node

const DIALOGS: Array[String] = [
	"Every few thousands years,",
	"an evil entity breaks free",
	"from the ancient sacred seal.",
	"...",
	"The evil is coming back",
	"and only you can save us",
	"Every one is counting on you",
	"...",
	"Good luck brave one",
]

const CHAR_DELAY: float = 0.04
const LINE_PAUSE: float = 2.0

var _full_text: String = ""

@onready var log_label: RichTextLabel = $CanvasLayer/Label
@onready var background: ColorRect = $CanvasLayer/Background

func _ready() -> void:
	log_label.text = ""
	_play_sequence()

func _play_sequence() -> void:
	for line in DIALOGS:
		await _type_line(line)
		await get_tree().create_timer(LINE_PAUSE).timeout
		_full_text += "\n"

	# Fade out
	var tween: Tween = create_tween()
	tween.tween_property(background, "modulate:a", 0.0, 1.2)
	await tween.finished
	if OS.get_name() == "Web":
		JavaScriptBridge.eval("document.querySelector('canvas').focus()")
	SceneManager.change_scene("res://scenes/levels/test_map.tscn")

func _type_line(line: String) -> void:
	for i in line.length():
		_full_text += line[i]
		log_label.text = _full_text
		await get_tree().create_timer(CHAR_DELAY).timeout
