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

public sealed class CreditsUICounter : MonoBehaviour
{
    [SerializeField]
    private Text creditsLabel = null;
    [SerializeField]
    private string creditsSymbol = "◯ ";
    [SerializeField]
    private float effectTime = 1f;
    [SerializeField]
    private Gradient effectGradient = new Gradient()
    {
        colorKeys = new GradientColorKey[] {
        new GradientColorKey(Color.white, 0),
        new GradientColorKey(Color.red, 0.5f),
        new GradientColorKey(Color.white, 1f)
    }
    };

    public void Show(int credits)
    {
        creditsLabel.text = $"{creditsSymbol}{credits}";
        gameObject.SetActive(true);
    }

    public void Hide() => gameObject.SetActive(false);

    public void ShowNoCreditsEffect() => StartCoroutine(ShowErrorEffect(creditsLabel, effectTime));

    private IEnumerator ShowErrorEffect(Text label, float showTime)
    {
        var timer = showTime;
        while (timer > 0)
        {
            timer -= Time.deltaTime;
            var animation = 1f - timer / showTime;
            label.color = effectGradient.Evaluate(animation);
            yield return null;
        }
        label.color = effectGradient.Evaluate(1);
    }
}
