using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;

/// <summary>
/// GAME 1303 Tier 1 — Game state: pause, win, lose. One per scene (or persistent).
///
/// SETUP
///  1. Empty GameObject "GameManager" with this script.
///  2. Drag your Pause menu panel into Pause Menu (it will be toggled on/off).
///  3. Pause key defaults to Escape (keyboard) / Start (gamepad) via the Input System.
///  4. Wire events:
///        OnLevelComplete -> show win panel, SceneLoader.LoadNext (after a delay)
///        OnGameOver      -> show game-over panel
///     RespawnManager.OnGameOver can call GameManager.GameOver().
///  5. A level-exit trigger just needs:  GameManager.Instance.LevelComplete();
///
/// The static Instance means any script can reach it without a reference.
/// </summary>
public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }

    public enum State { Playing, Paused, LevelComplete, GameOver }
    public State CurrentState { get; private set; } = State.Playing;

    [Header("UI (optional)")]
    public GameObject pauseMenu;
    public GameObject levelCompletePanel;
    public GameObject gameOverPanel;

    [Header("Events")]
    public UnityEvent OnPaused;
    public UnityEvent OnResumed;
    public UnityEvent OnLevelComplete;
    public UnityEvent OnGameOver;

    [Header("Timer")]
    public bool trackTime = true;
    public float ElapsedTime { get; private set; }

    void Awake()
    {
        if (Instance != null && Instance != this) { Destroy(gameObject); return; }
        Instance = this;
    }

    void Start()
    {
        Time.timeScale = 1f;
        if (pauseMenu) pauseMenu.SetActive(false);
        if (levelCompletePanel) levelCompletePanel.SetActive(false);
        if (gameOverPanel) gameOverPanel.SetActive(false);
    }

    void Update()
    {
        if (CurrentState == State.Playing && trackTime) ElapsedTime += Time.deltaTime;

        // Pause toggle — Escape or gamepad Start
        bool pausePressed =
            (Keyboard.current != null && Keyboard.current.escapeKey.wasPressedThisFrame) ||
            (Gamepad.current != null && Gamepad.current.startButton.wasPressedThisFrame);

        if (pausePressed) TogglePause();
    }

    public void TogglePause()
    {
        if (CurrentState == State.Playing) Pause();
        else if (CurrentState == State.Paused) Resume();
    }

    public void Pause()
    {
        if (CurrentState != State.Playing) return;
        CurrentState = State.Paused;
        Time.timeScale = 0f;
        if (pauseMenu) pauseMenu.SetActive(true);
        OnPaused?.Invoke();
    }

    public void Resume()
    {
        if (CurrentState != State.Paused) return;
        CurrentState = State.Playing;
        Time.timeScale = 1f;
        if (pauseMenu) pauseMenu.SetActive(false);
        OnResumed?.Invoke();
    }

    public void LevelComplete()
    {
        if (CurrentState != State.Playing) return;
        CurrentState = State.LevelComplete;
        if (levelCompletePanel) levelCompletePanel.SetActive(true);
        OnLevelComplete?.Invoke();
    }

    public void GameOver()
    {
        if (CurrentState != State.Playing) return;
        CurrentState = State.GameOver;
        if (gameOverPanel) gameOverPanel.SetActive(true);
        OnGameOver?.Invoke();
    }

    // Convenience wrappers for UI buttons
    public void RestartLevel() => SceneLoader.Instance?.ReloadCurrent();
    public void NextLevel()    => SceneLoader.Instance?.LoadNext();
    public void Quit()         => SceneLoader.Instance?.QuitGame();
}