using System;
using UnityEngine;

namespace Kasizuki
{
	public class NGUIWindow : MonoBehaviour
	{
		protected virtual void Awake()
		{
			if (this.m_CachedObjectDic == null)
			{
				this.m_CachedObjectDic = new ObjectCacheDic();
			}
		}

		public void Open(float time = 0.25f, Action callback = null)
		{
			if (!base.gameObject.activeSelf)
			{
				base.gameObject.SetActive(true);
			}
			if (this.OnOpen != null)
			{
				this.OnOpen();
			}
			this.SetRaycastBlockerActive(true);
			this.tweenAlpha.SetOnFinished(delegate()
			{
				if (callback != null)
				{
					callback();
				}
				this.SetRaycastBlockerActive(false);
			});
			TweenAlpha.Begin(base.gameObject, time, 1f);
		}

		public void Close(float time = 0.25f, Action callback = null)
		{
			if (!base.gameObject.activeInHierarchy)
			{
				return;
			}
			callback = (Action)Delegate.Combine(callback, new Action(delegate()
			{
				this.gameObject.SetActive(false);
				if (this.OnClose != null)
				{
					this.OnClose();
				}
			}));
			this.SetRaycastBlockerActive(true);
			this.tweenAlpha.SetOnFinished(delegate()
			{
				if (callback != null)
				{
					callback();
				}
				this.SetRaycastBlockerActive(false);
			});
			TweenAlpha.Begin(base.gameObject, time, 0f);
		}

		public Action OnOpen { get; set; }

		public Action OnClose { get; set; }

		private GameObject raycastBlocker
		{
			get
			{
				if (this.m_RaycastBlocker == null)
				{
					this.m_RaycastBlocker = this.CreateBlocker();
				}
				return this.m_RaycastBlocker;
			}
		}

		private GameObject CreateBlocker()
		{
			GameObject gameObject = null;
			UIRect component = base.gameObject.GetComponent<UIRect>();
			if (component != null)
			{
				gameObject = new GameObject("Ray Blocker");
				gameObject.transform.SetParent(base.transform, false);
				gameObject.layer = base.gameObject.layer;
				gameObject.AddComponent<BoxCollider>();
				UIWidget uiwidget = gameObject.AddComponent<UIWidget>();
				uiwidget.depth = 9999;
				uiwidget.updateAnchors = UIRect.AnchorUpdate.OnStart;
				uiwidget.SetRect(-2500f, -2500f, 5000f, 5000f);
				uiwidget.autoResizeBoxCollider = true;
				uiwidget.ResizeCollider();
			}
			else
			{
				Debug.Log("[NGUI Window] " + base.gameObject.name + "には UI Rect を継承したクラスがありません");
			}
			return gameObject;
		}

		private void SetRaycastBlockerActive(bool isActive)
		{
			if (this.raycastBlocker == null)
			{
				return;
			}
			if (this.raycastBlocker.activeSelf != isActive)
			{
				this.raycastBlocker.SetActive(isActive);
			}
		}

		public T AddCache<T>(string name, T Obj) where T : UnityEngine.Object
		{
			if (this.m_CachedObjectDic == null)
			{
				this.m_CachedObjectDic = new ObjectCacheDic();
			}
			this.m_CachedObjectDic.AddCache<T>(name, Obj);
			return Obj;
		}

		public T GetCache<T>(string name) where T : UnityEngine.Object
		{
			return this.m_CachedObjectDic.GetCache<T>(name);
		}

		public void RemoveCache(string name)
		{
			this.m_CachedObjectDic.RemoveCache(name);
		}

		public GameObject GetChildObject(string name)
		{
			return UTY.GetChildObject(base.gameObject, name, false);
		}

		public T GetChildObject<T>(string name) where T : UnityEngine.Object
		{
			if (this.m_CachedObjectDic == null)
			{
				this.m_CachedObjectDic = new ObjectCacheDic();
			}
			return this.m_CachedObjectDic.GetChildObject<T>(base.gameObject, name);
		}

		public T CacheChildObject<T>(string path, string name) where T : UnityEngine.Object
		{
			return this.AddCache<T>(name, this.GetChildObject<T>(path));
		}

		public ObjectCacheDic cachedObjectDic
		{
			get
			{
				return this.m_CachedObjectDic;
			}
		}

		private TweenAlpha tweenAlpha
		{
			get
			{
				if (this.m_TweenAlpha == null)
				{
					this.m_TweenAlpha = base.GetComponent<TweenAlpha>();
					if (this.m_TweenAlpha == null)
					{
						this.m_TweenAlpha = base.gameObject.AddComponent<TweenAlpha>();
						if (this.m_TweenAlpha == null)
						{
							Debug.LogErrorFormat("「{0}」には「{1}」をアタッチ出来ません", new object[]
							{
								base.gameObject.name,
								typeof(TweenAlpha).Name
							});
						}
					}
				}
				return this.m_TweenAlpha;
			}
		}

		private GameObject m_RaycastBlocker;

		private TweenAlpha m_TweenAlpha;

		private ObjectCacheDic m_CachedObjectDic;
	}
}
