using UnityEngine;
using UnityEngine.UI;

public class TextWithCountUpInt : Text {

	private int value;
    private Coroutine coroutine;

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

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

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