tool
class_name NewGamePanel
extends PopupPanel

signal start_triggered(config)

const pref_path = "user://preferences.cfg"

var preferences = ConfigFile.new()

func _init():
	var err = preferences.load(pref_path)

func _ready():
	pass

func _on_Cancel_pressed():
	self.hide()

func _on_Start_pressed():
	if $Panel/HBoxContainer/Easy.pressed:
		preferences.set_value("game", "difficulty", "easy")
	if $Panel/HBoxContainer/Normal.pressed:
		preferences.set_value("game", "difficulty", "normal")
	if $Panel/HBoxContainer/Hard.pressed:
		preferences.set_value("game", "difficulty", "hard")

	preferences.save(pref_path)

	emit_signal("start_triggered", preferences)
	self.hide()

func _on_NewGamePanel_about_to_show():
	$Panel/HBoxContainer/Easy.pressed = false
	$Panel/HBoxContainer/Normal.pressed = false
	$Panel/HBoxContainer/Hard.pressed = false

	var difficulty = preferences.get_value("game", "difficulty", "normal")
	match difficulty:
		"easy":
			$Panel/HBoxContainer/Easy.pressed = true
		"normal":
			$Panel/HBoxContainer/Normal.pressed = true
		"hard":
			$Panel/HBoxContainer/Hard.pressed = true
		_:
			assert("Invalid value")
			$Panel/HBoxContainer/Normal.pressed = true

	$Panel/Start.grab_focus()
