using System.Collections;
using System.Collections.Generic;
using Assets._game.scripts;
using UnityEngine;
using UnityEngine.UI;

public class UIEndScreen : MonoBehaviour
{
    public AudioClip Musica;
    public GameObject Managers;
    public UIPlayerReportItem ReportItem;
    public Transform ReportParent;
    public Text CountDownLabel;

    public void Awake()
    {
        if (GameManager.Instance == null)
        {
            Instantiate(Managers);
        }
    }

    public void Start()
    {
        AudioManager.Instance.ChangeBGM(Musica);
        if (GameManager.Instance.Stats != null)
        {
            foreach (var stats in GameManager.Instance.Stats.players)
            {
                var reportItem = Instantiate(ReportItem, ReportParent);
                reportItem.Init(stats);
            }
        }

        StartCoroutine(ReturnToMenu());
    }

    public IEnumerator ReturnToMenu()
    {
        float endTime = Time.timeSinceLevelLoad + 20;
        int lastInt = -1;
        while (Time.timeSinceLevelLoad < endTime)
        {
            var timeLeft = endTime - Time.timeSinceLevelLoad;
            int timeInt = (int)timeLeft;
            if (timeInt != lastInt)
            {
                CountDownLabel.text = string.Format("Returning in {0:0}s", timeLeft);
            }
            lastInt = (int)timeLeft;
            
            yield return null;
        }


        GameManager.Instance.GoToMenu();
    }

}
