using System;
using System.Collections;
using UnityEngine;

public class uGUICanvasFade : MonoBehaviour
{
	private CanvasGroup canvasGroup
	{
		get
		{
			if (this.m_CanvasGroup == null)
			{
				this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
				if (this.m_CanvasGroup == null)
				{
					this.m_CanvasGroup = base.gameObject.AddComponent<CanvasGroup>();
				}
			}
			return this.m_CanvasGroup;
		}
	}

	public bool interactable
	{
		get
		{
			return this.canvasGroup.interactable;
		}
		set
		{
			this.canvasGroup.interactable = value;
		}
	}

	public bool blocksRaycasts
	{
		get
		{
			return this.canvasGroup.blocksRaycasts;
		}
		set
		{
			this.canvasGroup.blocksRaycasts = value;
		}
	}

	public float alpha
	{
		get
		{
			return this.canvasGroup.alpha;
		}
		set
		{
			this.canvasGroup.alpha = value;
		}
	}

	private float nowAlpha { get; set; }

	private void Awake()
	{
		this.nowAlpha = this.canvasGroup.alpha;
	}

	public void FadeIn(float time = 0.5f, Action callback = null)
	{
		this.nowAlpha = this.canvasGroup.alpha;
		if (!base.gameObject.activeSelf)
		{
			base.gameObject.SetActive(true);
		}
		if (1f <= this.canvasGroup.alpha)
		{
			if (callback != null)
			{
				callback();
			}
			return;
		}
		base.StopAllCoroutines();
		base.StartCoroutine(this.coroutine_Fade(time, true, callback));
	}

	public void FadeOut(float time = 0.5f, Action callback = null)
	{
		this.nowAlpha = this.canvasGroup.alpha;
		if (this.canvasGroup.alpha <= 0f)
		{
			if (callback != null)
			{
				callback();
			}
			return;
		}
		if (!base.gameObject.activeSelf)
		{
			base.gameObject.SetActive(true);
		}
		base.StopAllCoroutines();
		base.StartCoroutine(this.coroutine_Fade(time, false, callback));
	}

	private IEnumerator coroutine_Fade(float time, bool isFadeIn, Action callback)
	{
		float speed = 1f;
		if (0f < time)
		{
			speed /= time;
			if (isFadeIn)
			{
				while (this.nowAlpha < 1f)
				{
					this.nowAlpha += speed * Time.deltaTime;
					this.canvasGroup.alpha = this.nowAlpha;
					yield return null;
				}
			}
			else
			{
				while (0f < this.nowAlpha)
				{
					this.nowAlpha -= speed * Time.deltaTime;
					this.canvasGroup.alpha = this.nowAlpha;
					yield return null;
				}
			}
			if (callback != null)
			{
				callback();
			}
			yield break;
		}
		if (isFadeIn)
		{
			this.nowAlpha = 1f;
			this.canvasGroup.alpha = 1f;
		}
		else
		{
			this.nowAlpha = 0f;
			this.canvasGroup.alpha = 0f;
		}
		if (callback != null)
		{
			callback();
		}
		yield break;
	}

	private CanvasGroup m_CanvasGroup;
}
