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

public class Game10 : MonoBehaviour{
    public string menuScene;
    public string[] scenes;
    public Image timeBar;
    public float maxTime = 5f;
    float currentTime;
    [Space(10)]
    public GameObject death;
    public float rotationSpeed;
    public Transform player;
    float rotZ;

    // Start is called before the first frame update
    void Start(){
        currentTime = maxTime;
        transform.localRotation = Quaternion.Euler(0, 0, Random.Range(30f, 330f));
    }

    // 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());
            death.SetActive(true);
        }
        if (Input.GetKey(KeyCode.Space)){
            rotZ -= rotationSpeed * 10 * Time.deltaTime;
        }
        player.localRotation = Quaternion.Euler(0, 0, rotZ);
    }

    void OnTriggerStay2D(Collider2D col){
        if (Input.GetKeyUp(KeyCode.Space)){
            player.localRotation = transform.localRotation;
            CompleteLevel();
        }
    }

    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);
    }

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