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

public class PlayerCount : Photon.MonoBehaviour {
	public UnityEngine.UI.Text dataText;
	private UnityEngine.UI.Text playerCount;
	private bool hasJoined = false;
	public string currentTime;
	private int numberOfPlayers;

	// Use this for initialization
	void Start () {
		dataText = GameObject.Find ("DataText").GetComponent<UnityEngine.UI.Text>();

		playerCount = GetComponent<Text>();
		//text = GetComponent<Text> ();
	}
	void OnJoinedRoom() {
		hasJoined = true;
		numberOfPlayers = PhotonNetwork.playerList.Length;
	}
	// Update is called once per frame
	void Update () {
		if (hasJoined == true) {
			if ((PhotonNetwork.playerList.Length - 1) == 1) { 
				playerCount.text = ((PhotonNetwork.playerList.Length - 1) + " player is connected");
			} else {
				playerCount.text = ((PhotonNetwork.playerList.Length - 1) + " players are connected");
			}
			if (numberOfPlayers > PhotonNetwork.playerList.Length) {
				PlayerLeave ();
			}
			numberOfPlayers = PhotonNetwork.playerList.Length;
		}
	}
[RPC] void PlayerLeave ()
	{
		currentTime = System.DateTime.Now.ToString ();
		Debug.Log("A use has left the soundscape at " + currentTime);
		dataText.text = "A user has left the soundscape";
	}

}