extends Node

var net = NetworkedMultiplayerENet.new()
var api = MultiplayerAPI.new()
var port = 8081
var ip = '127.0.0.1'
# Called when the node enters the scene tree for the first time.
func _ready():
	print('connecting to port: ' + str(port))
	net.create_client(ip,port)
	
	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):
	custom_multiplayer.poll()

remote func ping():
	print('messaged received from server')

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

var playerphsyics = preload('res://playersprite.tscn')
var playerhud = preload('res://hud.tscn')

func _On_Connection_Succeeded():
	$Input.enabled = true
	print("Succesfully connected to game server")
	if not has_node("players"): return
	request_clients()
	yield(self, 'client_request')
	for x in current_clients:
		gen_objs(playerphsyics,x,$players)
		gen_objs(playerhud,x, $Hud/HBoxContainer)

func gen_objs(arg,x, node):
		var instance = arg.instance()
		instance.name = str(x)
		node.add_child(instance)

signal client_request
var current_clients = []

func request_clients():
	rpc_id(1,'send_clients')

remote func client_data(arg):
	current_clients = arg
	emit_signal('client_request')

# extra stuff

var ping = 0

func latency_test():
	rpc_id(1,'latency',OS.get_system_time_msecs())

remote func returnping(arg):
	ping = (OS.get_system_time_msecs() - arg)

func send_input(arg):
	rpc_id(1,'input_handler',arg)

remote func player_position(arg):
	if not has_node("players"): return
	for id in arg.keys():
		if $players.has_node(str(id)):
			var node = $players.get_node(str(id))
			if node.global_position.x != arg[id].x:
				node.scale.x = -1 if node.global_position.x > arg[id].x else 1
			node.global_position = arg[id]

remote func player_animation(arg):
	if not has_node("players"): return
	for id in arg.keys():
		if $players.has_node(str(id)):
			$players.get_node(str(id)).animation  = arg[id]

remote func enemies_position(arg):
	if not has_node('enemies'): return
	for i in $enemies.get_children():
		i.hide()
	for id in arg.keys().size():
		var node = $enemies.get_child(id)
		node.global_position = arg[arg.keys()[id]][0]
		node.animation = arg[arg.keys()[id]][1]
		node.show()

remote func attack_anim(arg, id):
	if not has_node("players"): return
#	for i in $players.get_children():
#		print(i.name)
	$players.get_node(str(id)).get_node('weapon/AnimationPlayer').play(arg)

remote func atr_update(arg):
#	var arg1 = 'player1'
#	player_atributes[arg1] = arg
	print(arg)
	if has_node('Hud/HBoxContainer/'+arg.keys()[0]):
		get_node('Hud/HBoxContainer/'+arg.keys()[0])._update(arg.values()[0])
	

#onready var player_atributes = {'player1':default_atributes.duplicate()}
#var default_atributes = {
#	'xp':0,
#	'hp':0,
#	'name':'player1',
#	'k':0,
#}
