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

public class EndCondition : MonoBehaviour
{
    public static bool IsGameOver = false;

    public GameObject Player;
    public CanvasGroup GameOverOverlay;
    public float FadeInLength = 1;
    public float ReturnToMenuLength = 5;

    private IKillable player;

    public void Start()
    {
        IsGameOver = false;
        player = Player.GetComponent<IKillable>();
    }

    public void Update()
    {
        if (player.Health <= 0 && !IsGameOver)
        {
            IsGameOver = true;
            StartCoroutine(FadeIn());
            StartCoroutine(ReturnToMenu());
        }
    }

    private IEnumerator FadeIn()
    {
        float start = Time.time;
        while (GameOverOverlay.alpha < 1)
        {
            GameOverOverlay.alpha += (Time.time - start) / FadeInLength;
            yield return new WaitForEndOfFrame();
        }
    }

    private IEnumerator ReturnToMenu()
    {
        yield return new WaitForSeconds(ReturnToMenuLength);
        SceneManager.LoadScene("Menu");
    }
}
