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

public class PlayerController07 : MonoBehaviour{

    public GameObject death;
    public string menuScene;
    public string[] scenes;
    public Image timeBar;
    public float maxTime = 5;
    float currentTime;
    Rigidbody2D rb2d;

    // Start is called before the first frame update
    void Start(){
        currentTime = maxTime;
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update(){
        currentTime -= 1 * Time.deltaTime;
        timeBar.transform.localScale = new Vector2(currentTime / maxTime, timeBar.transform.localScale.y);
        if (currentTime <= 0){
            StartCoroutine(Death());
        }
    }

    void OnTriggerStay2D(Collider2D col){
        if (rb2d.velocity.y < 0){
            CompleteLevel();
        }
    }

    IEnumerator Death(){
        Time.timeScale = 0;
        death.SetActive(true);
        yield return new WaitForSecondsRealtime(4f);
        LoseGame();
    }

    public void CompleteLevel(){
        string scene = scenes[Random.Range(0, scenes.Length - 1)];
        LevelLoader.Instance.LoadLevel(scene);
    }

    public void LoseGame(){
        Debug.LogError(SceneManager.GetActiveScene().name);
        LevelLoader.Instance.LoadLevel(menuScene);
    }
}
