using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;

namespace uTools
{
    public class uTweenCountDown : uTweenValue
    {
        public int start;

        public int end;

        public UnityEvent onValueChange = new UnityEvent();

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

        int previousValue;

        protected override void Awake()
        {
            base.Awake();
            previousValue = int.MinValue;
        }

        protected override void ValueUpdate(float value, bool isFinished)
        {            
            int int_value = Mathf.FloorToInt(value);
            if (int_value > start) int_value = start;
            if (int_value < end) int_value = end;           
            cacheText.text = int_value.ToString();
            if (int_value != previousValue)
            {
                onValueChange.Invoke();
                previousValue = int_value;
            }
        }
    }
}
