using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using String = System.String;

public class AnimateTextDots : MonoBehaviour
{
    [SerializeField]
    private Text textField;
    [SerializeField]
    private float animationDelay = 0.5f;

    private int numberOfDots = 3;

    private void OnEnable()
    {
        if (textField == null)
        {
            return;
        }

        StartCoroutine(TextDotsAnimation());
    }

    private IEnumerator TextDotsAnimation()
    {
        // Remove trailing dots from text
        string strippedText = textField.text.TrimEnd('.', ' ');
        textField.text = strippedText;

        // animate
        while (true)
        {
            for (int i = 0; i < numberOfDots + 1; i++)
            {
                textField.text = $"{strippedText}{new String('.', i)}{new String(' ', numberOfDots - i)}";
                yield return new WaitForSeconds(animationDelay);
            }
        }
    }

    private void OnDisable()
    {
        StopAllCoroutines();
    }
}
