using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TransitionManager : MonoBehaviour
{
    public static TransitionManager instance { get; private set; }

    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
            instance = this;
        }
    }

    public float beforeTransitionWaitTime;
    public float afterTransitionWaitTime;

    public Animator transitionAnimator;

    public void ChangeScene(string DesiredScene)
    {
        StartCoroutine(ChangeSceneCoroutine(DesiredScene));
    }

    public void ChangeScene(int DesiredScene)
    {
        StartCoroutine(ChangeSceneCoroutine(DesiredScene));
    }

    public void ResetScene()
    {
        StartCoroutine(ResetSceneCoroutine());
    }

    IEnumerator ChangeSceneCoroutine(string DesiredScene)
    {
        yield return new WaitForSeconds(beforeTransitionWaitTime);

        transitionAnimator.SetTrigger("end");

        yield return new WaitForSeconds(afterTransitionWaitTime);

        SceneManager.LoadScene(DesiredScene);
    }

    IEnumerator ChangeSceneCoroutine(int DesiredScene)
    {
        yield return new WaitForSeconds(beforeTransitionWaitTime);

        transitionAnimator.SetTrigger("end");

        yield return new WaitForSeconds(afterTransitionWaitTime);

        SceneManager.LoadScene(DesiredScene);
    }

    IEnumerator ResetSceneCoroutine()
    {
        yield return new WaitForSeconds(beforeTransitionWaitTime);

        transitionAnimator.SetTrigger("end");

        yield return new WaitForSeconds(afterTransitionWaitTime);

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}
