using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Menu : MonoBehaviour
{
    [SerializeField] private GameObject escMenuUI;
    [SerializeField] private Button resumeButton;
    [SerializeField] private Button restartButton;
    [SerializeField] private Button quitButton;

    private bool isPaused = false;

    void Start()
    {
        // Ensure the menu is hidden at the start
        escMenuUI.SetActive(false);

        // Assign button listeners
        resumeButton.onClick.AddListener(ResumeGame);
        restartButton.onClick.AddListener(RestartGame);
        quitButton.onClick.AddListener(QuitToMainMenu);
    }

    void Update()
    {
        // Toggle the escape menu
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("Escape key pressed");
            if (isPaused)
                ResumeGame();
            else
                PauseGame();
        }
    }

    private void PauseGame()
    {
        Debug.Log("Paused");
        isPaused = true;
        escMenuUI.SetActive(true);
        Time.timeScale = 0f; // Pause the game
    }

    public void ResumeGame()
    {
        Debug.Log("Resumed");
        isPaused = false;
        escMenuUI.SetActive(false);
        Time.timeScale = 1f; // Resume the game
    }

    public void RestartGame()
    {
        Debug.Log("Restarted");
        Time.timeScale = 1f; // Reset time scale before restarting
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void QuitToMainMenu()
    {
        Debug.Log("Quitting to Main Menu");
        Time.timeScale = 1f; // Reset time scale before quitting
        SceneManager.LoadScene("Main"); // Replace "MainMenu" with the actual name of your main menu scene
    }
}
