using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace uTools
{
    public class uTweenTimer : uTweenValue
    {
        private Text m_text;

        public Text Text
        {
            get
            {
                if (null == m_text)
                {
                    m_text = GetComponent<Text>();
                }
                return m_text;
            }
        }

        [SerializeField]
        string format;

        [SerializeField]
        int digitSize;

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

        const string size1 = ":<size=";

        const string size2 = ">";

        const string size3 = "</size>";

        const string zero = "0";

        protected override void ValueUpdate(float value, bool isFinished)
        {
            string[] result = value.ToString(format).Split('.');
            sb.Length = 0;
            if (value < 10)
            {
                sb.Append(zero);
            }
            sb.Append(result[0]);
            sb.Append(size1);
            sb.Append(digitSize);
            sb.Append(size2);
            sb.Append(result[1]);
            sb.Append(size3);            
            Text.text = sb.ToString();
        }

        public void SetValue(float value)
        {
            this.value = value;
            ValueUpdate(value, false);
        }
    }
}
