extends Label

#Hide all charafcters checked start
func _ready():
	text = "Authorization Granted: Unit " + String($"/root/SaveManager".get_run())
	show_text(2)
	visible_characters = 0


var timePassed = 0 #Current amount of time spend displaying te
const CHECK_LENGTH = 0.05 #Interval to check for updates
var timeNeeded = 0 #Time that it takes to display the text
var timer #Timer object
var fadeTimer #Timer object

func show_text(seconds):
	timeNeeded = seconds
	timePassed = 0
	if(!timer):
		timer = Timer.new()
		add_child(timer)
		timer.connect("timeout",Callable(self,"on_timeout"))
	timer.start(CHECK_LENGTH)
	
func fade_text(seconds):
	timeNeeded = seconds
	timePassed = 0
	if(!fadeTimer):
		fadeTimer = Timer.new()
		add_child(fadeTimer)
		fadeTimer.connect("timeout",Callable(self,"on_fade_timeout"))
	fadeTimer.start(CHECK_LENGTH)

func on_fade_timeout():
	timePassed += CHECK_LENGTH
	modulate.a = 1 - (timePassed/timeNeeded)
	if(timePassed >= timeNeeded):
		fadeTimer.queue_free()
	else:
		fadeTimer.start(CHECK_LENGTH)
	pass

func on_timeout():
	timePassed += CHECK_LENGTH
	visible_characters = (timePassed/timeNeeded) * get_total_character_count()
	if(timePassed >= timeNeeded):
		timer.queue_free()
		fade_text(2)
	else:
		timer.start(CHECK_LENGTH)
	pass