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

public class VRTestInputModule : BaseInputModule
{
	public static VRTestInputModule Instance
	{
		get
		{
			if (VRTestInputModule.m_Instance == null)
			{
			}
			return VRTestInputModule.m_Instance;
		}
	}

	public void AddController(VRControllerLaserCast controller)
	{
		if (this.m_ControllerData.ContainsKey(controller))
		{
			return;
		}
		this.m_ControllerData.Add(controller, new VRTestInputModule.ControllerData());
		Debug.Log("コントローラを追加しました。");
	}

	public void RemoveController(VRControllerLaserCast controller)
	{
		this.m_ControllerData.Remove(controller);
	}

	private void ChangeCanvasWorldCamera(Camera worldCamera)
	{
		Canvas[] array = Resources.FindObjectsOfTypeAll<Canvas>();
		for (int i = 0; i < array.Length; i++)
		{
			array[i].worldCamera = worldCamera;
		}
	}

	protected override void OnDisable()
	{
		base.OnDisable();
		this.ChangeCanvasWorldCamera(null);
	}

	protected override void OnEnable()
	{
		base.OnEnable();
		this.ChangeCanvasWorldCamera(this.m_UIRaycastCamera);
	}

	protected override void Awake()
	{
		base.Awake();
		if (VRTestInputModule.m_Instance != null)
		{
			Debug.LogWarning(string.Concat(new object[]
			{
				"シングルトン「",
				typeof(VRTestInputModule),
				"」が既に作成されていました。\nゲームオブジェクト「",
				base.gameObject.name,
				"」を削除します。"
			}));
			UnityEngine.Object.DestroyImmediate(base.gameObject);
			return;
		}
		VRTestInputModule.m_Instance = this;
		Debug.Log("シングルトン「" + typeof(VRTestInputModule) + "」を作成しました。");
		this.m_UIRaycastCamera = new GameObject("VR Controller Raycast Camera").AddComponent<Camera>();
		this.m_UIRaycastCamera.clearFlags = CameraClearFlags.Nothing;
		this.m_UIRaycastCamera.orthographic = true;
		this.m_UIRaycastCamera.fieldOfView = 60f;
		this.m_UIRaycastCamera.nearClipPlane = 0f;
		this.m_UIRaycastCamera.farClipPlane = 100f;
		this.m_UIRaycastCamera.depth = -100f;
		this.m_UIRaycastCamera.orthographicSize = 1E-09f;
		this.m_UIRaycastCamera.transform.SetParent(base.gameObject.transform);
		this.m_UIRaycastCamera.stereoTargetEye = StereoTargetEyeMask.None;
		this.m_UIRaycastCamera.gameObject.AddComponent<PhysicsRaycaster>();
		this.ChangeCanvasWorldCamera(this.m_UIRaycastCamera);
		this.m_UIRaycastCamera.enabled = false;
	}

	public override void Process()
	{
		foreach (KeyValuePair<VRControllerLaserCast, VRTestInputModule.ControllerData> keyValuePair in this.m_ControllerData)
		{
			VRControllerLaserCast key = keyValuePair.Key;
			VRTestInputModule.ControllerData value = keyValuePair.Value;
			if (key.gameObject.activeInHierarchy)
			{
				if (key.EnableUIRay())
				{
					if (this.m_UIRaycastCamera)
					{
						this.m_UIRaycastCamera.transform.position = key.transform.position;
						this.m_UIRaycastCamera.transform.rotation = key.transform.rotation;
						if (value.pointerData == null)
						{
							value.pointerData = new PointerEventData(base.eventSystem);
						}
						else
						{
							value.pointerData.Reset();
						}
						PointerEventData pointerData = value.pointerData;
						pointerData.position = new Vector2(key.PosScreenX * (float)this.m_UIRaycastCamera.pixelWidth, key.PosScreenY * (float)this.m_UIRaycastCamera.pixelHeight);
						pointerData.delta = Vector2.zero;
						pointerData.scrollDelta = key.GetScrollDelta();
						base.eventSystem.RaycastAll(pointerData, this.m_RaycastResultCache);
						RaycastResult pointerCurrentRaycast = BaseInputModule.FindFirstRaycast(this.m_RaycastResultCache);
						GameObject gameObject = pointerCurrentRaycast.gameObject;
						pointerData.pointerCurrentRaycast = pointerCurrentRaycast;
						this.m_RaycastResultCache.Clear();
						if (pointerCurrentRaycast.distance > key.raycastMaxLength)
						{
							gameObject = null;
						}
						key.SetRaycastHitPoint(pointerCurrentRaycast.worldPosition, pointerCurrentRaycast.worldNormal, pointerCurrentRaycast.distance, gameObject);
						if (value.pointObject != gameObject)
						{
							if (value.pointObject)
							{
								ExecuteEvents.ExecuteHierarchy<IPointerExitHandler>(value.pointObject, pointerData, ExecuteEvents.pointerExitHandler);
								key.OnEventRayHitExit(value.pointObject);
							}
							if (gameObject)
							{
								ExecuteEvents.ExecuteHierarchy<IPointerEnterHandler>(gameObject, pointerData, ExecuteEvents.pointerEnterHandler);
								key.OnEventRayHitEnter(gameObject);
							}
						}
						value.pointObject = gameObject;
						if (pointerData.scrollDelta.sqrMagnitude > 0f)
						{
							ExecuteEvents.ExecuteHierarchy<IScrollHandler>(value.pointObject, pointerData, ExecuteEvents.scrollHandler);
						}
						if (key.GetButtonDown())
						{
							if (base.eventSystem.currentSelectedGameObject)
							{
								base.eventSystem.SetSelectedGameObject(null);
							}
							pointerData.pressPosition = pointerData.position;
							pointerData.pointerPressRaycast = pointerData.pointerCurrentRaycast;
							pointerData.pointerPress = null;
							if (value.pointObject)
							{
								value.pressedObject = value.pointObject;
								GameObject gameObject2 = ExecuteEvents.ExecuteHierarchy<IPointerDownHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerDownHandler);
								if (!gameObject2)
								{
									gameObject2 = ExecuteEvents.ExecuteHierarchy<IPointerClickHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerClickHandler);
									if (gameObject2 != null)
									{
										value.pressedObject = gameObject2;
									}
								}
								else
								{
									pointerData.pointerPress = gameObject2;
									value.pressedObject = gameObject2;
									base.eventSystem.SetSelectedGameObject(gameObject2);
								}
								ExecuteEvents.ExecuteHierarchy<IBeginDragHandler>(value.pressedObject, pointerData, ExecuteEvents.beginDragHandler);
								GameObject gameObject3 = ExecuteEvents.ExecuteHierarchy<IDragHandler>(value.pressedObject, pointerData, ExecuteEvents.dragHandler);
								pointerData.pointerDrag = gameObject3;
								value.draggingObject = gameObject3;
							}
						}
						if (key.GetButtonUp())
						{
							if (value.draggingObject)
							{
								ExecuteEvents.ExecuteHierarchy<IEndDragHandler>(value.draggingObject, pointerData, ExecuteEvents.endDragHandler);
								pointerData.pointerDrag = null;
								value.draggingObject = null;
							}
							if (value.pressedObject)
							{
								GameObject eventHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(value.pointObject);
								if (value.pressedObject == eventHandler)
								{
									key.OnEventClick(value.pressedObject);
									if (!ExecuteEvents.Execute<ISubmitHandler>(value.pressedObject, pointerData, ExecuteEvents.submitHandler))
									{
										ExecuteEvents.Execute<IPointerClickHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerClickHandler);
									}
								}
								ExecuteEvents.Execute<IPointerUpHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerUpHandler);
								pointerData.pointerPress = null;
								value.pressedObject = null;
							}
							if (base.eventSystem.currentSelectedGameObject)
							{
								base.eventSystem.SetSelectedGameObject(null);
							}
						}
						if (value.draggingObject)
						{
							ExecuteEvents.ExecuteHierarchy<IDragHandler>(value.draggingObject, pointerData, ExecuteEvents.dragHandler);
						}
					}
				}
			}
		}
	}

	private static VRTestInputModule m_Instance;

	private Camera m_UIRaycastCamera;

	private Dictionary<VRControllerLaserCast, VRTestInputModule.ControllerData> m_ControllerData = new Dictionary<VRControllerLaserCast, VRTestInputModule.ControllerData>();

	private class ControllerData
	{
		public PointerEventData pointerData;

		public GameObject pointObject;

		public GameObject pressedObject;

		public GameObject draggingObject;
	}
}
