using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

[RequireComponent(typeof(CanvasGroup))]
public class UICanvasFade : MonoBehaviour
{
	private GameObject objCanvas
	{
		get
		{
			if (!this.m_ObjCanvas)
			{
				this.m_ObjCanvas = base.gameObject;
			}
			return this.m_ObjCanvas;
		}
	}

	private CanvasGroup canvasGroup
	{
		get
		{
			if (!this.m_CanvasGroup)
			{
				this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
			}
			return this.m_CanvasGroup;
		}
	}

	public float fadeTime
	{
		get
		{
			return this.m_FadeTime;
		}
		set
		{
			this.m_FadeTime = Mathf.Clamp(value, 0f, 1f);
		}
	}

	public bool isTimeScaling
	{
		get
		{
			return this.m_IsTimeScaling;
		}
		set
		{
			this.m_IsTimeScaling = value;
		}
	}

	public void FadeIn(float fadeTime, bool isTimeScaling, UnityAction callback = null)
	{
		base.StopAllCoroutines();
		this.objCanvas.SetActive(true);
		this.canvasGroup.interactable = false;
		base.StartCoroutine(this.Coroutine_Fade(true, fadeTime, isTimeScaling, callback));
	}

	public void FadeIn()
	{
		this.FadeIn(this.m_FadeTime, this.m_IsTimeScaling, null);
	}

	public void FadeOut(float fadeTime, bool isTimeScaling, UnityAction callback = null)
	{
		if (!this.objCanvas.activeInHierarchy)
		{
			if (callback != null)
			{
				callback();
			}
			return;
		}
		base.StopAllCoroutines();
		this.canvasGroup.interactable = false;
		base.StartCoroutine(this.Coroutine_Fade(false, fadeTime, isTimeScaling, callback));
	}

	public void FadeOut()
	{
		this.FadeOut(this.m_FadeTime, this.m_IsTimeScaling, null);
	}

	private IEnumerator Coroutine_Fade(bool IsFadeIn, float fadeTime, bool isTimeScaling, UnityAction callback)
	{
		if (fadeTime > Mathf.Epsilon)
		{
			float speed = 1f / fadeTime;
			if (isTimeScaling)
			{
				speed *= Time.deltaTime;
			}
			else
			{
				speed *= Time.unscaledDeltaTime;
			}
			if (!IsFadeIn)
			{
				speed *= -1f;
			}
			do
			{
				this.canvasGroup.alpha += speed;
				yield return null;
			}
			while (this.canvasGroup.alpha > 0f && this.canvasGroup.alpha < 1f);
		}
		if (IsFadeIn)
		{
			this.objCanvas.SetActive(true);
			this.canvasGroup.interactable = true;
			this.canvasGroup.alpha = 1f;
		}
		else
		{
			this.objCanvas.SetActive(false);
			this.canvasGroup.interactable = false;
			this.canvasGroup.alpha = 0f;
		}
		if (callback != null)
		{
			callback();
		}
		yield break;
	}

	[SerializeField]
	private GameObject m_ObjCanvas;

	[SerializeField]
	private CanvasGroup m_CanvasGroup;

	[SerializeField]
	[Range(0f, 1f)]
	private float m_FadeTime = 0.5f;

	[SerializeField]
	private bool m_IsTimeScaling;
}
