using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ManagerUI : MonoBehaviour
{
    [SerializeField] public GameObject player;
    [SerializeField] public GameObject gameOverPanel;
    [SerializeField] public Text gameOverText;
    [SerializeField] public Text restartText;
    [SerializeField] public Text HP;
    [SerializeField] public Text Energy;
    public bool isGameOver = false;
    private Droid playerDroid;

    void Start()
    {
        //Disactive le panneau et les textes si actives
        gameOverPanel.SetActive(false);
        gameOverText.gameObject.SetActive(false);
        restartText.gameObject.SetActive(false);
    }

    void Update()
    {
        if(this.player == null) 
        {
            this.isGameOver = true;
        }
        else
        {
            playerDroid = this.player.GetComponent<Droid>();
        }
                

        //ai la touche G est pressée, lance la méthode StartCoroutine qui affiche le panneau avec le message Game Over etattends 5 séconds puis affiche un deuxième message invitant le jouer à presser R pour relancer le jeu
        if (Input.GetKeyDown(KeyCode.G) && !isGameOver)
        {
            isGameOver = true;
            Destroy(this.player);
        }
        //si le jeu est terminé et on saisie la touche R, le jeu est relancé
        if (isGameOver)
        {
            //If R is hit, restart the current scene
            if (Input.GetKeyDown(KeyCode.R))
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            }
            //la touche Q nous permet de sortir du jeu à tout moment
            if (Input.GetKeyDown(KeyCode.Q))
            {
                print("Application Quit");
                Application.Quit();
            }
        }
        if(isGameOver)
        {
            StartCoroutine(GameOverSequence());
        }



    }
    //la méthode GameOverSequence affiche le message GameOver puis attends 3 séconds et affiche le message “Press R to restart”
    private IEnumerator GameOverSequence()
    {
        HP.text="";
        Energy.text = "";
        gameOverPanel.SetActive(true);
        gameOverText.gameObject.SetActive(true);
        yield return new WaitForSeconds(3.0f);
        restartText.text = "Press R to restart";
        restartText.gameObject.SetActive(true);
    }

    public IEnumerator UpdateUI(int hpVal, int energyVal)
    {
        HP.text = "HP : " + hpVal;
        Energy.text = "Energy : " + energyVal;
        yield return new WaitForSeconds(0f);
    }
}