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

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

	private void Start()
	{
		if (VRDialogMenu.m_Instance != null)
		{
		}
		VRDialogMenu.m_Instance = this;
		base.gameObject.SetActive(false);
	}

	public void CloseDialog()
	{
		this.m_UICanvasFade.FadeOut();
		VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
		VRCanvasManager.Instance.ClearCanvasStack();
	}

	public void OpenDialog(string message, params VRDialogMenu.DialogButton[] dialogButtons)
	{
		VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.Dialog);
		base.gameObject.SetActive(true);
		this.m_TextMessage.text = message;
		for (int i = 0; i < this.m_ButtonArray.Count; i++)
		{
			this.m_ButtonArray[i].SetActive(false);
		}
		for (int j = 0; j < dialogButtons.Length; j++)
		{
			VRDialogMenu.DialogButton dialogButton = dialogButtons[j];
			GameObject gameObject;
			if (this.m_ButtonArray.Count > j)
			{
				gameObject = this.m_ButtonArray[j].gameObject;
			}
			else
			{
				gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_PrefabDialogButton);
				this.m_ButtonArray.Add(gameObject);
			}
			gameObject.SetActive(true);
			Button component = gameObject.GetComponent<Button>();
			Text componentInChildren = gameObject.GetComponentInChildren<Text>();
			Image image = gameObject.GetComponent<Image>();
			component.onClick.RemoveAllListeners();
			if (dialogButton.isImage)
			{
				componentInChildren.enabled = false;
				string path = this.m_uGUIAtlasPath + dialogButton.text;
				base.StartCoroutine(this.Coroutine_Load<Sprite>(path, delegate(ResourceRequest req)
				{
					image.sprite = (req.asset as Sprite);
				}));
			}
			else
			{
				componentInChildren.text = dialogButton.text;
				componentInChildren.enabled = true;
				string path2 = this.m_uGUIAtlasPath + "vr_frame02.png";
				base.StartCoroutine(this.Coroutine_Load<Sprite>(path2, delegate(ResourceRequest req)
				{
					image.sprite = (req.asset as Sprite);
				}));
			}
			component.onClick.AddListener(dialogButton.action);
			RectTransform component2 = gameObject.GetComponent<RectTransform>();
			if (!dialogButton.isFreePosition)
			{
				component2.SetParent(this.m_ParentButtonArea, false);
			}
			else
			{
				component2.SetParent(base.gameObject.transform, false);
				component2.anchorMin = Vector2.one * 0.5f;
				component2.anchorMax = Vector2.one * 0.5f;
				component2.anchoredPosition3D = dialogButton.anchoredPosition3D;
				component2.sizeDelta = dialogButton.size;
			}
		}
	}

	public void OpenDialog(string message, VRDialogMenu.TYPE_STYLE style, UnityAction<VRDialogMenu.TYPE_STYLE> callback)
	{
		List<VRDialogMenu.DialogButton> list = new List<VRDialogMenu.DialogButton>();
		if ((style & VRDialogMenu.TYPE_STYLE.NO) != (VRDialogMenu.TYPE_STYLE)0)
		{
			list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_no", delegate()
			{
				callback(VRDialogMenu.TYPE_STYLE.NO);
			}));
		}
		if ((style & VRDialogMenu.TYPE_STYLE.YES) != (VRDialogMenu.TYPE_STYLE)0)
		{
			list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_yes", delegate()
			{
				callback(VRDialogMenu.TYPE_STYLE.YES);
			}));
		}
		if ((style & VRDialogMenu.TYPE_STYLE.CANCEL) != (VRDialogMenu.TYPE_STYLE)0)
		{
			list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_cancel", delegate()
			{
				callback(VRDialogMenu.TYPE_STYLE.CANCEL);
			}));
		}
		if ((style & VRDialogMenu.TYPE_STYLE.OK) != (VRDialogMenu.TYPE_STYLE)0)
		{
			list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_ok", delegate()
			{
				callback(VRDialogMenu.TYPE_STYLE.OK);
			}));
		}
		if ((style & VRDialogMenu.TYPE_STYLE.TV) != (VRDialogMenu.TYPE_STYLE)0)
		{
			list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_tv", delegate()
			{
				callback(VRDialogMenu.TYPE_STYLE.TV);
			}));
		}
		if ((style & VRDialogMenu.TYPE_STYLE.CD) != (VRDialogMenu.TYPE_STYLE)0)
		{
			list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_cd", delegate()
			{
				callback(VRDialogMenu.TYPE_STYLE.CD);
			}));
		}
		this.OpenDialog(message, list.ToArray());
	}

	private IEnumerator Coroutine_Load<T>(string path, UnityAction<ResourceRequest> callback) where T : UnityEngine.Object
	{
		ResourceRequest res = Resources.LoadAsync<T>(path);
		while (!res.isDone)
		{
			yield return null;
		}
		callback(res);
		yield break;
	}

	private static VRDialogMenu m_Instance;

	[SerializeField]
	private UICanvasFade m_UICanvasFade;

	[SerializeField]
	private Text m_TextMessage;

	[SerializeField]
	private RectTransform m_ParentButtonArea;

	[SerializeField]
	private GameObject m_PrefabDialogButton;

	private List<GameObject> m_ButtonArray = new List<GameObject>();

	private string m_uGUIAtlasPath = "SceneVRCommunication/Tablet/Sprite/System UI/";

	public class DialogButton
	{
		public DialogButton(bool IsImage, string ButtonText, UnityAction ButtonAction)
		{
			this.isImage = IsImage;
			this.text = ButtonText;
			this.action = ButtonAction;
		}

		public DialogButton(bool IsImage, string ButtonText, UnityAction ButtonAction, Vector3 AnchoredPosition3D, Vector2 Size)
		{
			this.isImage = IsImage;
			this.text = ButtonText;
			this.action = ButtonAction;
			this.anchoredPosition3D = AnchoredPosition3D;
			this.size = Size;
			this.isFreePosition = true;
		}

		public bool isImage;

		public string text = "?";

		public UnityAction action;

		public Vector3 anchoredPosition3D = Vector3.zero;

		public Vector2 size = Vector2.one;

		public bool isFreePosition;
	}

	public enum TYPE_STYLE
	{
		YES = 1,
		NO,
		OK = 4,
		CANCEL = 8,
		TV = 16,
		CD = 32
	}
}
