using System;
using com.workman.cm3d2.scene.dailyEtc;
using UnityEngine;

public abstract class BasePanelMgr : MonoBehaviour
{
	public abstract void Init();

	public void SetDurationToFadeIn(float duration)
	{
		this.durationToFadeIn_ = duration;
	}

	public void SetDurationToFadeOut(float duration)
	{
		this.durationToFadeOut_ = duration;
	}

	protected virtual void SetFadeTargetPanel()
	{
	}

	protected virtual void OpenPanel()
	{
	}

	protected virtual void OpenPanel(string openType)
	{
	}

	protected virtual void ClosePanel()
	{
	}

	protected virtual void BeforeFadeOut()
	{
	}

	protected virtual void BeforeFadeIn()
	{
	}

	private float durationToFadeIn
	{
		get
		{
			return this.durationToFadeIn_;
		}
	}

	private float durationToFadeOut
	{
		get
		{
			return this.durationToFadeOut_;
		}
	}

	protected virtual void Start()
	{
		GameObject f_goParent = GameObject.Find("/UI Root");
		GameObject childObject = UTY.GetChildObject(f_goParent, "Manager", false);
		if (childObject != null)
		{
			this.sceneMgr = childObject.GetComponent<SceneMgr>();
		}
		this.SetDurationToFadeIn(0.25f);
		this.SetDurationToFadeOut(0.25f);
		this.ChangeFadeState(BasePanelMgr.FadeState.None);
	}

	private void ChangeFadeState(BasePanelMgr.FadeState fadeState)
	{
		this.currentFadeState = fadeState;
	}

	protected T GetManager<T>() where T : BasePanelMgr
	{
		return this.sceneMgr.GetManager<T>();
	}

	public void BaseInit()
	{
		this.Init();
		this.SetFadeTargetPanel();
	}

	public void BaseOpenPanel()
	{
		this.BeforeOpen();
		this.OpenPanel();
		this.FadeInPanel();
	}

	public void BaseOpenPanel(string openType)
	{
		this.BeforeOpen();
		this.OpenPanel(openType);
		this.FadeInPanel();
	}

	public void BaseClosePanel()
	{
		this.BeforeClose();
		this.ClosePanel();
		this.FadeOutPanel();
	}

	public void BaseClosePanel(Action OnFinished)
	{
		this.BeforeClose();
		this.ClosePanel();
		this.FadeOutPanel(OnFinished);
	}

	public void BaseClosePanel<T>(Action<T> OnFinished, T param)
	{
		this.BeforeClose();
		this.ClosePanel();
		this.FadeOutPanel<T>(OnFinished, param);
	}

	protected void FadeInPanel()
	{
		this.fadeTargetPanel.SetActive(true);
		this.BeforeFadeIn();
		this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeIn);
		TweenAlpha tweenAlpha = this.fadeTargetPanel.GetComponent<TweenAlpha>();
		if (tweenAlpha)
		{
			UnityEngine.Object.Destroy(tweenAlpha);
		}
		if (this.durationToFadeIn <= 0f)
		{
			UIRect uirect = this.fadeTargetPanel.GetComponent<UIRect>();
			SpriteRenderer component = this.fadeTargetPanel.GetComponent<SpriteRenderer>();
			Material material = null;
			if (uirect == null && component == null)
			{
				Renderer component2 = this.fadeTargetPanel.GetComponent<Renderer>();
				if (component2 != null)
				{
					material = component2.material;
				}
				if (material == null)
				{
					uirect = this.fadeTargetPanel.GetComponentInChildren<UIRect>();
				}
			}
			float num = 1f;
			if (uirect != null)
			{
				uirect.alpha = num;
			}
			else if (component != null)
			{
				Color color = component.color;
				color.a = num;
				component.color = color;
			}
			else if (material != null)
			{
				Color color2 = material.color;
				color2.a = num;
				material.color = color2;
			}
			this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeIn);
			this.AfterFadeIn();
			return;
		}
		tweenAlpha = this.fadeTargetPanel.AddComponent<TweenAlpha>();
		tweenAlpha.from = 0f;
		tweenAlpha.to = 1f;
		tweenAlpha.duration = this.durationToFadeIn;
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeIn);
			this.AfterFadeIn();
		});
		tweenAlpha.PlayForward();
	}

	protected void FadeOutPanel()
	{
		this.BeforeFadeOut();
		this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeOut);
		if (this.durationToFadeOut <= 0f)
		{
			UIRect uirect = this.fadeTargetPanel.GetComponent<UIRect>();
			SpriteRenderer component = this.fadeTargetPanel.GetComponent<SpriteRenderer>();
			Material material = null;
			if (uirect == null && component == null)
			{
				Renderer component2 = this.fadeTargetPanel.GetComponent<Renderer>();
				if (component2 != null)
				{
					material = component2.material;
				}
				if (material == null)
				{
					uirect = this.fadeTargetPanel.GetComponentInChildren<UIRect>();
				}
			}
			float num = 0f;
			if (uirect != null)
			{
				uirect.alpha = num;
			}
			else if (component != null)
			{
				Color color = component.color;
				color.a = num;
				component.color = color;
			}
			else if (material != null)
			{
				Color color2 = material.color;
				color2.a = num;
				material.color = color2;
			}
			this.fadeTargetPanel.SetActive(false);
			this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
			this.AfterFadeOut();
			return;
		}
		TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, this.durationToFadeOut, 0f);
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			this.fadeTargetPanel.SetActive(false);
			this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
			this.AfterFadeOut();
		});
	}

	protected void FadeOutPanel(Action OnFinished)
	{
		this.BeforeFadeOut();
		this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeOut);
		TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, this.durationToFadeOut, 0f);
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			this.fadeTargetPanel.SetActive(false);
			this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
			this.AfterFadeOut();
			OnFinished();
		});
	}

	protected void FadeOutPanel<T>(Action<T> OnFinished, T param)
	{
		this.BeforeFadeOut();
		this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeOut);
		TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, this.durationToFadeOut, 0f);
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			this.fadeTargetPanel.SetActive(false);
			this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
			this.AfterFadeOut();
			OnFinished(param);
		});
	}

	protected virtual void AfterFadeIn()
	{
		this.AfterOpen();
	}

	protected virtual void AfterFadeOut()
	{
		this.AfterClose();
	}

	protected virtual void BeforeOpen()
	{
	}

	protected virtual void AfterOpen()
	{
	}

	protected virtual void BeforeClose()
	{
	}

	protected virtual void AfterClose()
	{
	}

	protected GameObject fadeTargetPanel;

	protected SceneMgr sceneMgr;

	protected BasePanelMgr.FadeState currentFadeState;

	private float durationToFadeIn_;

	private float durationToFadeOut_;

	private const string uiRootPath = "/UI Root";

	private const float DURATION_TO_FADE = 0.25f;

	public enum FadeState
	{
		None,
		StartFadeIn,
		EndFadeIn,
		StartFadeOut,
		EndFadeOut
	}
}
