using UnityEngine;
using System.Collections;

public class ScoreChecker : MonoBehaviour
{
    public AudioSource winSound;

    public AudioSource loseSound;

    public Fader fadeOut;

    // 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 void ResetFader()
    {
        Invoke(nameof(CheckScore), 0.5f);
    }

    private IEnumerator PlayAudioThenActivateGameObject(AudioSource audio, bool showGui)
    {
        audio.Play();

        if (showGui) 
        {
            // Activate your GUI here
        }
        else
        {
            yield return new WaitUntil(() => audio.time >= audio.clip.length);
            fadeOut.ResetFader();
        }
    }

    void CheckScore()
    {
        int currentScore = PlayerPrefs.GetInt("CurrentScore");
        AudioSource audio = null;
        bool showGui = false;

        if (GameSetup.IsHighScore(currentScore))
        {
            audio = winSound;
            showGui = true;
        }
        else
        {
            audio = loseSound;
        }

        StartCoroutine(PlayAudioThenActivateGameObject(audio, showGui));
    }
}
