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

public class GameManager : MonoBehaviour{
    public GameObject Ship, Player, containerGameOver, btn_pause, containerTutorial, Enemy, Fade;
    public GameObject[] arrows;
    public AudioSource musics, audios;
    public SpriteRenderer value, bar;
    public AudioClip[] clips;
    public Slider timerslider;
    public int level, seedMax, seeds, tutorial, life, take;
    public TextMeshProUGUI time;
    public float minutes, op, seconds;
    public bool isPause, gameover, win, EnterShip;
    public static GameManager Instance;
    void Awake(){
        Instance = this;    
    }
    void Start(){
        life = 1;
        seconds = 65;
        timerslider.maxValue = 3;
        tutorial = PlayerPrefs.GetInt("tutorial");
        time.gameObject.SetActive(false);
        timerslider.gameObject.SetActive(false);
        btn_pause.SetActive(false);
        Player.GetComponent<Player>().move = true;
        GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[6]);
        GameManager.Instance.musics.PlayOneShot(GameManager.Instance.clips[7]);
    }
    public void GoMenu(){
        SceneManager.LoadScene("Menu");
    }
    public void paused(GameObject panelPause){
        if(isPause){
            panelPause.SetActive(false);
            Time.timeScale = 1;
            isPause =false;
        }else{
            panelPause.SetActive(true);
            Time.timeScale = 0;
            isPause = true;
        }
    }
    public void Tutorial(){
        tutorial++;
        if(tutorial == 1){
            arrows[0].SetActive(false);
            arrows[1].SetActive(true);
        }
        if(tutorial == 3){
            arrows[1].SetActive(false);
            arrows[2].SetActive(true);
        }
        if(tutorial == 4){
            arrows[2].SetActive(false);
        }
    }
    void Update() {
        timerslider.value = seeds;
        time.text = seconds.ToString("00");
        if(life == 0 || seconds <= 0f){
            if(Fade.GetComponent<Image>().color.a < 1f && gameover){
                Fade.GetComponent<Image>().color = new Color(0f,0f,0f, op += 0.5f * Time.deltaTime);
            }
            if(Fade.GetComponent<Image>().color.a >= 1f){
                SceneManager.LoadScene("Menu");
            }
        }
        if(EnterShip){
            if(Fade.GetComponent<Image>().color.a < 1f){
                Fade.GetComponent<Image>().color = new Color(0f,0f,0f, op += 0.5f * Time.deltaTime);
            }
            if(Fade.GetComponent<Image>().color.a >= 1f){
                SceneManager.LoadScene("Menu");
            }
        }
        if(take >= 3){
            arrows[1].SetActive(true);
            win = true;
            PlayerPrefs.SetInt("win", 1);
        }
        if(seconds > 0){
            seconds -= 1f * Time.deltaTime;
        }
       if(Input.anyKey && Time.timeScale != 1f){
           Time.timeScale = 1f;
           GameManager.Instance.containerTutorial.SetActive(false);
           Enemy.GetComponent<Enemy>().active = true;
           Player.GetComponent<Rigidbody2D>().AddForce(new Vector2(50f, -50f));
       }
    }
}