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

public class GameManager : MonoBehaviour
{
    public Transform player;
    public static Transform Player { get { if (instance == null) { return null; } return instance.player; } }

    public static GameManager instance { get; private set; }

    // public Sprite normalFace, sadFace, pogFace;


    public Text timerTxt;
    public float game_start_timer = 5;
    public float game_end_timer = 15;
    public GameObject gameOverPanel;
    public GameObject gameWonPanel;
    public Image[] stars;
    public GameObject[] bees;
    void Awake()
    {
        instance = this;
    }

    float t;
    bool gameStarted = false;
    bool beesReleased = false;
    public static void StartGame()
    {
        instance.gameStarted= true;
    }

    private void Update()
    {
        if(!gameStarted) { return; }

        t += Time.deltaTime;

        if(t < game_start_timer)
        {
            timerTxt.text = (game_start_timer - t).ToString("n0");
        }else if(t < game_end_timer)
        {
            if (!beesReleased) {
                beesReleased = true;
                ReleaseTheBees();
            } 
            timerTxt.text = (game_end_timer - t).ToString("n0");
        }else{
            StopTheBees();
            GameWon();
        }
    }


    async void ReleaseTheBees()
    {
        foreach(GameObject enemy in bees)
        {
            enemy.SetActive(true);
            await Task.Delay(200);
        }
    }

    void StopTheBees()
    {
        foreach(GameObject enemy in bees)
        {
            enemy.GetComponent<Enemy>().enabled=false;
        }
    }

    public static void GameOver()
    {
        instance.gameOverPanel.SetActive(true);
        instance.gameStarted = false;

        // instance.player.GetComponent<SpriteRenderer>().sprite = instance.sadFace;
    }

    public static void GameWon(){
        instance.gameStarted = false;
        instance.gameWonPanel.SetActive(true);
        // instance.player.GetComponent<SpriteRenderer>().sprite = instance.pogFace;

        instance.gameWon();
    }

    void gameWon(){
        float fuelLeft = Drawer.instance.drawingFuel.value / Drawer.instance.drawingFuel.maxValue;
        int level = 1;
        if(fuelLeft > 0.5f){
            level=2;
        }else if(fuelLeft> 0.8f){
            level =3;
        }
        for(int i =0;i < 3; i++){
            stars[i].color = level-1 >= i ? Color.yellow : Color.gray;
        }

        int levelNumber = int.Parse(SceneManager.GetActiveScene().name.Replace("Level",""));
        Debug.Log($"Level {levelNumber} won with {level} stars");
        LevelSelect.SetLevel(levelNumber,level);
    }

    public void NextLevel(){
        int levelNumber = int.Parse(SceneManager.GetActiveScene().name.Replace("Level",""));

        SceneManager.LoadScene($"Level{levelNumber+1}");
    }

    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void BackToMenu(){
        SceneManager.LoadScene(0);
    }
}
