using TMPro;
using UnityEngine;

// 플레이 중 실시간 점수/콤보 HUD. RhythmManager 이벤트만 구독하고 표시만 한다.
public class RhythmScoreHud : MonoBehaviour
{
    [SerializeField] private RhythmManager _manager;
    [SerializeField] private GameObject _root;     // HUD 루트 (곡 시작/종료에 켜고 끔)
    [SerializeField] private TMP_Text _scoreText;
    [SerializeField] private TMP_Text _comboText;
    [SerializeField] private TMP_Text _judgeText;  // Perfect/Good 등 최근 판정 표시(선택)

    private void Awake()
    {
        if (_root != null) _root.SetActive(false);   // 곡 시작 전엔 숨김
    }

    private void OnEnable()
    {
        if (_manager == null) return;
        _manager.OnSongStarted += HandleSongStarted;
        _manager.OnScoreChanged += HandleScoreChanged;
        _manager.OnJudged += HandleJudged;
        _manager.OnSongFinished += HandleSongFinished;
    }

    private void OnDisable()
    {
        if (_manager == null) return;
        _manager.OnSongStarted -= HandleSongStarted;
        _manager.OnScoreChanged -= HandleScoreChanged;
        _manager.OnJudged -= HandleJudged;
        _manager.OnSongFinished -= HandleSongFinished;
    }

    private void HandleSongStarted()
    {
        if (_root != null) _root.SetActive(true);    // 곡 시작 시 실시간 HUD 표시
    }

    private void HandleScoreChanged(RhythmScore score)
    {
        if (_scoreText != null) _scoreText.text = $"SCORE {score.Score:N0}";
        if (_comboText != null) _comboText.text = score.Combo > 0 ? $"{score.Combo}" : "";
    }

    private void HandleJudged(Result result)
    {
        if (_judgeText != null) _judgeText.text = result.ToString();
    }

    private void HandleSongFinished(RhythmScore score)
    {
        if (_root != null) _root.SetActive(false);   // 곡 끝나면 실시간 HUD는 숨김(결과창이 대신 뜸)
    }
}
