using System;
using System.Collections;
using UnityEngine;

public class Fade : MonoBehaviour
{
	private void Start()
	{
		this.Init();
		this.fade.Range = this.cutoutRange;
	}

	private void Init()
	{
		this.fade = base.GetComponent<IFade>();
	}

	private void OnValidate()
	{
		this.Init();
		this.fade.Range = this.cutoutRange;
	}

	private IEnumerator FadeoutCoroutine(float time, Action action)
	{
		float endTime = Time.timeSinceLevelLoad + time * this.cutoutRange;
		WaitForEndOfFrame endFrame = new WaitForEndOfFrame();
		while (Time.timeSinceLevelLoad <= endTime)
		{
			this.cutoutRange = (endTime - Time.timeSinceLevelLoad) / time;
			this.fade.Range = this.cutoutRange;
			yield return endFrame;
		}
		this.cutoutRange = 0f;
		this.fade.Range = this.cutoutRange;
		if (action != null)
		{
			action();
		}
		yield break;
	}

	private IEnumerator FadeinCoroutine(float time, Action action)
	{
		float endTime = Time.timeSinceLevelLoad + time * (1f - this.cutoutRange);
		WaitForEndOfFrame endFrame = new WaitForEndOfFrame();
		while (Time.timeSinceLevelLoad <= endTime)
		{
			this.cutoutRange = 1f - (endTime - Time.timeSinceLevelLoad) / time;
			this.fade.Range = this.cutoutRange;
			yield return endFrame;
		}
		this.cutoutRange = 1f;
		this.fade.Range = this.cutoutRange;
		if (action != null)
		{
			action();
		}
		yield break;
	}

	public Coroutine FadeOut(float time, Action action)
	{
		base.StopAllCoroutines();
		return base.StartCoroutine(this.FadeoutCoroutine(time, action));
	}

	public Coroutine FadeOut(float time)
	{
		return this.FadeOut(time, null);
	}

	public Coroutine FadeIn(float time, Action action)
	{
		base.StopAllCoroutines();
		return base.StartCoroutine(this.FadeinCoroutine(time, action));
	}

	public Coroutine FadeIn(float time)
	{
		return this.FadeIn(time, null);
	}

	private IFade fade;

	private float cutoutRange;
}
