using UnityEngine;
using System.Collections;

public class Intro : FadeCallback
{
    public AudioSource music;

    public MonoBehaviour faderScript; // Reference to the Fader script for handling the fade effect

    private IEnumerator playAudioCoroutine;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start() { }

    // Update is called once per frame
    void Update() { }

    public override void OnFadeStarted()
    {
        playAudioCoroutine = PlayAudioThenActivateGameObject();
        StartCoroutine(playAudioCoroutine);
    }

    public override void OnFadeComplete() { }

    public void ForceStopAudioCoroutine()
    {
        if (playAudioCoroutine != null)
        {
            StopCoroutine(playAudioCoroutine);
            playAudioCoroutine = null;
        }

        music.Stop();
        faderScript.Invoke("ResetFader", 0.0f);
    }

    private IEnumerator PlayAudioThenActivateGameObject()
    {
        music.Play();
        yield return new WaitUntil(() => music.time >= music.clip.length);
        faderScript.Invoke("ResetFader", 0.0f);
    }
}
