extends Node

var client = preload('res://Client.tscn')
var server = preload('res://Server.tscn')
var auth = preload('res://Auth.tscn')
signal login_confimed
var login_await = true


func _ready():
	for i in get_children():
		i.hide()
	$directconnect.show()

func _on_Button_button_down():
	$directconnect.hide()
	$loginscreen.show()
	get_parent().get_node("Loginetcode").ip = $directconnect/VBoxContainer/HBoxContainer/LineEdit2.get_text()
	get_parent().get_node("Loginetcode")._connect()
	if login_await: yield(self,'login_confimed')
	$loginscreen.hide()
	var client2 = client.instance()
	var ip = $directconnect/VBoxContainer/HBoxContainer/LineEdit2.get_text()
	var port = $directconnect/VBoxContainer/HBoxContainer/LineEdit.get_text()
	client2.ip = ip
	client2.port = int(port)
	get_parent().add_child(client2)
	pass # Replace with function body.

func _on_Button2_button_down():
	var server2 = server.instance()
	var auth2 = auth.instance()
	get_parent().add_child(server2)
	server2.db_validation = false
	get_parent().add_child(auth2)
	pass # Replace with function body.

func _on_CheckButton_toggled(button_pressed):
	login_await = button_pressed
	pass # Replace with function body.

func _on_register_button_down():
	$loginscreen.hide()
	$register.show()
	pass # Replace with function body.

func _on_login2_button_down():
	$loginscreen.show()
	$register.hide()
	pass # Replace with function body.


func _on_Register2_button_down():
	var username = $register/VBoxContainer/HBoxContainer1/LineEdit2.get_text()
	var password = $register/VBoxContainer/HBoxContainer2/LineEdit2.get_text()
	var password2 = $register/VBoxContainer/HBoxContainer3/LineEdit2.get_text()
	var email = $register/VBoxContainer/HBoxContainer4/LineEdit2.get_text()
	var salt = str(OS.get_system_time_msecs())
	$register/VBoxContainer/Label.text = ''
	print(salt)
	for i in 512: password = (password+salt).sha256_text()
	for i in 512: password2 = (password2+salt).sha256_text()
	print(password)
	get_parent().get_node("Loginetcode").Register(username, password, password2, email, salt)
	password = null

func _on_login_button_down():
	var username = $loginscreen/VBoxContainer/HBoxContainer/LineEdit2.get_text()
	get_parent().get_node("Loginetcode").Request_Salt(username)
	yield(get_parent().get_node("Loginetcode"),'salt')
	var salt = get_parent().get_node("Loginetcode").salt_buffer
	print(salt)
	var password = $loginscreen/VBoxContainer/HBoxContainer2/LineEdit2.get_text()
	if password == null or salt == null: return
	for i in 512: password = (password+salt).sha256_text()
	print(password)
	get_parent().get_node("Loginetcode").Login_Salt(username, password, salt)
	password = null
	yield(get_parent().get_node("Loginetcode"),'login_confirmed')
	$loginscreen.hide()
	var client2 = client.instance()
	var ip = $directconnect/VBoxContainer/HBoxContainer/LineEdit2.get_text()
	var port = $directconnect/VBoxContainer/HBoxContainer/LineEdit.get_text()
	client2.ip = ip
	client2.port = int(port)
	get_parent().add_child(client2)
	pass # Replace with function body.

