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 FirstPlaceName;
	public GameObject FirstPlaceScore;
	public GameObject SecondPlaceName;
	public GameObject SecondPlaceScore;
	public GameObject ThirdPlaceName;
	public GameObject ThirdPlaceScore;
	public GameObject FourthPlaceName;
	public GameObject FourthPlaceScore;

	public string nextScene;
	public GameObject MainScoreboard;

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

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

		FirstPlaceName.GetComponent<TextMeshProUGUI>().text = ConnectedClients[0].Name;
		FirstPlaceScore.GetComponent<TextMeshProUGUI>().text = ConnectedClients[0].Score.ToString();
		SecondPlaceName.GetComponent<TextMeshProUGUI>().text = ConnectedClients[1].Name;
		SecondPlaceScore.GetComponent<TextMeshProUGUI>().text = ConnectedClients[1].Score.ToString();
		ThirdPlaceName.GetComponent<TextMeshProUGUI>().text = ConnectedClients[2].Name;
		ThirdPlaceScore.GetComponent<TextMeshProUGUI>().text = ConnectedClients[2].Score.ToString();
		FourthPlaceName.GetComponent<TextMeshProUGUI>().text = ConnectedClients[3].Name;
		FourthPlaceScore.GetComponent<TextMeshProUGUI>().text = ConnectedClients[3].Score.ToString();

		StartCoroutine(displayforSeconds(MainScoreboard, 10.0f));
	}
}
