using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class uGUICanvas : MonoBehaviour
{
	private RectTransform CanvasTransform
	{
		get
		{
			if (this.m_CanvasTransform == null)
			{
				this.m_CanvasTransform = this.m_Canvas.GetComponent<RectTransform>();
			}
			return this.m_CanvasTransform;
		}
	}

	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;
		}
	}

	private int CurrentScreenWidth
	{
		get
		{
			return Screen.width;
		}
	}

	private int CurrentScreenHeight
	{
		get
		{
			return Screen.height;
		}
	}

	private UIRoot uiRoot
	{
		get
		{
			if (this.m_UIRoot == null)
			{
				string text = GameMain.Instance.GetNowSceneName();
				if (string.IsNullOrEmpty(text))
				{
					text = SceneManager.GetActiveScene().name;
				}
				Scene sceneByName = SceneManager.GetSceneByName(text);
				if (!sceneByName.IsValid())
				{
					Debug.LogWarning(string.Format("シーン「{0}」は有効ではありません", text));
					return this.m_UIRoot;
				}
				if (!sceneByName.isLoaded)
				{
					Debug.Log("シーンロード中");
					return this.m_UIRoot;
				}
				GameObject[] rootGameObjects = sceneByName.GetRootGameObjects();
				GameObject gameObject = null;
				foreach (GameObject gameObject2 in rootGameObjects)
				{
					if (!(gameObject2.name != "UI Root"))
					{
						gameObject = gameObject2;
						break;
					}
				}
				if (gameObject == null)
				{
					return this.m_UIRoot;
				}
				UIRoot component = gameObject.GetComponent<UIRoot>();
				if (component == null)
				{
					Debug.LogWarning(string.Format("オブジェクト「{0}」にUIRootコンポーネントがありません", gameObject.name));
					return null;
				}
				this.m_UIRoot = component;
			}
			return this.m_UIRoot;
		}
	}

	private UIPanel uiPanel
	{
		get
		{
			if (this.m_UIPanel == null)
			{
				this.m_UIPanel = base.transform.GetComponentInParent<UIPanel>();
			}
			return this.m_UIPanel;
		}
	}

	private UICamera NGUI_UICamera
	{
		get
		{
			if (this.m_NGUI_UICamera == null && this.uiRoot)
			{
				this.m_NGUI_UICamera = this.uiRoot.GetComponentInChildren<UICamera>();
			}
			return this.m_NGUI_UICamera;
		}
	}

	private Camera SystemUICamera
	{
		get
		{
			if (this.m_SystemUICamera == null)
			{
				this.m_SystemUICamera = this.m_SystemUI_UICamera.GetComponent<Camera>();
			}
			return this.m_SystemUICamera;
		}
	}

	public bool IsHover
	{
		get
		{
			return this.m_IsRaycastHit;
		}
	}

	public int sortOrder
	{
		get
		{
			return this.m_SortOrder;
		}
		set
		{
			if (this.m_SortOrder != value || this.m_Canvas.sortingOrder != value)
			{
				this.m_Canvas.sortingOrder = value;
				this.m_SortOrder = value;
			}
		}
	}

	private void Start()
	{
		this.OnReset();
	}

	private void Update()
	{
		if (!this.canvasGroup.ignoreParentGroups && this.uiPanel != null && this.canvasGroup.alpha != this.uiPanel.alpha)
		{
			this.canvasGroup.alpha = this.uiPanel.alpha;
		}
		if (GameMain.Instance.MainCamera.IsFadeOut() || this.m_SystemUI_UICamera.Hover)
		{
			if (uGUIUtility.GetEventSystem() == null)
			{
				Debug.LogFormat("[uGUICanvas]EventSystem が見つからなかったので、「{0}」の子オブジェクトにEventSystemを作成します。", new object[]
				{
					base.name
				});
				GameObject gameObject = new GameObject();
				gameObject.AddComponent<EventSystem>();
				gameObject.AddComponent<StandaloneInputModule2>();
				gameObject.transform.SetParent(base.gameObject.transform, false);
				gameObject.name = "EventSystem";
			}
			uGUIUtility.SetActiveEventSystem(false);
		}
		else
		{
			if (uGUIUtility.GetEventSystem() == null)
			{
				Debug.LogFormat("[uGUICanvas]EventSystem が見つからなかったので、「{0}」の子オブジェクトにEventSystemを作成します。", new object[]
				{
					base.name
				});
				GameObject gameObject2 = new GameObject();
				gameObject2.AddComponent<EventSystem>();
				gameObject2.AddComponent<StandaloneInputModule2>();
				gameObject2.transform.SetParent(base.gameObject.transform, false);
				gameObject2.name = "EventSystem";
			}
			uGUIUtility.SetActiveEventSystem(true);
		}
		this.m_IsRaycastHit = this.IsPointerHit();
		if (this.NGUI_UICamera)
		{
			this.NGUI_UICamera.EnableProcess = (!this.m_SystemUI_UICamera.Hover && !this.m_IsRaycastHit);
		}
	}

	private bool IsPointerHit()
	{
		EventSystem eventSystem = uGUIUtility.GetEventSystem();
		if (eventSystem == null)
		{
			return false;
		}
		Vector2 position = Input.mousePosition;
		PointerEventData pointerEventData = new PointerEventData(eventSystem);
		pointerEventData.position = position;
		List<RaycastResult> list = new List<RaycastResult>();
		this.m_GraphicRaycaster.Raycast(pointerEventData, list);
		return list.Count > 0;
	}

	private void OnReset()
	{
		this.m_Canvas = base.GetComponent<Canvas>();
		if (!this.m_Canvas)
		{
			Debug.LogErrorFormat("[uGUICanvas]「{0}」には Canvas コンポーネントが存在しません", new object[]
			{
				base.name
			});
		}
		this.m_GraphicRaycaster = base.GetComponent<GraphicRaycaster>();
		if (!this.m_GraphicRaycaster)
		{
			Debug.LogErrorFormat("[uGUICanvas]「{0}」には GraphicRaycaster コンポーネントが存在しません", new object[]
			{
				base.name
			});
		}
		this.m_SystemUI_UICamera = GameMain.Instance.SysShortcut.GetComponentInParent<UIRoot>().GetComponentInChildren<UICamera>();
		if (this.m_SystemUI_UICamera == null)
		{
			Debug.LogError("システムUIルートが見つかりませんでした");
		}
		UIRoot componentInParent = this.m_Canvas.GetComponentInParent<UIRoot>();
		if (componentInParent == null)
		{
			NDebug.Warning(string.Format("{0}の親にUIRootがありません", base.name));
		}
		else
		{
			UICamera componentInChildren = componentInParent.GetComponentInChildren<UICamera>();
			if (componentInChildren == null)
			{
				NDebug.Warning(string.Format("{0}の子オブジェクトにUICameraがありません", componentInParent.name));
			}
			else
			{
				this.m_Canvas.renderMode = RenderMode.WorldSpace;
				this.m_Canvas.worldCamera = componentInChildren.GetComponent<Camera>();
			}
		}
		this.m_Canvas.planeDistance = 5f;
		uGUIUtility.SetLayer(base.gameObject, LayerMask.NameToLayer("NGUI"), true);
	}

	private void OnApplicationQuit()
	{
		this.m_IsQuittingApplication = true;
	}

	private void OnDestroy()
	{
		if (this.m_IsQuittingApplication)
		{
			return;
		}
		if (this.m_NGUI_UICamera)
		{
			this.m_NGUI_UICamera.EnableProcess = true;
		}
	}

	private void OnDisable()
	{
		if (this.m_IsQuittingApplication)
		{
			return;
		}
		if (this.m_NGUI_UICamera)
		{
			this.m_NGUI_UICamera.EnableProcess = true;
		}
	}

	[SerializeField]
	private Canvas m_Canvas;

	private RectTransform m_CanvasTransform;

	private CanvasGroup m_CanvasGroup;

	private UIRoot m_UIRoot;

	private UIPanel m_UIPanel;

	private UICamera m_NGUI_UICamera;

	private UICamera m_SystemUI_UICamera;

	private Camera m_SystemUICamera;

	private GraphicRaycaster m_GraphicRaycaster;

	private bool m_IsRaycastHit;

	[SerializeField]
	[Tooltip("uGUIキャンバスの描画順")]
	private int m_SortOrder = -1;

	private bool m_IsQuittingApplication;
}
