extends Node2D

@onready var inputField = $CanvasLayer/LineEdit
@onready var outputField = $CanvasLayer/RichTextLabel
@onready var background = $CanvasLayer/ColorRect

var valid_words := {}

func _ready():
	var f := FileAccess.open("res://dictionary.txt", FileAccess.READ)
	if f:
		while not f.eof_reached():
			var word := f.get_line().strip_edges().to_upper()
			if word != "":
				valid_words[word] = true
		f.close()
	if valid_words.size() == 0:
		outputField.text = "No entries in dictionary!"

func check(entry: String) -> void:
	entry = entry.strip_edges().to_upper()

	if entry == "DISCO":
		background.discoMode = !background.discoMode

	if valid_words.has(entry):
		outputField.text = "Valid Word!"
	else:
		outputField.text = "Not a legal word..."

func _on_button_pressed() -> void:
	check(inputField.text)

func _on_line_edit_text_submitted(new_text: String) -> void:
	check(new_text)
