using System;
using System.Linq;
using Level;
using UI;
using UnityEngine;

namespace Pausable
{
    public class Pauser : MonoBehaviour
    {
        protected Input.PlayerInput playerInput = null;
        protected bool isPaused = false;

        protected void Start()
        {
            playerInput = new Input.PlayerInput();
            playerInput.Enable();
            playerInput.PlayerControl.Pause.performed += (context) => TogglePause();
        }

        public void TogglePause()
        {
            if (LevelManager.Instance.CurrentLevel == null)
                return;

            isPaused = !isPaused;

            if (isPaused)
                UIManager.Instance.SwitchToCanvas("Pause Menu");
            else
                UIManager.Instance.CloseAllCanvases();

            foreach (IPausable pausable in FindObjectsOfType<MonoBehaviour>().OfType<IPausable>())
                if (isPaused)
                    pausable.Pause();
                else
                    pausable.Resume();
        }
    }
}
