using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PauseMenu : MonoBehaviour
{
    public GameObject pauseMenuPanel; // Reference to the pause menu panel
    public Button pauseButton; // Reference to the pause button
    public Button resumeButton; // Reference to the resume button
    public Button restartButton; // Reference to the restart button
    public Button quitButton; // Reference to the quit button

    private bool isPaused = false;
    private bool muertoB = false;

    void Start()
    {
        // Make sure the pause menu is hidden at the start
        pauseMenuPanel.SetActive(false);
        
        // Assign button listeners
        pauseButton.onClick.AddListener(TogglePause);
        resumeButton.onClick.AddListener(ResumeGame);
        restartButton.onClick.AddListener(RestartGame);
        quitButton.onClick.AddListener(QuitGame);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape) && !muertoB)
        {
            TogglePause();
        }
    }

    void TogglePause()
    {
        if (isPaused)
        {
            ResumeGame();
        }
        else
        {
            PauseGame();
        }
    }

    void PauseGame()
    {
        Time.timeScale = 0f; // Stops the game time (pauses the game)
        pauseMenuPanel.SetActive(true); // Show the pause menu
        pauseButton.gameObject.SetActive(false); // Hide the pause button
        isPaused = true;
    }

    void ResumeGame()
    {
        Time.timeScale = 1f; // Resumes the game time
        pauseMenuPanel.SetActive(false); // Hide the pause menu
        pauseButton.gameObject.SetActive(true); // Show the pause button again
        isPaused = false;
    }

    public void RestartGame()
    {
        Time.timeScale = 1f; // Ensure the game time is running before restarting
        SceneManager.LoadScene("Inicio"); // Reload the current scene
    }

    public void EmpezarDeNuevo()
    {
        Time.timeScale = 1f; // Ensure the game time is running before restarting
        SceneManager.LoadScene(sceneBuildIndex: 2);
    }

    public void QuitGame()
    {
        Time.timeScale = 1f; // Ensure the game time is running before quitting
        Application.Quit(); // Quit the game
    }

    public void muerto()
    {
        muertoB = true;
    }
}
