﻿using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Yumon.Template.Lite.Internal;

namespace Yumon.Template.Lite
{
    /// <summary>
    ///     Example of a text that displays the current league, automatically updating when the league changes.
    /// </summary>
    [RequireComponent(typeof(TextMeshProUGUI))]
    public class LeagueTextDebugger : MonoBehaviour
    {
        // Map the rarities to colors for fancier debugging
        readonly Dictionary<League, string> _leagueColors = new Dictionary<League, string>
        {
            { League.Practice, "white" },
            { League.Common, "green" },
            { League.Limited, "blue" },
            { League.Epic, "purple" },
        };

        TextMeshProUGUI _text;

        void Awake()
        {
            _text = GetComponent<TextMeshProUGUI>();
            YumonLeagueManager.OnLeagueChanged += OnLeagueChange;
        }

        void Start() => OnLeagueChange(YumonLeagueManager.CurrentLeague);
        void OnDestroy() => YumonLeagueManager.OnLeagueChanged -= OnLeagueChange;
        void OnLeagueChange(League league) => _text.text =
            _leagueColors.TryGetValue(league, out string color)
                ? $"Current League: <color={color}>{league}</color>" :
                $"Current League: {league}";
    }
}
