using System.Collections;
using TMPro;
using UnityEngine;

namespace LiveOpsExamples
{
  /// <summary>
  /// World-space "+N" score popup. Floats up ~0.5 world units and fades out over
  /// 0.7 s (Cubic.easeOut, like the Phaser floatScore tween), then destroys itself.
  /// </summary>
  public class ScorePopupView : MonoBehaviour
  {
    private const float FloatDistance = 0.5f;
    private const float LifetimeSeconds = 0.7f;

    [SerializeField] private TextMeshPro label;

    public void Set(int gain)
    {
      label.text = "+" + gain;
      StartCoroutine(Animate());
    }

    private IEnumerator Animate()
    {
      var start = transform.position;
      var color = label.color;
      var elapsed = 0f;

      while (elapsed < LifetimeSeconds)
      {
        elapsed += Time.deltaTime;
        var t = Mathf.Clamp01(elapsed / LifetimeSeconds);
        var eased = 1f - Mathf.Pow(1f - t, 3f);
        transform.position = start + new Vector3(0f, FloatDistance * eased, 0f);
        label.color = new Color(color.r, color.g, color.b, color.a * (1f - eased));
        yield return null;
      }

      Destroy(gameObject);
    }
  }
}
