using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Immortal : MonoBehaviour {

	private static List<Tuple<string, int>> serverList = new List<Tuple<string, int>>();

	private static bool onClientStart = false;

	// Use this for initialization
	void Start () {
		NetworkManager.initConnection();
		DontDestroyOnLoad(this);
	}
	
	// Update is called once per frame
	void Update () {
		NetworkManager.manageNetworkInputs();
	}

	void OnGUI() {
		if (onClientStart) {
			for (int i = 0; i < serverList.Count; i++) {
				GUI.Label(new Rect(300, 200 + i * 50, 150, 25), serverList[i]._1);

				GUI.Label(new Rect(500, 200 + i * 50, 150, 25), "" + serverList[i]._2);

				if (GUI.Button(new Rect(700, 200 + i * 50, 150, 25), "Connect")) {
					NetworkManager.connectToServer(serverList[i]._1, serverList[i]._2);
				}
			}
		}


	}


	public void startClient() {
		if (NetworkManager.isConnectedToMasterServer()) {
			SceneManager.LoadScene("ClientStart", LoadSceneMode.Single);
			NetworkManager.indicateIsServer(false);
			onClientStart = true;
		}
	}

	public static void addServerToList(string ip, int port) {
		serverList.Add(new Tuple<string, int>(ip, port));
	}

	public void startServer() {
		if (true || NetworkManager.isConnectedToMasterServer()) {
			NetworkManager.indicateIsServer(true);
			startGame();
		}
	}

	public static void startGame() {
		onClientStart = false;
		SceneManager.LoadScene("Game", LoadSceneMode.Single);
	}
}
