using UnityEngine;
using UnityEngine.UI;

namespace uTools
{
    [AddComponentMenu("uTools/Tween/Tween Text(uTools)")]

    public class uTweenText : uTweenValue
    {
        private Text mText;
        public Text cacheText
        {
            get
            {
                if (mText == null)
                {
                    mText = GetComponent<Text>();
                }
                return mText;
            }
        }

        System.Text.StringBuilder sb = new System.Text.StringBuilder(256);

        /// <summary>
        /// number after the digit point
        /// </summary>
        public int digits;

        protected override void ValueUpdate(float value, bool isFinished)
        {
            sb.Length = 0;
            sb.Append(System.Math.Round(value, digits));
            cacheText.text = sb.ToString();
        }

        public static uTweenText Begin(Text label, float duration, float delay, float from, float to)
        {
            uTweenText comp = Begin<uTweenText>(label.gameObject, duration);
            comp.from = from;
            comp.to = to;
            comp.delay = delay;

            if (duration <= 0)
            {
                comp.Sample(1, true);
                comp.enabled = false;
            }
            return comp;
        }
    }
}