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

public class VRCanvasManager : MonoBehaviour
{
	public static VRCanvasManager Instance
	{
		get
		{
			if (VRCanvasManager.m_Instance == null)
			{
				VRCanvasManager.m_Instance = UnityEngine.Object.FindObjectOfType<VRCanvasManager>();
			}
			return VRCanvasManager.m_Instance;
		}
	}

	public UICanvasFade CanvasParent
	{
		get
		{
			return this.m_CanvasParent;
		}
	}

	public bool IsYotogiSelecting
	{
		get
		{
			return this.m_IsYotogiSelecting;
		}
		set
		{
			this.m_IsYotogiSelecting = value;
		}
	}

	private void Awake()
	{
		VRCanvasManager.m_Instance = this;
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.MainMenu, this.m_CanvasMainMenu);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.Manual, this.m_CanvasManual);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.NeiLetter, this.m_CanvasNeiLetter);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.Dialog, this.m_CanvasDialog);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.VRConfig, this.m_CanvasVRConfig);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.PhotoMode, this.m_CanvasPhotoMode);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.MoviePlayer, this.m_CanvasMoviePlayer);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.MusicPlayer, this.m_CanvasMusicPlayer);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.Selector, this.m_CanvasSelector);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.Culture, this.m_CanvasCulture);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.Trophy, this.m_CanvasTrophy);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.VIP, this.m_CanvasVIP);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.ChangeBG, this.m_CanvasChangeBG);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.RentalMaid, this.m_CanvasRentalMaid);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.Tutorial, this.m_CanvasTutorial);
		this.m_CanvasArray.Add(VRCanvasManager.VRCanvasType.DecideUseTouch, this.m_CanvasDecideUseTouch);
	}

	private void Start()
	{
		Camera componentInChildren = EventSystem.current.gameObject.GetComponentInChildren<Camera>();
		foreach (Canvas canvas in this.m_CanvasArray.Values)
		{
			canvas.worldCamera = componentInChildren;
		}
	}

	private void Update()
	{
		CanvasGroup component = this.m_CanvasParent.GetComponent<CanvasGroup>();
		if (!GameMain.Instance.OvrMgr.OvrCamera.IsUIShow)
		{
			component.interactable = false;
			component.blocksRaycasts = false;
			component.alpha = 0f;
			return;
		}
		bool isFallThrough = GameMain.Instance.OvrMgr.OvrCamera.IsFallThrough;
		if (isFallThrough)
		{
			component.interactable = true;
			component.blocksRaycasts = true;
		}
		else
		{
			component.interactable = false;
			component.blocksRaycasts = false;
		}
		bool isRotDown = GameMain.Instance.OvrMgr.OvrCamera.OvrTablet.IsRotDown;
		bool isSideBack = GameMain.Instance.OvrMgr.OvrCamera.OvrTablet.IsSideBack;
		Vector3 zero = Vector3.zero;
		zero.z = ((!isRotDown) ? 0f : 180f);
		zero.y = ((!isSideBack) ? 0f : 180f);
		zero.x = 0f;
		this.m_CanvasParent.transform.localEulerAngles = zero;
	}

	private void LateUpdate()
	{
		bool isUIShow = GameMain.Instance.OvrMgr.OvrCamera.IsUIShow;
		bool isFreeMode = SceneVRCommunication.Instance.IsFreeMode;
		bool uiNoMoveMode = SceneVRCommunication.Instance.UiNoMoveMode;
		bool uiNoHomeMode = SceneVRCommunication.Instance.UiNoHomeMode;
		bool flag = GameMain.Instance.MainCamera.IsFadeProc();
		bool flag2 = GameMain.Instance.MainCamera.IsFadeOut();
		bool isEnableSkip = SceneVRCommunication.Instance.IsEnableSkip;
		bool uiNoTimeMode = SceneVRCommunication.Instance.UiNoTimeMode;
		bool beforeShowUI = this.m_BeforeShowUI;
		bool beforeFreeMode = this.m_BeforeFreeMode;
		bool beforeNoMoveMode = this.m_BeforeNoMoveMode;
		this.m_BeforeShowUI = isUIShow;
		this.m_BeforeFreeMode = isFreeMode;
		this.m_BeforeNoMoveMode = uiNoMoveMode;
		CanvasGroup component = this.m_CanvasParent.GetComponent<CanvasGroup>();
		if (isUIShow)
		{
			if (uiNoHomeMode)
			{
				component.blocksRaycasts = false;
				component.alpha = 0f;
			}
			else if (!this.m_IsYotogiSelecting)
			{
				component.alpha = 1f;
			}
			else
			{
				component.blocksRaycasts = false;
				component.alpha = 0f;
			}
		}
		this.SetMainMenuButtonEnable(VRMainMenu.ButtonType.Edit, isFreeMode);
		this.SetMainMenuButtonEnable(VRMainMenu.ButtonType.Yotogi, isFreeMode);
		this.SetMainMenuButtonEnable(VRMainMenu.ButtonType.Time, isFreeMode && !uiNoTimeMode);
		this.SetMainMenuButtonEnable(VRMainMenu.ButtonType.ChangeBG, !uiNoMoveMode && isFreeMode);
		this.SetMainMenuButtonEnable(VRMainMenu.ButtonType.Skip, !isFreeMode && isEnableSkip);
		this.SetMainMenuButtonActive(VRMainMenu.ButtonType.Skip, !isFreeMode && isEnableSkip);
		if (!isFreeMode && beforeFreeMode)
		{
			this.ClearCanvasStack();
			this.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
		}
		if (uiNoMoveMode && !beforeNoMoveMode)
		{
			this.ClearCanvasStack();
			this.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
		}
		bool flag3 = false;
		flag3 |= this.m_CanvasVRConfig.gameObject.activeInHierarchy;
		flag3 |= this.m_CanvasSelector.gameObject.activeInHierarchy;
		flag3 |= this.m_CanvasDialog.gameObject.activeInHierarchy;
		flag3 |= this.m_CanvasTutorial.gameObject.activeInHierarchy;
		flag3 |= this.m_CanvasDecideUseTouch.gameObject.activeInHierarchy;
		if (flag3 && isUIShow)
		{
			component.blocksRaycasts = true;
			component.alpha = 1f;
		}
		GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
		if (vrdeviceTypeID != GameMain.VRDeviceType.NON && vrdeviceTypeID != GameMain.VRDeviceType.RIFT && vrdeviceTypeID != GameMain.VRDeviceType.FOVE)
		{
			bool isHandCameraMode = GameMain.Instance.OvrMgr.OvrCamera.IsHandCameraMode;
			if (!this.m_IsYotogiSelecting)
			{
				if (isHandCameraMode && !this.m_BeforeHandCameraMode && isFreeMode && this.m_CanvasArray[VRCanvasManager.VRCanvasType.MainMenu].gameObject.activeInHierarchy)
				{
					this.OpenVRCanvas(VRCanvasManager.VRCanvasType.PhotoMode);
				}
				else if (this.m_CanvasArray[VRCanvasManager.VRCanvasType.PhotoMode].gameObject.activeInHierarchy && ((!isHandCameraMode && this.m_BeforeHandCameraMode) || (!isFreeMode && this.m_BeforeFreeMode)))
				{
					this.m_CanvasPhotoMode.GetComponent<UICanvasFade>().FadeOut();
					if (this.m_CanvasArray[VRCanvasManager.VRCanvasType.MainMenu].gameObject.activeSelf)
					{
						this.ClearCanvasStack();
					}
					else if (!this.IsExistsBeforeVRCanvas())
					{
						this.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
					}
					else
					{
						this.OpenBeforeVRCanvas();
					}
				}
			}
			this.m_BeforeHandCameraMode = isHandCameraMode;
		}
		if (flag || flag2)
		{
			component.blocksRaycasts = false;
		}
	}

	public void OpenVRCanvas()
	{
		this.m_CanvasParent.FadeIn();
		CanvasGroup component = this.m_CanvasParent.GetComponent<CanvasGroup>();
		component.alpha = 1f;
		component.interactable = true;
		component.blocksRaycasts = true;
	}

	public virtual void OpenVRCanvas(VRCanvasManager.VRCanvasType type)
	{
		if (!base.enabled)
		{
			return;
		}
		bool isUIShow = GameMain.Instance.OvrMgr.OvrCamera.IsUIShow;
		if (isUIShow)
		{
			this.m_CanvasParent.FadeIn();
		}
		GameObject mainCanvas = null;
		if (this.m_CanvasArray.ContainsKey(type))
		{
			mainCanvas = this.m_CanvasArray[type].gameObject;
		}
		this.ChangeCanvas(mainCanvas);
	}

	public void CloseVRCanvas()
	{
		foreach (KeyValuePair<VRCanvasManager.VRCanvasType, Canvas> keyValuePair in this.m_CanvasArray)
		{
			Canvas value = keyValuePair.Value;
			UICanvasFade component = value.GetComponent<UICanvasFade>();
			if (component)
			{
				value.gameObject.SetActive(false);
			}
		}
		CanvasGroup component2 = this.m_CanvasParent.GetComponent<CanvasGroup>();
		component2.alpha = 0f;
		component2.interactable = false;
	}

	public virtual void ChangeCanvas(GameObject MainCanvas)
	{
		if (!this.m_BeforeShowUI)
		{
			for (int i = 0; i < this.m_CanvasParent.transform.childCount; i++)
			{
				GameObject gameObject = this.m_CanvasParent.transform.GetChild(i).gameObject;
				UICanvasFade component = gameObject.GetComponent<UICanvasFade>();
				if (MainCanvas != gameObject)
				{
					if (component != null && gameObject.activeInHierarchy)
					{
						this.m_CanvasStack.Add(component);
					}
					if (component)
					{
						component.FadeOut(0f, false, null);
					}
				}
				else
				{
					component.FadeIn(0f, false, null);
				}
			}
		}
		else
		{
			List<UICanvasFade> list = new List<UICanvasFade>();
			for (int j = 0; j < this.m_CanvasParent.transform.childCount; j++)
			{
				GameObject gameObject2 = this.m_CanvasParent.transform.GetChild(j).gameObject;
				UICanvasFade component2 = gameObject2.GetComponent<UICanvasFade>();
				if (MainCanvas != gameObject2 && component2 != null && gameObject2.activeInHierarchy)
				{
					list.Add(component2);
					this.m_CanvasStack.Add(component2);
				}
			}
			for (int k = 1; k < list.Count; k++)
			{
				list[k].FadeOut();
			}
			if (list.Count > 0)
			{
				float fadeTime = list[0].fadeTime;
				bool isTimeScaling = list[0].isTimeScaling;
				list[0].FadeOut(fadeTime, isTimeScaling, delegate()
				{
					if (MainCanvas)
					{
						MainCanvas.GetComponent<UICanvasFade>().FadeIn();
					}
				});
			}
			else if (MainCanvas)
			{
				MainCanvas.GetComponent<UICanvasFade>().FadeIn();
			}
		}
	}

	public void OpenBeforeVRCanvas()
	{
		if (this.m_CanvasStack.Count <= 0)
		{
			return;
		}
		UICanvasFade uicanvasFade = this.m_CanvasStack[this.m_CanvasStack.Count - 1];
		this.m_CanvasStack.RemoveAt(this.m_CanvasStack.Count - 1);
		this.ChangeCanvas(uicanvasFade.gameObject);
	}

	public bool IsExistsBeforeVRCanvas()
	{
		return this.m_CanvasStack.Count > 0;
	}

	public void ClearCanvasStack()
	{
		this.m_CanvasStack.Clear();
	}

	public void UpdateCanvas()
	{
		foreach (Canvas canvas in this.m_CanvasArray.Values)
		{
			InterfaceVRCanvas[] componentsInChildren = canvas.GetComponentsInChildren<InterfaceVRCanvas>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].OnUpdateCanvas();
			}
		}
		bool flag = true;
		foreach (Canvas canvas2 in this.m_CanvasArray.Values)
		{
			if (canvas2.gameObject.activeSelf)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			bool isFreeMode = SceneVRCommunication.Instance.IsFreeMode;
			bool uiNoHomeMode = SceneVRCommunication.Instance.UiNoHomeMode;
			if (isFreeMode && !uiNoHomeMode)
			{
				UICanvasFade component = this.m_CanvasArray[VRCanvasManager.VRCanvasType.MainMenu].GetComponent<UICanvasFade>();
				component.FadeIn();
			}
		}
	}

	public void OpenVRConfig(UnityAction callback)
	{
		this.OpenVRCanvas(VRCanvasManager.VRCanvasType.VRConfig);
		Canvas canvas = this.m_CanvasArray[VRCanvasManager.VRCanvasType.VRConfig];
		Transform transform = canvas.transform.Find("Button return");
		Button button = transform.GetComponent<Button>();
		UICanvasFade fade = canvas.GetComponent<UICanvasFade>();
		this.m_CallbackCloseVRConfig = callback;
		button.onClick.RemoveAllListeners();
		button.onClick.AddListener(delegate()
		{
			fade.FadeOut(fade.fadeTime, fade.isTimeScaling, delegate()
			{
				if (this.m_CallbackCloseVRConfig != null)
				{
					this.m_CallbackCloseVRConfig();
				}
				button.onClick.RemoveAllListeners();
				this.CloseVRCanvas();
			});
		});
	}

	public void OpenVRCulture(UnityAction callback_select, UnityAction callback_cancel)
	{
		VRCultivationSeedInventory componentInChildren = this.m_CanvasCulture.GetComponentInChildren<VRCultivationSeedInventory>();
		componentInChildren.m_OnEventButtonSelected = callback_select;
		componentInChildren.m_OnEventButtonClosed = callback_cancel;
		this.OpenVRCanvas(VRCanvasManager.VRCanvasType.Culture);
	}

	public void OpenVRRentalMaid()
	{
		this.OpenVRCanvas(VRCanvasManager.VRCanvasType.RentalMaid);
	}

	public virtual void OpenVRDecideUseTouch(UnityAction<bool> callback_is_oculus_touch)
	{
		this.OpenVRCanvas(VRCanvasManager.VRCanvasType.DecideUseTouch);
		VRDecideUseTouch component = this.m_CanvasDecideUseTouch.GetComponent<VRDecideUseTouch>();
		component.StartDecideController(callback_is_oculus_touch);
	}

	public virtual void OpenVRTutorialMenu(UnityAction callback)
	{
		this.OpenVRCanvas(VRCanvasManager.VRCanvasType.Tutorial);
		VRTutorialMenu component = this.m_CanvasTutorial.GetComponent<VRTutorialMenu>();
		component.Init(callback, "vrcom_tutorial.nei");
	}

	private void SetMainMenuButtonEnable(VRMainMenu.ButtonType buttonType, bool flag)
	{
		if (this.m_CanvasMainMenu == null)
		{
			return;
		}
		VRMainMenu component = this.m_CanvasMainMenu.GetComponent<VRMainMenu>();
		component.Event_SetButtonEnabled(buttonType, flag);
	}

	private void SetMainMenuButtonActive(VRMainMenu.ButtonType buttonType, bool flag)
	{
		if (this.m_CanvasMainMenu == null)
		{
			return;
		}
		VRMainMenu component = this.m_CanvasMainMenu.GetComponent<VRMainMenu>();
		component.Event_SetButtonActive(buttonType, flag);
	}

	public void SetSkipButtonEvent(UnityAction callback)
	{
		this.m_CallbackSkipEvent = callback;
	}

	public void ButtonEvent_Skip()
	{
		if (this.m_CallbackSkipEvent != null)
		{
			this.m_CallbackSkipEvent();
		}
	}

	public virtual Canvas GetVRCanvas(VRCanvasManager.VRCanvasType type)
	{
		if (!this.m_CanvasArray.ContainsKey(type))
		{
			return null;
		}
		return this.m_CanvasArray[type];
	}

	public virtual void StartKaraokeMode(Action callback_end_karaoke_mode)
	{
		this.m_CallbackEndKaraokeMode = callback_end_karaoke_mode;
		if (!this.m_KaraokeDataManager)
		{
			GameObject gameObject = Resources.Load<GameObject>("SceneVRCommunication/Tablet/Karaoke/Prefab/uGUI Karaoke");
			if (gameObject)
			{
				GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObject);
				Transform transform = gameObject2.transform;
				transform.SetParent(base.transform.parent, false);
				transform.localPosition = Vector3.zero;
				transform.localEulerAngles = Vector3.right * 90f;
				transform.localScale = Vector3.one;
				this.m_KaraokeDataManager = gameObject2.GetComponent<KaraokeDataManager>();
			}
		}
		base.enabled = false;
		UICanvasFade canvasParent = this.CanvasParent;
		if (canvasParent)
		{
			canvasParent.gameObject.SetActive(false);
		}
	}

	public virtual void EndKaraokeMode()
	{
		base.enabled = true;
		UICanvasFade canvasParent = this.CanvasParent;
		if (canvasParent)
		{
			canvasParent.gameObject.SetActive(true);
		}
		if (this.m_CallbackEndKaraokeMode == null)
		{
			Debug.LogWarning("[VRCanvasManager]カラオケモード終了時のコールバックが見つかりません");
			return;
		}
		this.m_CallbackEndKaraokeMode();
		if (this.m_KaraokeDataManager == null)
		{
			Debug.LogWarning("[VRCanvasManager]カラオケモード用のUIのキャッシュが見つかりません");
		}
		UnityEngine.Object.Destroy(this.m_KaraokeDataManager.gameObject);
	}

	protected static VRCanvasManager m_Instance;

	[SerializeField]
	[Tooltip("デバッグ用：メインメニューのボタンをすべて有効にする")]
	protected bool Debug_IsMainMenuButtonEnabled;

	[SerializeField]
	[Tooltip("VR用キャンバスの親オブジェクト")]
	protected UICanvasFade m_CanvasParent;

	[SerializeField]
	protected Canvas m_CanvasMainMenu;

	[SerializeField]
	protected Canvas m_CanvasManual;

	[SerializeField]
	protected Canvas m_CanvasNeiLetter;

	[SerializeField]
	protected Canvas m_CanvasDialog;

	[SerializeField]
	protected Canvas m_CanvasVRConfig;

	[SerializeField]
	protected Canvas m_CanvasPhotoMode;

	[SerializeField]
	protected Canvas m_CanvasMoviePlayer;

	[SerializeField]
	protected Canvas m_CanvasMusicPlayer;

	[SerializeField]
	protected Canvas m_CanvasSelector;

	[SerializeField]
	protected Canvas m_CanvasCulture;

	[SerializeField]
	protected Canvas m_CanvasTrophy;

	[SerializeField]
	protected Canvas m_CanvasVIP;

	[SerializeField]
	protected Canvas m_CanvasChangeBG;

	[SerializeField]
	protected Canvas m_CanvasRentalMaid;

	[SerializeField]
	protected Canvas m_CanvasTutorial;

	[SerializeField]
	protected Canvas m_CanvasDecideUseTouch;

	private Dictionary<VRCanvasManager.VRCanvasType, Canvas> m_CanvasArray = new Dictionary<VRCanvasManager.VRCanvasType, Canvas>();

	private List<UICanvasFade> m_CanvasStack = new List<UICanvasFade>();

	private bool m_BeforeHandCameraMode;

	private bool m_BeforeShowUI;

	private bool m_BeforeFreeMode;

	private bool m_BeforeNoMoveMode;

	private bool m_IsYotogiSelecting;

	private UnityAction m_CallbackSkipEvent;

	protected Action m_CallbackEndKaraokeMode;

	protected KaraokeDataManager m_KaraokeDataManager;

	public UnityAction m_CallbackCloseVRConfig;

	public enum VRCanvasType
	{
		NONE,
		MainMenu,
		Manual,
		NeiLetter,
		Dialog,
		VRConfig,
		PhotoMode,
		MoviePlayer,
		MusicPlayer,
		Selector,
		Culture,
		Trophy,
		VIP,
		ChangeBG,
		RentalMaid,
		Tutorial,
		DecideUseTouch
	}
}
