using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public enum GameState
    {
        InMainMenu,
        Paused,
        Playing,
        GameOver,
        Won
    }

    public static GameManager gameManagerInstance { get; private set; }

    [SerializeField] int currentOrbCounter = 0;
    [SerializeField] int maximumOrbs = 10;

    [SerializeField] TextMeshProUGUI orbCounterText;

    public GameState gameState = GameState.InMainMenu;

    private void Awake()
    {
        if(gameManagerInstance == null)
        {
            gameManagerInstance = this;

            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
            return;
        }
    }

    private void Update()
    {
        switch (gameState)
        {
            case GameState.InMainMenu: break;
            case GameState.Playing:
                if(orbCounterText == null)
                {
                    orbCounterText = GameObject.Find("Orb_Text").GetComponent<TextMeshProUGUI>();
                }
                break;
            case GameState.GameOver:
                SceneManager.LoadScene("GameOverScene");
                break;
            default: 
                Debug.Log("GameState error");
                break;
        }
    }

    public void IncreaseOrbCounter()
    {
        currentOrbCounter++;
        string orbCounterString = currentOrbCounter.ToString() + " / " + maximumOrbs.ToString();
        orbCounterText.text = orbCounterString;
        checkAllOrbsCollected(currentOrbCounter);
    }

    private void checkAllOrbsCollected(int currentOrbCounter)
    {
        if(currentOrbCounter == maximumOrbs)
        {
            gameState = GameState.Won;
            Debug.Log("Player won! All orbs were collected!");

            SceneManager.LoadScene("VictoryScene");
        }
    }

    public void StartGame()
    {
        SceneManager.LoadScene("Sandbox");
        gameState = GameState.Playing;
    }
}
