extends Control

var host_port = 2675
var host_playermax = 2
var ip = "127.0.0.1"
var port = 2670

# Called when the node enters the scene tree for the first time.
func _ready():
	#var myip = IP.get_local_addresses()
	#var interfaces = IP.get_local_interfaces ()
	#$HostPanel/label_myip.text = "IP "+myip[0]+". "+interfaces[0]
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass


func _on_singleplayer_button_up():
	var _letsgo = get_tree().change_scene("res://world_scenes/World.tscn")


func _on_JoinGameBtn_button_up():
	ip = $JoinPanel/join_ip
	port = $JoinPanel/join_port
	print("Netplay - Attempting to join game at IP ",ip," via port ",port)
	join_game()

func _on_HostGameBtn_button_up():
	var host_port = $HostPanel/host_port.value
	print("Netplay - Attempting to host game on port ",host_port,"...")
	host_game(host_port)

func host_game(port):
	var maxplayers = $HostPanel/playercount.value
	var host = NetworkedMultiplayerENet.new()
	print("Netplay - Returned: ",host)
	host.create_server(port, maxplayers) # ports, maxplayers
	get_tree().set_network_peer(host)
	print("Netplay - Hosting on port ",port,". Max players: ",maxplayers)
	print("Netplay - Are you the server ... ",get_tree().is_network_server())
	$HostPanel/HostGameBtn.disabled = 1
	$HostPanel/HostGameBtn.hint_tooltip = "Cannot start another server, Server already started."
	$JoinPanel/JoinGameBtn.disabled = 1
	$JoinPanel/JoinGameBtn.hint_tooltip = "Cannot join another server, Server already started."
	#Goto a lobby instance

func join_game():
	var host = NetworkedMultiplayerENet.new()
	host.create_client(ip, port)
	get_tree().set_network_peer(host)
	print("Maybe it joined...?")
