using UnityEngine;
using TMPro; 
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance; // Allows other scripts to find this one easily

    [Header("Game Settings")]
    public int totalOrbsToWin = 20;
    public float timeRemaining = 300f; // 5 minutes in seconds

    [Header("UI References")]
    public TextMeshProUGUI orbText;
    public TextMeshProUGUI timerText;

    private int currentOrbs = 0;
    private bool isGameOver = false;

    void Awake()
    {
        // Singleton pattern: ensures only one GameManager exists
        if (Instance == null) Instance = this;
        else Destroy(gameObject);
    }

    void Update()
    {
        if (isGameOver) return;

        // Handle Timer
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            UpdateTimerUI();
        }
        else
        {
            GameOver(false); // Timer ran out
        }
    }

    public void AddOrb()
    {
        currentOrbs++;
        UpdateOrbUI();

        if (currentOrbs >= totalOrbsToWin)
        {
            GameOver(true); // Collected all orbs
        }
    }

    void UpdateOrbUI()
    {
        orbText.text = "Orbs: " + currentOrbs + " / " + totalOrbsToWin;
    }

    void UpdateTimerUI()
    {
        float minutes = Mathf.FloorToInt(timeRemaining / 60);
        float seconds = Mathf.FloorToInt(timeRemaining % 60);
        timerText.text = string.Format("Time: {0:00}:{1:00}", minutes, seconds);
    }

    void GameOver(bool won)
    {
        isGameOver = true;
        if (won) Debug.Log("VICTORY! All orbs collected.");
        else Debug.Log("GAME OVER! Night has fallen.");

        // Later we will trigger Victory/Loss screens here
    }
}