using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Photon.Pun;

public class PlayersRemainingDisplay : MonoBehaviour {

    public TMP_Text playersRemainingGameplayText;
    public TMP_Text playersRemainingSpectatorText;
    int remainingPlayers = -1;
	
	// Update is called once per frame
	void Update () {
        if (PhotonNetwork.IsConnected && PhotonNetwork.CurrentRoom != null) {
            Dictionary<int, Photon.Realtime.Player> players = PhotonNetwork.CurrentRoom.Players;
            remainingPlayers = 0;
            foreach (KeyValuePair<int, Photon.Realtime.Player> player in players) {
                object prop;
                if(player.Value.CustomProperties.TryGetValue("remaining", out prop)){
                    if ((bool)prop) {
                        remainingPlayers++;
                    }
                }
            }
            if (playersRemainingGameplayText) {
                playersRemainingGameplayText.SetText(remainingPlayers.ToString());
            }
            if (playersRemainingSpectatorText) {
                playersRemainingGameplayText.SetText(remainingPlayers.ToString());
            }
        }
	}

    public int GetRemainingPlayers() {
        return remainingPlayers;
    }
}
