using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class CreditsText : MonoBehaviour
{
    [TextArea]
    public string[] texts;
    TextMeshProUGUI text;
    Coroutine routine;

    private void Start()
    {
        text = GetComponent<TextMeshProUGUI>();
    }

    public void PlayCredits()
    {
        text = GetComponent<TextMeshProUGUI>();

        if (routine != null) { StopCoroutine(routine); }
        routine = StartCoroutine(CreditsC());
    }

    IEnumerator CreditsC()
    {
        for (int i = 0; i < texts.Length; i++, i%=texts.Length)
        {
            text.color = Color.clear;
            text.text = texts[i];
            float t = 0.0f;
            
            while(t < 1.0f)
            {
                text.color = Color.Lerp(Color.clear, Color.white, t);
                t += Time.deltaTime;
                yield return null;
            }
            yield return new WaitForSeconds(3.0f);
            
            t = 0.0f;
            while (t < 1.0f)
            {
                text.color = Color.Lerp(Color.white, Color.clear, t);
                t += Time.deltaTime;
                yield return null;
            }
        }
    }

    private void OnDisable()
    {
        if (routine != null) { StopCoroutine(routine); }

    }
}
