extends RichTextLabel
var dnum
var displaying = false
var currentlyDisplaying = ""
var secretCode = randi_range(100000, 999999)
var quizCorrect = false
var quizAnswered = false
var freeze = false
signal gabeTrigger
signal gabeExit
signal shounicTrigger
signal shounicExit
signal fadeToBlack
signal unfade

func _ready():
	dnum = 0
	displaySlow("A peaceful day in VALVe HQ")
	unfade.emit()

func displaySlow(txt):
	currentlyDisplaying = txt
	displaying = true
	var doneChars = ""
	for character in txt:
		if displaying:
			doneChars = doneChars + character
			await get_tree().create_timer(0.015).timeout
			if displaying:
				text = doneChars
				if not get_child(0).playing:
					get_child(0).play()
			else:
				break
		else:
			break
	currentlyDisplaying = ""
	displaying = false

func _on_continue_pressed():
	if not currentlyDisplaying and not freeze:
		match dnum:
			-1:
				var save = FileAccess.open("user://secrets.sds", FileAccess.WRITE)
				var json = JSON.new()
				var err = json.parse(Marshalls.base64_to_utf8(save.get_as_text()))
				if err == OK:
					var data = json.data
					data["quizSecretOne"] = secretCode
					save.store_string(Marshalls.utf8_to_base64(JSON.stringify(data)))
				else:
					save.store_string(Marshalls.utf8_to_base64(JSON.stringify({"quizSecretOne": secretCode})))
				text = "Psst, use code " + str(secretCode) + "!"
				dnum = -2
			0:
				displaySlow("Oh?")
			1:
				gabeTrigger.emit()
				displaySlow("Hi, I'm Gabe Newell!")
				get_parent().get_child(1).text = "Gabe Newell"
				get_parent().get_child(1).visible = true
			2:
				displaySlow("I'd like to invite you to a quiz at VALVe HQ!")
			3:
				displaySlow("It's a TF2 Trivia thing. You will be going against many famous TF2ubers, like Shounic... Shounic... and erm, shounic (we couldn't get permission from LazyPurple).")
			4:
				displaySlow("Will you accept?")
				get_node("/root/Control/Continue").visible = false
				get_node("/root/Control/YesNo").visible = true
			6:
				displaySlow("I'll see you then soon!")
				fadeToBlack.emit()
			7:
				get_parent().get_child(1).visible = false
				displaySlow("2 Hours later...")
				unfade.emit()
			8:
				get_parent().get_child(1).visible = true
				displaySlow("Hi!")
			9:
				displaySlow("The quiz is ready now! I'll go call shounic.")
				gabeExit.emit()
			10:
				get_parent().get_child(1).text = "Shounic"
				displaySlow("Hi.")
				shounicTrigger.emit()
			11:
				displaySlow("So apparently to celebrate them embracing treadmill work, we are doing a trivia quiz.")
			12:
				displaySlow("Neat.")
			13:
				displaySlow("What's your name?")
			14:
				displaySlow("Your name is \"Chat\"? That's a very strange name.")
			15:
				displaySlow("It also sounds oddly familiar...")
			16:
				displaySlow("Anyways, let's get on with the quiz.")
			17:
				shounicExit.emit()
				gabeTrigger.emit()
				get_node("/root/Control/Continue").visible = false
				get_parent().get_child(1).text = "Gabe Newell"
				displaySlow("Which one of these is not a real item?")
				get_node("/root/Control/QuizAnswers").visible = true
				for i in range(500):
					await get_tree().create_timer(0.01).timeout
					if quizAnswered:
						break
				get_node("/root/Control/QuizAnswers").visible = false
				get_node("/root/Control/Continue").visible = true
				get_parent().get_child(1).text = "Shounic"
				if !quizCorrect:
					dnum = 21
				get_node("/root/Control/Continue").emit_signal("pressed")
				gabeExit.emit()
				shounicTrigger.emit()
			18:
				displaySlow("That's... right (it got removed).")
			19:
				get_parent().get_child(1).text = "Gabe Newell"
				displaySlow("Welp, that's all we have time for. See you in another 2-4 years for the next quiz!")
			20:
				get_parent().get_child(1).text = "Shounic"
				displaySlow("That was underwealming...")
				var transparency = 0
				freeze = true
				for i in range(100):
					transparency += 0.01
					await get_tree().create_timer(0.01).timeout
					get_node("/root/Control/FadeToBlack").color = Color(0, 0, 0, transparency)
				await get_tree().create_timer(0.01).timeout
				get_tree().change_scene_to_file("res://scenes/house/house.tscn")
			21:
				displaySlow("It's the community jarate. It used to exist but got removed.")
				dnum = 20
			22:
				freeze = true
				displaySlow("Congratulations. You are a loser.")
				var transparency = 0
				for i in range(100):
					transparency += 0.01
					await get_tree().create_timer(0.01).timeout
					get_node("/root/Control/FadeToBlack").color = Color(0, 0, 0, transparency)
				await get_tree().create_timer(0.01).timeout
				get_tree().change_scene_to_file("res://scenes/house/house.tscn")
		dnum += 1
	elif not freeze:
		displaying = false
		text = currentlyDisplaying
		currentlyDisplaying = ""


func _on_no_pressed():
	if not currentlyDisplaying:
		match dnum:
			5:
				get_parent().get_child(1).visible = false
				get_node("/root/Control/YesNo").visible = false
				get_node("/root/Control/Continue").visible = true
				displaySlow("You get up slowly, and leave the building")
				DirAccess.remove_absolute("user:///chapter.sds")
				var save = FileAccess.open("user://endings.sds", FileAccess.READ_WRITE)
				var json = JSON.new()
				var err = json.parse(Marshalls.base64_to_utf8(save.get_as_text()))
				if err == OK:
					var data = json.data
					data["end_bldleave"] = "1"
					save.store_string(Marshalls.utf8_to_base64(JSON.stringify(data)))
				else:
					save.store_string(Marshalls.utf8_to_base64(JSON.stringify({"end_bldleave": "1"})))
				var transparency = 0
				dnum = -2
				for i in range(100):
					transparency += 0.01
					await get_tree().create_timer(0.01).timeout
					get_node("/root/Control/FadeToBlack").color = Color(0, 0, 0, transparency)
				await get_tree().create_timer(0.01).timeout
				get_tree().change_scene_to_file("res://scenes/menu/menu.tscn")
		dnum += 1
	else:
		displaying = false
		text = currentlyDisplaying
		currentlyDisplaying = ""

func _on_yes_pressed():
	if not currentlyDisplaying:
		match dnum:
			5:
				get_node("/root/Control/YesNo").visible = false
				get_node("/root/Control/Continue").visible = true
				displaySlow("Great!")
		dnum += 1
	else:
		displaying = false
		text = currentlyDisplaying
		currentlyDisplaying = ""


func _on_three_years_pressed():
	if not currentlyDisplaying:
		quizAnswered = true
	else:
		displaying = false
		text = currentlyDisplaying
		currentlyDisplaying = ""


func _on_five_years_pressed():
	if not currentlyDisplaying:
		quizAnswered = true
	else:
		displaying = false
		text = currentlyDisplaying
		currentlyDisplaying = ""

func _on_nine_years_pressed():
	if not currentlyDisplaying:
		quizCorrect = true
		quizAnswered = true
	else:
		displaying = false
		text = currentlyDisplaying
		currentlyDisplaying = ""
