using System;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CanvasUI : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI scoreText;
    [SerializeField] GameObject winPanel;

    private void Start()
    {
        UpdateScoreText();
        GameManager.Instance.OnScoreChanged += OnChangeScore;
        GameManager.Instance.OnWin += OnWin;
    }

    private void Update()
    {
        if (GameManager.Instance.IsWinScore)
        {
            if (Input.GetKeyDown(KeyCode.Return))
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            }
        }
    }

    public void OnChangeScore()
    {
        UpdateScoreText();
    }

    public void OnWin()
    {
        winPanel.SetActive(true);
    }

    private void UpdateScoreText()
    {
        scoreText.text = $"{GameManager.Instance.Score.ToString()}/{GameManager.Instance.MaxScore}";
    }
}
