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

public sealed class SpaceUICounter : MonoBehaviour
{
    [SerializeField]
    private Text spaceLabel = null;
    [SerializeField]
    private string spaceSymbol = "◻ ";
    [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 space, int maxSpace)
    {
        spaceLabel.text = $"{spaceSymbol}{space}/{maxSpace}";
        spaceLabel.color = effectGradient.Evaluate(1f);
        gameObject.SetActive(true);
    }

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

    public void ShowNoSpaceEffect() => StartCoroutine(ShowErrorEffect(spaceLabel, 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);
    }
}
