using UnityEngine;
using UnityEngine.UI;

public class TextWithCountUpInfiniteFloat : Text {

    [SerializeField]
    private bool isFloat = true;
    [SerializeField]
    private bool isChineseNumerals = false;
    [SerializeField]
    private uint decimalCount = 1;
    [SerializeField]
    private int minUnitIndex = 3;

	private InfiniteFloat value;
    private Coroutine coroutine;

    public void ChangeValue(string format, InfiniteFloat value){
		this.SafeStopCoroutine(coroutine);
        this.value = value;
        if(isFloat){
            if(isChineseNumerals){
                text = string.Format(format, value.ToChineseNumerals());
            }else{
                text = string.Format(format, value);
            }
        }else{
            if(isChineseNumerals){
                text = string.Format(format, value.ToChineseNumeralsInt());
            }else{
                text = string.Format(format, value.ToStringInt(decimalCount, minUnitIndex));
            }
        }
    }

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

    private Coroutine _CountUpAnimation(string format, InfiniteFloat oldValue, InfiniteFloat toValue, float duration){
        if(isFloat){
            if(isChineseNumerals){
                if(gameObject.activeInHierarchy){
                    return this.CallLerpSmooth(duration, t => {
                        value = InfiniteFloat.Lerp(oldValue, toValue, t);
                        text = string.Format(format, value.ToChineseNumerals());
                    });
                }else{
                    text = string.Format(format, toValue.ToChineseNumerals());
                    return null;
                }
            }else{
                if(gameObject.activeInHierarchy){
                    return this.CallLerpSmooth(duration, t => {
                        value = InfiniteFloat.Lerp(oldValue, toValue, t);
                        text = string.Format(format, value);
                    });
                }else{
                    text = string.Format(format, toValue);
                    return null;
                }
            }
        }else{
            if(isChineseNumerals){
                if(gameObject.activeInHierarchy){
                    return this.CallLerpSmooth(duration, t => {
                        value = InfiniteFloat.Lerp(oldValue, toValue, t);
                        text = string.Format(format, value.ToChineseNumeralsInt());
                    });
                }else{
                    text = string.Format(format, toValue.ToChineseNumeralsInt());
                    return null;
                }
            }else{
                if(gameObject.activeInHierarchy){
                    return this.CallLerpSmooth(duration, t => {
                        value = InfiniteFloat.Lerp(oldValue, toValue, t);
                        text = string.Format(format, value.ToStringInt(decimalCount, minUnitIndex));
                    });
                }else{
                    text = string.Format(format, toValue.ToStringInt(decimalCount, minUnitIndex));
                    return null;
                }
            }
        }
    }
}
