using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class BeatHudUI : MonoBehaviour
{
    [Header("Refs")]
    [SerializeField] private BeatClock beatClock;

    [SerializeField] private RectTransform beatDot; // UI element to pulse
    [SerializeField] private Image beatDotImage;

    [SerializeField] private TMP_Text beatText;
    [SerializeField] private TMP_Text barText;
    [SerializeField] private TMP_Text debugText; // optional

    [Header("Pulse")]
    [SerializeField] private float dotBaseScale = 1.0f;
    [SerializeField] private float dotPulseScale = 1.6f;
    [SerializeField] private float pulseDuration = 0.10f; // seconds (visual), not beat duration

    [Header("Controls")]
    [SerializeField] private KeyCode testKey = KeyCode.Space; // press to show timing error

    private long lastBeatIndex = -1;
    private float pulseT = 1f;

    private void Reset()
    {
        // Try to auto-fill image if present on same object
        if (beatDot != null && beatDotImage == null)
            beatDotImage = beatDot.GetComponent<Image>();
    }

    private void Update()
    {
        if (beatClock == null || !beatClock.IsPlaying)
        {
            SetDotScale(dotBaseScale);
            if (beatText) beatText.text = "Beat: -";
            if (barText) barText.text = "Bar: -";
            if (debugText) debugText.text = "";
            return;
        }

        // --- Beat / Bar labels ---
        long beatIndex = beatClock.GetCurrentBeatIndex();
        int beatInBar = beatClock.GetBeatInBar(beatIndex);
        long barIndex = beatClock.GetBarIndex(beatIndex);

        if (beatText) beatText.text = $"Beat: {beatInBar}/{beatClock.BeatsPerBar}";
        if (barText) barText.text = $"Bar: {barIndex + 1}"; // show as 1-based

        // --- Pulse trigger: when beat index changes ---
        if (beatIndex != lastBeatIndex)
        {
            lastBeatIndex = beatIndex;
            pulseT = 0f;

            // Optional: accent downbeat
            if (beatDotImage != null)
            {
                // Don’t change colors if you don’t want; just change alpha a bit.
                // Downbeat = beatInBar == 1
                /*
                var c = beatDotImage.color;
                c.a = (beatInBar == 1) ? 1.0f : 0.75f;
                beatDotImage.color = c;
                */
            }
        }

        // --- Animate pulse ---
        if (pulseT < 1f)
        {
            pulseT += Time.unscaledDeltaTime / Mathf.Max(0.001f, pulseDuration);
            float k = 1f - Mathf.Clamp01(pulseT);

            // Ease-out feel
            float eased = 1f - (1f - k) * (1f - k);

            float scale = Mathf.Lerp(dotBaseScale, dotPulseScale, eased);
            SetDotScale(scale);
        }
        else
        {
            SetDotScale(dotBaseScale);
        }

        // --- Optional: timing error debug when pressing a key ---
        if (debugText != null)
        {
            if (Input.GetKeyDown(testKey))
            {
                var result = beatClock.JudgeNow(out double signedErrorSec);
                double ms = signedErrorSec * 1000.0;
                debugText.text = $"{result} | Err: {ms:+0;-0;0} ms";
            }
        }
    }

    private void SetDotScale(float s)
    {
        if (beatDot != null)
            beatDot.localScale = new Vector3(s, s, 1f);
    }
}

