using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject gameoverText;   // 게임오버 UI
    public Text timeText;             // 생존 시간 UI
    public Text recordText;           // 최고 기록 UI

    private float surviveTime;
    private bool isGameover;

    void Start()
    {
        surviveTime = 0f;
        isGameover = false;

        gameoverText.SetActive(false);

        float bestTime = PlayerPrefs.GetFloat("BestTime", 0f);
        recordText.text = "Best Time : " + (int)bestTime;

        Time.timeScale = 1f;
    }

    void Update()
    {
        if (!isGameover)
        {
            surviveTime += Time.deltaTime;
            timeText.text = "Time : " + (int)surviveTime;
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                Time.timeScale = 1f;
                SceneManager.LoadScene("SampleScene");
            }
        }
    }

    public void EndGame()
    {
        if (isGameover) return;

        isGameover = true;

        gameoverText.SetActive(true);

        float bestTime = PlayerPrefs.GetFloat("BestTime", 0f);
        if (surviveTime > bestTime)
        {
            bestTime = surviveTime;
            PlayerPrefs.SetFloat("BestTime", bestTime);
        }

        recordText.text = "Best Time : " + (int)bestTime;

        Time.timeScale = 0f;
    }

    public void Die()
    {
        EndGame();
        gameObject.SetActive(false);
    }
} // <-- 클래스 끝, 반드시 존재
