using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HighScoreManager : MonoBehaviour
{
    private Transform entryContainer;
    private Transform entryTemplate;
    private List<HighScoreEntry> highScoreEntryList;
    private List<Transform> highScoreEntryTransformList;


    private void Awake()
    {
        entryContainer = transform.Find("EntryContainer");
        entryTemplate = entryContainer.Find("EntryTemplate");
        entryTemplate.gameObject.SetActive(false);

        highScoreEntryList = new List<HighScoreEntry>()
        {
            new HighScoreEntry{score = 1000, time = "60"},
            new HighScoreEntry{score = 9000, time = "60"},
            new HighScoreEntry{score = 3000, time = "60"},
            new HighScoreEntry{score = 8000, time = "60"},
        };
        highScoreEntryList.Sort((s1, s2) => s1.score.CompareTo(s2.score));

        highScoreEntryTransformList = new List<Transform>();
        foreach(HighScoreEntry highScoreEntry in highScoreEntryList)
        {
            CreateHighScoreEntryTransform(highScoreEntry, entryContainer, highScoreEntryTransformList);
        }       
    }


    private void CreateHighScoreEntryTransform(HighScoreEntry entry, Transform container, List<Transform> transformList)
    {
        float templateHeight = 50f;
        Transform entryTransform = Instantiate(entryTemplate, container);
        RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
        entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * transformList.Count);
        entryTransform.gameObject.SetActive(true);

        entryTransform.Find("Rank").GetComponent<Text>().text = $"{transformList.Count + 1}";
        entryTransform.Find("Score").GetComponent<Text>().text = $"{entry.score}";
        entryTransform.Find("Time").GetComponent<Text>().text = entry.time;

        transformList.Add(entryTransform);
    }
    private class HighScoreEntry
    {
        public int score;
        public string time;
    }
}


