using System;
using UnityEngine;
using UnityEngine.UI;

public class PauseMenuUI : MonoBehaviour
{
    [SerializeField] private GameObject pauseMenuObject;
    [SerializeField] private Button resumeButton;
    [SerializeField] private Button optionsButton;
    [SerializeField] private Button quitButton;
    [SerializeField] private OptionsMenuUI optionsMenuUIReference;
    void Awake()
    {
        DeactivatePauseMenu();
        AssignEventsToTheButtons();
    }
    
    void Start()
    {
        GameManager.Instance.OnGameStateChanged += HandleGameStateChange;
    }
    
    private void OnDestroy()
    {
        GameManager.Instance.OnGameStateChanged -= HandleGameStateChange;
    }

    private void AssignEventsToTheButtons()
    {
        resumeButton.onClick.AddListener(()=> GameManager.Instance.ResumeGame());
        optionsButton.onClick.AddListener(OpenOptionsMenu);
        quitButton.onClick.AddListener(()=> GameManager.Instance.GoToMainMenu());
    }
    private void HandleGameStateChange(GameState state)
    {
        if (state == GameState.Paused)
        {
            OpenPauseMenu();
        }else if (state == GameState.Playing || state == GameState.Tutorial)
        {
            ClosePauseMenu();
        }
    }

    public void OpenOptionsMenu()
    {
        optionsMenuUIReference.ActivateChild();
        ClosePauseMenu();

    }

    public void CloseOptionsMenu()
    {
        optionsMenuUIReference.DeactivateChild();
        OpenPauseMenu();
    }
    private void OpenPauseMenu()
    {
        pauseMenuObject.SetActive(true);
    }

    private void ClosePauseMenu()
    {
        pauseMenuObject.SetActive(false);
    }

    private void DeactivatePauseMenu()
    {
        pauseMenuObject.SetActive(false);
    }
    
}
