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

public class ScoreDisplay : MonoBehaviour
{

    public List<ClientData> ConnectedClients;
    public ClientList clientDataList;

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

    public ScoreBoard mainscoreboard;

    /// <summary>
    /// calculates and assigns scores in desc order, call coroutine
    /// </summary>
    /// 

    /// <summary>
	/// couroutine to display level scores
	/// </summary>
	IEnumerator displayforSeconds(GameObject display, float time)
    {
        display.SetActive(true);
        yield return new WaitForSeconds(time);
        display.SetActive(false);
        mainscoreboard.endGame();
    }

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

        for (int i = 0; i < ConnectedClients.Count; i++)
        {
            players[i].GetComponent<TextMeshProUGUI>().text = ConnectedClients[i].Name;
            scores[i].GetComponent<TextMeshProUGUI>().text = ConnectedClients[i].Lives.ToString();
        }
        int assignedPoints = 3;
        for (int i = 0; i < ConnectedClients.Count; i++)
        {
            ConnectedClients[i].Score += assignedPoints;
            assignedPoints--;
        }

            StartCoroutine(displayforSeconds(levelScoreboard, 5.0f));
    }

    public void Start()
    {
        levelComplete();
    }

}
