extends Node2D

# word stuff
var word_set
var word_set_file_path
var word_set_file
var word_set_content
var word_set_array

# init random
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var comets = []
var comet_speed = 250
var active

# display
var viewport_size
var scale_factor_x
var scale_factor_y

# numbers
var delay = 1.5
var spawn_timer = 0
var max_character_count = 0

# objects
var comet_source = preload("res://scenes/objects/game/comet.tscn")
var fade
var pause_menu

# Runs on start
func _ready() -> void:
	viewport_size = get_viewport_rect().size
	#scale_factor_x = viewport_size.x/1280
	#scale_factor_y = viewport_size.y/720
	
	#print($Tux.position)
	#$Tux.position = Vector2($Tux.position.x * scale_factor_x, $Tux.position.y * scale_factor_y)
	#print($Tux.position)
	
	active = true
	
	pause_menu = self.get_node("OnTop").get_node("Pause")
	fade = self.get_node("OnTop").get_node("Fade")
	fade.visible = true
	var tween := create_tween()
	tween.parallel().tween_property(fade, "modulate:a", 0, 1).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_OUT)
	
	# Load Corresponding Text File
	word_set_file_path = "res://gameplay/word_sets/" + word_set + ".txt"
	word_set_file = FileAccess.open(word_set_file_path, FileAccess.READ)
	
	# Gets file contents as text
	word_set_content = word_set_file.get_as_text()
	
	# Splits text file contents by line and removes the first entry (its just the name)
	word_set_array = word_set_content.split("\n", true)
	word_set_array.remove_at(0)
	
	if word_set_array[len(word_set_array)-1] == "":
		word_set_array.remove_at(len(word_set_array)-1)
		
	for word in word_set_array:
		if len(word) > max_character_count:
			print("new longest word: " + str(word) + " " + str(len(word)))
			max_character_count = len(word)
	
	#print(word_set_array)

# Runs every frame
func _process(delta: float) -> void:
	if active:
		spawn_timer += delta
		
		if spawn_timer >= delay:
			spawn_timer = 0
			spawn_word()

func spawn_word():
	var scan_commet = comet_source.instantiate()
	var comet_width = scan_commet.get_node("Sprite").sprite_frames.get_frame_texture("comet",0).get_width()
	var new_word = word_set_array[randi_range(0,len(word_set_array)-1)]
	var dist = (viewport_size.x - (2*comet_width))/(max_character_count-1)
	var offset = comet_width
	scan_commet.queue_free()
	
	for char in new_word:
		if char != " ":
			var new_commet = comet_source.instantiate()
			new_commet.position = Vector2(offset,-128)
			new_commet.get_node("Sprite").get_node("Label").text = char
			new_commet.speed = comet_speed
			add_child(new_commet)
			comets.append(new_commet)
		offset += dist
		
	delay = randf_range(0.75,2)

func _input(event):
	if event is InputEventKey and event.pressed:
		if event.as_text() in alphabet:
			if active:
				if len(comets) > 0:
					var comet = comets[0]
					if event.as_text() == comet.get_node("Sprite").get_node("Label").text:
						comet.active = false
						var laser = Line2D.new()
						laser.default_color = Color.RED
						laser.points = PackedVector2Array([$Tux.position,comet.position])
						add_child(laser)
						comets.erase(comet)
						comet.get_node("Sprite").play("cometbreak")
						$CometBreakSound.play()
						await comet.get_node("Sprite").animation_finished
						laser.queue_free()
						comet.queue_free()
		elif event.keycode == KEY_ESCAPE:
			if pause_menu.visible:
				pause_menu.resume()
				$BackgroundMusic.play()
			else:
				$Back.play()
				active = false
				pause_menu.visible = true
				$BackgroundMusic.stop()
