extends Control


@onready var settings_window            = %SettingsWindow
@onready var user_name_line_edit        = %UserNameLineEdit
@onready var network_name_line_edit     = %NetworkNameLineEdit
@onready var network_password_line_edit = %NetworkPasswordLineEdit
@onready var server_line_edit           = %ServerLineEdit
@onready var server_port_spin_box       = %ServerPortSpinBox
@onready var ip_line_edit               = %IPLineEdit
@onready var ip_check_button            = %IPCheckButton
@onready var mac_line_edit              = %MACLineEdit
@onready var mac_check_button           = %MACCheckButton
@onready var compression_check_button   = %CompressionCheckButton
@onready var auto_start_check_button    = %AutoStartCheckButton
@onready var save_settings_button       = %SaveSettingsButton


var uuid: String = ""


func load_settings():
	# Do we have a configuration yet?
	var config = ConfigFile.new()
	var err = config.load("user://network.cfg")
	if err != OK:
		# Assign defaults.
		user_name_line_edit.text                = "Change Me!"
		network_name_line_edit.text             = "Change Me!"
		network_password_line_edit.text         = "Change Me!"
		server_line_edit.text                   = "supernode.kangaroopunch.com"
		server_port_spin_box.value              = 7777
		ip_line_edit.text                       = "10.0.0.10"
		ip_check_button.button_pressed          = true
		ip_line_edit.editable                   = false
		mac_line_edit.text                      = "DE:AD:BE:EF:00:01"
		mac_check_button.button_pressed         = true
		mac_line_edit.editable                  = false
		compression_check_button.button_pressed = true
		auto_start_check_button.button_pressed  = false
		uuid                                    = Uuid.v4()
	else:
		# Load settings.
		user_name_line_edit.text                = config.get_value("settings", "userName",        "Change Me!")
		network_name_line_edit.text             = config.get_value("settings", "networkName",     "Change Me!")
		network_password_line_edit.text         = config.get_value("settings", "networkPassword", "Change Me!")
		server_line_edit.text                   = config.get_value("settings", "server",          "supernode.kangaroopunch.com")
		server_port_spin_box.value              = config.get_value("settings", "serverPort",      7777)
		ip_line_edit.text                       = config.get_value("settings", "ipAddress",       "10.0.0.10")
		ip_check_button.button_pressed          = config.get_value("settings", "ipAutomatic",     true)
		ip_line_edit.editable                   = !ip_check_button.button_pressed
		mac_line_edit.text                      = config.get_value("settings", "macAddress",      "DE:AD:BE:EF:00:01")
		mac_check_button.button_pressed         = config.get_value("settings", "macAutomatic",    true)
		mac_line_edit.editable                  = !mac_check_button.button_pressed
		compression_check_button.button_pressed = config.get_value("settings", "compression",     true)
		auto_start_check_button.button_pressed  = config.get_value("settings", "autoStart",       false)
		uuid                                    = config.get_value("settings", "identity",        Uuid.v4())


func save_settings():
	# Validate IP address, if needed.
	if !ip_check_button.button_pressed:
		var ipCheck = RegEx.create_from_string("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$")
		if ipCheck.search(ip_line_edit.text) == null:
			# Not an IP address.
			await Dialog.alert("Invalid IP Address", "The IP address entered is invalid.", settings_window)
			return

	# Validate MAC address, if needed.
	if !mac_check_button.button_pressed:
		var macCheck = RegEx.create_from_string("^(?:[[:xdigit:]]{2}([-:]))(?:[[:xdigit:]]{2}\\1){4}[[:xdigit:]]{2}$")
		if macCheck.search(mac_line_edit.text) == null:
			# Not a MAC address.
			await Dialog.alert("Invalid MAC Address", "The MAC address entered is invalid.", settings_window)
			return

	# Save it.
	var config = ConfigFile.new()
	config.set_value("settings", "userName",        user_name_line_edit.text)
	config.set_value("settings", "networkName",     network_name_line_edit.text)
	config.set_value("settings", "networkPassword", network_password_line_edit.text)
	config.set_value("settings", "server",          server_line_edit.text)
	config.set_value("settings", "serverPort",      server_port_spin_box.value)
	config.set_value("settings", "ipAddress",       ip_line_edit.text)
	config.set_value("settings", "ipAutomatic",     ip_check_button.button_pressed)
	config.set_value("settings", "macAddress",      mac_line_edit.text)
	config.set_value("settings", "macAutomatic",    mac_check_button.button_pressed)
	config.set_value("settings", "compression",     compression_check_button.button_pressed)
	config.set_value("settings", "autoStart",       auto_start_check_button.button_pressed)
	#config.set_value("settings", "position",        DisplayServer.window_get_position())
	config.set_value("settings", "identity",        uuid)
	config.save("user://network.cfg")


func _on_ip_check_button_toggled(_toggled_on):
	ip_line_edit.editable = !ip_check_button.button_pressed


func _on_mac_check_button_toggled(_toggled_on):
	mac_line_edit.editable = !mac_check_button.button_pressed


func _on_save_settings_button_pressed():
	save_settings()
	settings_window.visible = false


func _on_settings_window_close_requested():
	settings_window.visible = false


func _ready():
	load_settings()

