using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Networking.Server;
using Networking;
using TMPro;
using UnityEngine.SceneManagement;

public class ScoreBoard : MonoBehaviour
{
    public ClientList clientDataList;
	public List<ClientData> ConnectedClients;

    public GameObject[] players = new GameObject[4];
    public GameObject[] scores = new GameObject[4];

    public string nextScene;
	public GameObject MainScoreboard;

    private void Awake()
    {
        MainScoreboard.SetActive(false);
    }

    IEnumerator displayforSeconds(GameObject display, float time)
	{
		display.SetActive (true);
		yield return new WaitForSeconds(time);
        SceneManager.LoadScene("MainMenu Server");
	}

	public void endGame()
	{ 
		ConnectedClients = clientDataList.ConnectedClients;
		ConnectedClients.Sort((a, b) => b.Score.CompareTo(a.Score));

        for (int i = 0; i < ConnectedClients.Count; i++)
        {
            players[i].GetComponent<TextMeshProUGUI>().text = ConnectedClients[i].Name;
            scores[i].GetComponent<TextMeshProUGUI>().text = ConnectedClients[i].Score.ToString();
        }
        StartCoroutine(displayforSeconds(MainScoreboard, 5.0f));
	}
}
