extends Node

var net = NetworkedMultiplayerENet.new()
var api = MultiplayerAPI.new()
var port = 1909
var ip = '127.0.0.1'
var polling = false
# Called when the node enters the scene tree for the first time.
func _connect():
	polling = true
	print('Login connecting to port: ' + str(port))
	print('at ip: '+ip)
	net.create_client(ip,port)
#	net.set_dtls_enabled(true)
#	net.set_dtls_verify_enabled(false)
	self.set_custom_multiplayer(api)
	custom_multiplayer.set_root_node(self)
	custom_multiplayer.set_network_peer(net)
	
	net.connect("connection_failed", self, "_On_Connection_Failed")
	net.connect("connection_succeeded", self, "_On_Connection_Succeeded")
	pass # Replace with function body.

func _process(_delta):
	if polling: custom_multiplayer.poll()

func _On_Connection_Failed():
	print("Failed to connect to game server")

func _On_Connection_Succeeded():
	print("Connected to game server")

func Register(username, password, password2, email, salt):
	rpc_id(1,"RegisterPlayer", username, password, password2, email, salt, api.get_rpc_sender_id())

func Request_Salt(username):
	rpc_id(1,"salt_request", username)

func Login_Salt(username, password, salt):
	rpc_id(1,"password_confirmation", username, password, salt)

signal salt
signal login_confirmed
var salt_buffer = ''

remote func salt_arrived(salt):
	emit_signal("salt")
	salt_buffer = salt

remote func update_status(message):
	get_parent().get_node('LoginUI/register/VBoxContainer/Label').text = message
	get_parent().get_node('LoginUI/loginscreen/VBoxContainer/Label2').text = message

var token_buffer = ''
var username_server = ''
signal token_arrived

remote func _on_servertoken(token, username):
	print('token arrived: ')
	get_parent().get_node("LoginUI").emit_signal("login_confimed")
	print(token)
	token_buffer = token
	emit_signal('token_arrived')
	username_server = username
