using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class VRControllerLaserCast : MonoBehaviour
{
	public float raycastMaxLength
	{
		get
		{
			return this.m_RaycastMaxLength;
		}
		set
		{
			this.m_RaycastMaxLength = value;
		}
	}

	public float PosScreenX
	{
		get
		{
			return this.m_PosScreenX;
		}
	}

	public float PosScreenY
	{
		get
		{
			return this.m_PosScreenY;
		}
	}

	private void Start()
	{
		GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
		if (vrdeviceTypeID == GameMain.VRDeviceType.NON || vrdeviceTypeID == GameMain.VRDeviceType.RIFT || vrdeviceTypeID == GameMain.VRDeviceType.FOVE)
		{
			base.gameObject.SetActive(false);
			return;
		}
		base.StartCoroutine(this.Coroutine_Debug_FindParent());
	}

	private IEnumerator Coroutine_Debug_FindParent()
	{
		for (int i = 0; i < 4; i++)
		{
			yield return null;
		}
		while (this.m_Controller == null)
		{
			this.m_Controller = ((!this.m_IsLeftHand) ? GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller : GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller);
			yield return null;
		}
		GameObject hand = this.m_Controller.gameObject;
		VRTestInputModule.Instance.AddController(this);
		yield break;
	}

	private void Update()
	{
		if (!this.m_Controller)
		{
			if (GameMain.Instance == null)
			{
				return;
			}
			this.m_Controller = ((!this.m_IsLeftHand) ? GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller : GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller);
			if (!this.m_Controller)
			{
				return;
			}
			VRTestInputModule.Instance.AddController(this);
		}
		AVRController controller = this.m_Controller;
		if (!controller.UIRayEnable)
		{
			if (this.m_RaycastLineRenderer)
			{
				this.m_RaycastLineRenderer.gameObject.SetActive(false);
			}
			if (this.m_RaycastPointEffect)
			{
				this.m_RaycastPointEffect.SetActive(false);
			}
			return;
		}
		this.UpdateOffSet(controller.UIRayTrans);
	}

	public void UpdateOffSet(Transform trans)
	{
		base.transform.position = trans.position;
		base.transform.rotation = trans.rotation;
		base.transform.localScale = trans.lossyScale;
	}

	private void OnDestroy()
	{
		if (VRTestInputModule.Instance == null)
		{
			return;
		}
		VRTestInputModule.Instance.RemoveController(this);
	}

	public virtual bool GetButtonDown()
	{
		return this.m_Controller.VRControllerButtons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_L_CLICK);
	}

	public virtual bool GetButtonUp()
	{
		return this.m_Controller.VRControllerButtons.GetPressUp(AVRControllerButtons.BTN.VIRTUAL_L_CLICK);
	}

	public virtual Vector2 GetScrollDelta()
	{
		return Vector2.zero;
	}

	public virtual bool EnableUIRay()
	{
		return this.m_Controller && this.m_Controller.UIRayEnable;
	}

	public virtual void SetRaycastHitPoint(Vector3 WorldPos, Vector3 WorldNormal, float rayLength, GameObject HitObject)
	{
		bool active = false;
		if (HitObject != null && HitObject.GetComponent<RectTransform>() != null)
		{
			active = true;
		}
		if (!this.m_Controller.UIRayEnable)
		{
			return;
		}
		rayLength += this.m_RayLength;
		if (HitObject == null)
		{
			rayLength = this.m_RaycastMaxLength;
		}
		if (this.m_RaycastLineRenderer)
		{
			if (HitObject)
			{
				this.m_RaycastLineRenderer.SetColors(this.m_LineColorHighlighted, Color.white);
			}
			else
			{
				this.m_RaycastLineRenderer.SetColors(this.m_LineColorNormal, Color.white);
			}
			this.m_RaycastLineRenderer.SetPosition(0, base.transform.position);
			this.m_RaycastLineRenderer.SetPosition(1, base.transform.position + base.transform.forward * rayLength);
			this.m_RaycastLineRenderer.gameObject.SetActive(true);
		}
		if (this.m_RaycastPointEffect)
		{
			this.m_RaycastPointEffect.transform.position = base.transform.position + base.transform.forward * rayLength;
			if (HitObject)
			{
				this.m_RaycastPointEffect.transform.rotation = HitObject.transform.rotation;
			}
			this.m_RaycastPointEffect.SetActive(active);
		}
	}

	public void OnEventRayHitEnter(GameObject Obj)
	{
		bool flag = Obj.GetComponent<RectTransform>() != null;
		bool flag2 = false;
		Selectable component = Obj.GetComponent<Selectable>();
		if (component)
		{
			flag2 = component.interactable;
		}
		if (flag && flag2)
		{
			GameMain.Instance.SoundMgr.PlaySystem(SoundMgr.SeType.Hover);
		}
	}

	public void OnEventRayHitExit(GameObject Obj)
	{
	}

	public void OnEventClick(GameObject Obj)
	{
		bool flag = Obj.GetComponent<RectTransform>();
		bool flag2 = false;
		Selectable component = Obj.GetComponent<Selectable>();
		if (component)
		{
			flag2 = component.interactable;
		}
		if (flag && flag2)
		{
			GameMain.Instance.SoundMgr.PlaySystem(SoundMgr.SeType.Click);
		}
	}

	[SerializeField]
	private AVRController m_Controller;

	[SerializeField]
	private bool m_IsLeftHand;

	[SerializeField]
	private float m_RaycastMaxLength = 3f;

	[SerializeField]
	private LineRenderer m_RaycastLineRenderer;

	[SerializeField]
	protected GameObject m_RaycastPointEffect;

	[SerializeField]
	private Color m_LineColorNormal = Color.red;

	[SerializeField]
	private Color m_LineColorHighlighted = Color.green;

	[SerializeField]
	private float m_RayLength = 0.01f;

	protected float m_PosScreenX = 0.5f;

	protected float m_PosScreenY = 0.5f;
}
