using UnityEngine;
using UnityEngine.UI;

public class TextWithCountUpFloat : Text {

	private float value;
    private Coroutine coroutine;

    public void ChangeValue(string format, float value){
		this.SafeStopCoroutine(coroutine);
        this.value = value;
        text = string.Format(format, value);
    }

    public void CountUpAnimation(string format, float toValue, float duration){
		this.SafeStopCoroutine(coroutine);
        coroutine = _CountUpAnimation(format, value, toValue, duration);
    }

    private Coroutine _CountUpAnimation(string format, float oldValue, float toValue, float duration){
        if(gameObject.activeInHierarchy){
            return this.CallLerpSmooth(duration, lerp => {
                value = Mathf.Lerp(oldValue, toValue, lerp);
                text = string.Format(format, value);
            });
        }else{
            text = string.Format(format, toValue);
            return null;
        }
    }
}
