using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class NetworkManagerPhoton : Photon.MonoBehaviour {
	// Use this for initialization
	public UnityEngine.UI.Text dataText;
	public string actorID;
	public string currentTime;
	private bool inRoom = false;
	public int playerCount;
	//private UnityEngine.UI.Text startButton;
	//public UnityEngine.UI menuStart;
	void Start(){
		PhotonNetwork.ConnectUsingSettings("0.1");
		//startButton = GameObject.Find ("StartButton").GetComponent<Text>();
		//menuStart = GameObject.Find ("Canvas").GetComponent<UnityEngine.UI> ();
		//menuStart.transform.
		}

	private const string roomName = "RoomName";
	private RoomInfo[] roomsList;

	void OnGUI()
	{
		if (!PhotonNetwork.connected) {
			GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
		} else if (inRoom == false) {
			if (roomsList == null || roomsList.Length == 0) {
				// Create Room
				if (GUI.Button (new Rect ((Screen.width/2 - 125), (Screen.height/2 - 50), 250, 100), "<color=yellow><size=40>Start Server</size></color>"))
					PhotonNetwork.CreateRoom (roomName, new RoomOptions () { maxPlayers = 20}, null);
			} else if (roomsList.Length >= 1) { //Join room
					for (int i = 0; i < roomsList.Length; i++) {
					if (GUI.Button (new Rect ((Screen.width/2 - 125), (Screen.height/2 - 50) + (110 * i), 250, 100), "<color=yellow><size=40>Join Server</size></color>"))
							PhotonNetwork.JoinRoom (roomsList [i].name);
					}
				}
			}
		}
	//	foreach (PhotonPlayer pl in PhotonNetwork.playerList) {
	//		GUILayout.Box ("You are player " + pl.name);
	

	void OnReceivedRoomListUpdate()
	{
		roomsList = PhotonNetwork.GetRoomList();
	}
	void OnJoinedRoom()
	{
		dataText = GameObject.Find ("DataText").GetComponent<UnityEngine.UI.Text>();
		int playerNumber = Random.Range (1, 100);
		if (playerNumber == 1) {PhotonNetwork.player.name = "Aardvark";}
		else if (playerNumber == 2) {PhotonNetwork.player.name = "Albatross";}
		else if (playerNumber == 3) {PhotonNetwork.player.name = "Alligator";}
		else if (playerNumber == 4) {PhotonNetwork.player.name = "Alpaca";}
		else if (playerNumber == 5) {PhotonNetwork.player.name = "Ant";}
		else if (playerNumber == 6) {PhotonNetwork.player.name = "Ape";}
		else if (playerNumber == 7) {PhotonNetwork.player.name = "Donkey";}
		else if (playerNumber == 8) {PhotonNetwork.player.name = "Yak";}
		else if (playerNumber == 9) {PhotonNetwork.player.name = "Badger";}
		else if (playerNumber == 10) {PhotonNetwork.player.name = "Beaver";}
		else if (playerNumber == 11) {PhotonNetwork.player.name = "Bison";}
		else if (playerNumber == 12) {PhotonNetwork.player.name = "Butterfly";}
		else if (playerNumber == 13) {PhotonNetwork.player.name = "Camel";}
		else if (playerNumber == 14) {PhotonNetwork.player.name = "Kitten";}
		else if (playerNumber == 15) {PhotonNetwork.player.name = "Puppy";}
		else if (playerNumber == 16) {PhotonNetwork.player.name = "Caterpillar";}
		else if (playerNumber == 17) {PhotonNetwork.player.name = "Cheetah";}
		else if (playerNumber == 18) {PhotonNetwork.player.name = "Chicken";}
		else if (playerNumber == 19) {PhotonNetwork.player.name = "Cobra";}
		else if (playerNumber == 20) {PhotonNetwork.player.name = "Coyote";}
		else if (playerNumber == 21) {PhotonNetwork.player.name = "Crab";}
		else if (playerNumber == 22) {PhotonNetwork.player.name = "Crocodile";}
		else if (playerNumber == 23) {PhotonNetwork.player.name = "Dinosaur";}
		else if (playerNumber == 24) {PhotonNetwork.player.name = "Dolphin";}
		else if (playerNumber == 25) {PhotonNetwork.player.name = "Dove";}
		else if (playerNumber == 26) {PhotonNetwork.player.name = "Dragonfly";}
		else if (playerNumber == 27) {PhotonNetwork.player.name = "Unicorn";}
		else if (playerNumber == 28) {PhotonNetwork.player.name = "Duck";}
		else if (playerNumber == 29) {PhotonNetwork.player.name = "Eagle";}
		else if (playerNumber == 30) {PhotonNetwork.player.name = "Elephant";}
		else if (playerNumber == 31) {PhotonNetwork.player.name = "Emu";}
		else if (playerNumber == 32) {PhotonNetwork.player.name = "Falcon";}
		else if (playerNumber == 33) {PhotonNetwork.player.name = "Ferret";}
		else if (playerNumber == 34) {PhotonNetwork.player.name = "Finch";}
		else if (playerNumber == 35) {PhotonNetwork.player.name = "Flamingo";}
		else if (playerNumber == 36) {PhotonNetwork.player.name = "Fox";}
		else if (playerNumber == 37) {PhotonNetwork.player.name = "Frog";}
		else if (playerNumber == 38) {PhotonNetwork.player.name = "Gazelle";}
		else if (playerNumber == 39) {PhotonNetwork.player.name = "Gerbil";}
		else if (playerNumber == 40) {PhotonNetwork.player.name = "Panda";}
		else if (playerNumber == 41) {PhotonNetwork.player.name = "Giraffe";}
		else if (playerNumber == 42) {PhotonNetwork.player.name = "Gnu";}
		else if (playerNumber == 43) {PhotonNetwork.player.name = "Goat";}
		else if (playerNumber == 44) {PhotonNetwork.player.name = "Goose";}
		else if (playerNumber == 45) {PhotonNetwork.player.name = "Goldfish";}
		else if (playerNumber == 46) {PhotonNetwork.player.name = "Gorilla";}
		else if (playerNumber == 47) {PhotonNetwork.player.name = "Grasshopper";}
		else if (playerNumber == 48) {PhotonNetwork.player.name = "Guinea Pig";}
		else if (playerNumber == 49) {PhotonNetwork.player.name = "Hamster";}
		else if (playerNumber == 50) {PhotonNetwork.player.name = "Hare";}
		else if (playerNumber == 51) {PhotonNetwork.player.name = "Hawk";}
		else if (playerNumber == 52) {PhotonNetwork.player.name = "Hedgehog";}
		else if (playerNumber == 53) {PhotonNetwork.player.name = "Heron";}
		else if (playerNumber == 54) {PhotonNetwork.player.name = "Hippopotamus";}
		else if (playerNumber == 55) {PhotonNetwork.player.name = "Horse";}
		else if (playerNumber == 56) {PhotonNetwork.player.name = "Hornet";}
		else if (playerNumber == 57) {PhotonNetwork.player.name = "Hummingbird";}
		else if (playerNumber == 58) {PhotonNetwork.player.name = "Jaguar";}
		else if (playerNumber == 59) {PhotonNetwork.player.name = "Jellyfish";}
		else if (playerNumber == 60) {PhotonNetwork.player.name = "Kangaroo";}
		else if (playerNumber == 61) {PhotonNetwork.player.name = "Koala";}
		else if (playerNumber == 62) {PhotonNetwork.player.name = "Lemur";}
		else if (playerNumber == 63) {PhotonNetwork.player.name = "Leopard";}
		else if (playerNumber == 64) {PhotonNetwork.player.name = "Llama";}
		else if (playerNumber == 65) {PhotonNetwork.player.name = "Lobster";}
		else if (playerNumber == 66) {PhotonNetwork.player.name = "Manatee";}
		else if (playerNumber == 67) {PhotonNetwork.player.name = "Mantis";}
		else if (playerNumber == 68) {PhotonNetwork.player.name = "Meerkat";}
		else if (playerNumber == 69) {PhotonNetwork.player.name = "Mink";}
		else if (playerNumber == 70) {PhotonNetwork.player.name = "Mongoose";}
		else if (playerNumber == 71) {PhotonNetwork.player.name = "Monkey";}
		else if (playerNumber == 72) {PhotonNetwork.player.name = "Mouse";}
		else if (playerNumber == 73) {PhotonNetwork.player.name = "Moose";}
		else if (playerNumber == 74) {PhotonNetwork.player.name = "Mosquito";}
		else if (playerNumber == 75) {PhotonNetwork.player.name = "Narwhal";}
		else if (playerNumber == 76) {PhotonNetwork.player.name = "Octopus";}
		else if (playerNumber == 77) {PhotonNetwork.player.name = "Ostrich";}
		else if (playerNumber == 78) {PhotonNetwork.player.name = "Otter";}
		else if (playerNumber == 79) {PhotonNetwork.player.name = "Owl";}
		else if (playerNumber == 80) {PhotonNetwork.player.name = "Parrot";}
		else if (playerNumber == 81) {PhotonNetwork.player.name = "Pelican";}
		else if (playerNumber == 82) {PhotonNetwork.player.name = "Penguin";}
		else if (playerNumber == 83) {PhotonNetwork.player.name = "Polar Bear";}
		else if (playerNumber == 84) {PhotonNetwork.player.name = "Pony";}
		else if (playerNumber == 85) {PhotonNetwork.player.name = "Prairie Dog";}
		else if (playerNumber == 86) {PhotonNetwork.player.name = "Rabbit";}
		else if (playerNumber == 87) {PhotonNetwork.player.name = "Raccoon";}
		else if (playerNumber == 88) {PhotonNetwork.player.name = "Reindeer";}
		else if (playerNumber == 89) {PhotonNetwork.player.name = "Salamander";}
		else if (playerNumber == 90) {PhotonNetwork.player.name = "Scorpion";}
		else if (playerNumber == 91) {PhotonNetwork.player.name = "Seahorse";}
		else if (playerNumber == 92) {PhotonNetwork.player.name = "Shark";}
		else if (playerNumber == 93) {PhotonNetwork.player.name = "Sheep";}
		else if (playerNumber == 94) {PhotonNetwork.player.name = "Skunk";}
		else if (playerNumber == 95) {PhotonNetwork.player.name = "Sparrow";}
		else if (playerNumber == 96) {PhotonNetwork.player.name = "Sprider";}
		else if (playerNumber == 97) {PhotonNetwork.player.name = "Squirrel";}
		else if (playerNumber == 98) {PhotonNetwork.player.name = "Swan";}
		else if (playerNumber == 99) {PhotonNetwork.player.name = "Wolverine";}
		else if (playerNumber == 100) {PhotonNetwork.player.name = "Zebra";}


		actorID = PhotonNetwork.player.name;
		currentTime = System.DateTime.Now.ToString ();
		//playerCount = Room.playerCount;
		photonView.RPC ("PlayerJoin", PhotonTargets.All, actorID);
		inRoom = true;

	}
	void Update() {

		//Debug.Log(PhotonNetwork.countOfPlayers);
	}

	void OnConnectionFail()
	{
		Debug.Log("OnConnectionFail");
	}


[RPC] void PlayerJoin (string ID)
	{
		Debug.Log(ID + " joined the room at " + currentTime);
		dataText.text = "Anonymous " + ID + " has joined the soundscape";
	}

}