using UnityEngine;

public class UITweenAlpha : MonoBehaviour
{
	public delegate void FinishCallBack(UITweenAlpha in_FadeObject);

	public AnimationCurve _curve;

	public bool _autoStart;

	public float _from;

	public float _to;

	public float _delayTime;

	public float _endTime;

	public bool _loop;

	public bool PingPong;

	public UITweenAlpha[] LinkTargets;

	private UIPanel _panel;

	private UIWidget _uiWidget;

	private UIRect _rect;

	private bool _isPlay;

	private bool _isEnd;

	private bool _isPlayFoward;

	private bool _initialized;

	private float _value;

	public FinishCallBack _finishCallBack { get; set; }

	public float Timer { get; set; }

	private void Awake()
	{
		if (!_initialized)
		{
			Initialize();
		}
	}

	private void Initialize()
	{
		_initialized = true;
		_panel = base.gameObject.GetComponent<UIPanel>();
		if (_panel != null)
		{
			_rect = _panel;
		}
		else
		{
			_uiWidget = base.gameObject.GetComponent<UIWidget>();
			if (_uiWidget == null && _panel == null)
			{
				_uiWidget = base.gameObject.AddComponent<UIWidget>();
			}
			_rect = _uiWidget;
		}
		if (_autoStart)
		{
			PlayForward(isReset: true);
		}
		if (LinkTargets == null)
		{
			return;
		}
		for (int i = 0; i < LinkTargets.Length; i++)
		{
			if (LinkTargets[i].isActiveAndEnabled)
			{
				Timer = LinkTargets[i].Timer;
				break;
			}
		}
	}

	private void Update()
	{
		if (_isPlay)
		{
			Timer += Time.deltaTime;
			if (Timer >= _delayTime)
			{
				float b = (Timer - _delayTime) / (_curve.keys[_curve.length - 1].time * _endTime);
				b = Mathf.Min(_curve.keys[_curve.length - 1].time, b);
				if (_isPlayFoward)
				{
					_value = _from + (_to - _from) * _curve.Evaluate(b);
				}
				else
				{
					_value = _from + (_to - _from) * _curve.Evaluate(_curve.keys[_curve.length - 1].time - b);
				}
				_rect.alpha = _value;
				if (b >= _curve.keys[_curve.length - 1].time)
				{
					if (_isPlayFoward)
					{
						_rect.alpha = _to;
					}
					else
					{
						_rect.alpha = _from;
					}
					if (PingPong)
					{
						Timer = 0f;
						if (_isPlayFoward)
						{
							_isPlayFoward = false;
						}
						else
						{
							_isPlayFoward = true;
						}
					}
					else if (!_loop)
					{
						_isPlay = false;
						_isEnd = true;
					}
					else
					{
						Timer = 0f;
					}
				}
			}
		}
		if (_isEnd)
		{
			if (_finishCallBack != null)
			{
				_finishCallBack(this);
			}
			_isEnd = false;
		}
	}

	public void Cancel(bool isFrom = false, bool isTo = false)
	{
		_isPlay = false;
		if (isFrom)
		{
			_rect.alpha = _from;
		}
		else if (isTo)
		{
			_rect.alpha = _to;
		}
	}

	public void PlayForward(bool isReset = false)
	{
		if (!base.gameObject.activeSelf)
		{
			return;
		}
		if (!_initialized)
		{
			Initialize();
		}
		if (_curve == null)
		{
			return;
		}
		_isPlayFoward = true;
		if (isReset)
		{
			_rect.alpha = _from;
		}
		if (!_loop)
		{
			if (_rect.alpha != _to || isReset)
			{
				_isPlay = true;
				Timer = 0f;
			}
			else
			{
				_isEnd = true;
			}
		}
		else
		{
			_isPlay = true;
			Timer = 0f;
		}
	}

	public void PlayReverse(bool isReset = false)
	{
		if (!base.gameObject.activeSelf || _curve == null)
		{
			return;
		}
		_isPlayFoward = false;
		if (isReset)
		{
			_rect.alpha = _to;
		}
		if (!_loop)
		{
			if (_rect.alpha != _from || isReset)
			{
				_isPlay = true;
				Timer = 0f;
			}
			else
			{
				_isEnd = true;
			}
		}
		else
		{
			_isPlay = true;
			Timer = 0f;
		}
	}

	public void End()
	{
		if (_isPlay)
		{
			_isPlay = false;
			if (_finishCallBack != null)
			{
				_finishCallBack(this);
			}
		}
	}
}
