using UnityEngine;
using UnityEngine.UI;

public class TextWithCountUp : Text {

	private Coroutine coroutine;
	
    public void CountUpAnimation(string format, int oldCount, int count, float duration){
		if(gameObject.activeInHierarchy){
			this.SafeStopCoroutine(coroutine);
			coroutine = _CountUpAnimation(format, oldCount, count, duration);
		}else{
		    text = string.Format(format, count);
		}
	}

    private Coroutine _CountUpAnimation(string format, int oldCount, int count, float duration){
        return this.CallLerpSmooth(duration, lerp => {
			text = string.Format(format, (int)Mathf.Lerp(oldCount, count, lerp));
        }, () => {
		    text = string.Format(format, count);
        });
    }

    public void CountUpAnimation(string format, float oldCount, float count, float duration){
		if(gameObject.activeInHierarchy){
			this.SafeStopCoroutine(coroutine);
			coroutine = _CountUpAnimation(format, oldCount, count, duration);
		}else{
		    text = string.Format(format, count);
		}
	}
    private Coroutine _CountUpAnimation(string format, float oldCount, float count, float duration){
        return this.CallLerpSmooth(duration, lerp => {
			text = string.Format(format, Mathf.Lerp(oldCount, count, lerp));
        }, () => {
		    text = string.Format(format, count);
        });
    }
}
