extends Node

var client = preload('res://client.tscn')
var server = preload('res://server.tscn')
var auth = preload('res://auth.tscn')
signal login_confimed
signal choose_a_server(arg)
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()
	print(get_parent().get_node("Loginetcode").ip)
	get_parent().get_node("Loginetcode")._connect()
	if login_await: yield(self,'login_confimed')
	$loginscreen.hide()
	get_parent().get_node("BrowserListClient/PanelContainer").show()
	var client2 = client.instance()
	if login_await:
		var arg = yield(self,'choose_a_server')
		client2.ip = arg.IP
		client2.port = int(arg.Port)
	else:
		client2.ip = $directconnect/VBoxContainer/HBoxContainer/LineEdit2.get_text()
		client2.port = int($directconnect/VBoxContainer/HBoxContainer/LineEdit.get_text())

	get_parent().get_node("BrowserListClient/PanelContainer").hide()
	get_node('../Uibackground').hide()
	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():
	for x in get_children():
		x.hide()
	$register.show()
	pass # Replace with function body.

func _on_login2_button_down():

	for x in get_children():
		x.hide()
	$loginscreen.show()
	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

var ip =''
var port =''

func _on_login_button_down():
	var username = $loginscreen/VBoxContainer/HBoxContainer/LineEdit2.get_text()
	get_parent().get_node("Loginetcode").Request_Salt(username)
	var salt = yield(get_parent().get_node("Loginetcode"),'salt')
#	var salt = get_parent().get_node("Loginetcode").salt_buffer
	print('the salt is: ')
	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.

func choose_a_server(arg):
	print('choose server')
	print(arg)
	ip = arg.IP
	port = arg.Port
	print(ip)
	print(port)
	emit_signal("choose_a_server", arg)
	pass # Replace with function body.


func _on_changepassword_button_down():
	for x in get_children():
		x.hide()
	$changepassword.show()
	pass # Replace with function body.


func _on_chang_password_button_down():
	var username = $changepassword/VBoxContainer/HBoxContainer/LineEdit2.get_text()
	get_parent().get_node("Loginetcode").Request_Salt(username)
	var salt = yield(get_parent().get_node("Loginetcode"),'salt')
	var password_old = $changepassword/VBoxContainer/HBoxContainer2/LineEdit2.get_text()
	var password_new = $changepassword/VBoxContainer/HBoxContainer4/LineEdit2.get_text()
	var username_new = $changepassword/VBoxContainer/HBoxContainer5/LineEdit2.get_text()
	var email_new = $changepassword/VBoxContainer/HBoxContainer6/LineEdit2.get_text()
	
	print(salt)
	if salt == null: return
	for i in 512: password_old = (password_old+salt).sha256_text()
	for i in 512: password_new = (password_new+salt).sha256_text()
	get_parent().get_node("Loginetcode").chang_password(username, password_old, password_new, salt, username_new, email_new)
	password_old = null
	password_new = null
