using System;
using UnityEngine;

public class OvrGrabObj : MonoBehaviour
{
	private void Awake()
	{
		if (!GameMain.Instance.VRMode || GameMain.Instance.VRFamily == GameMain.VRFamilyType.NON)
		{
			base.enabled = false;
		}
		this.m_nOvrGrabHandLayerNo = LayerMask.NameToLayer("OvrGrabHand");
	}

	private void Start()
	{
		if (this.m_trTarget == null)
		{
			this.m_trTarget = base.transform;
		}
		this.m_rb = base.GetComponent<Rigidbody>();
		if (this.m_rb != null)
		{
			this.m_rb.maxAngularVelocity = float.PositiveInfinity;
			this.m_rb.sleepThreshold = -1f;
		}
	}

	private void OnDisable()
	{
		this.m_bHitL = (this.m_bHitR = false);
		this.m_bGrabSubR = (this.m_bGrabL = (this.m_bGrabSubL = (this.m_bGrabR = false)));
		this.m_bEnterCountR = (this.m_bEnterCountL = 0);
		if (this.m_goDummyChild != null)
		{
			UnityEngine.Object.Destroy(this.m_goDummyChild);
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.layer != this.m_nOvrGrabHandLayerNo)
		{
			return;
		}
		OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
		OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
		if (other.gameObject == left_controller.grip_collider.gameObject)
		{
			this.m_bHitL = true;
			this.m_bGrabSubL = false;
			this.m_bEnterCountL++;
		}
		if (other.gameObject == right_controller.grip_collider.gameObject)
		{
			this.m_bHitR = true;
			this.m_bGrabSubR = false;
			this.m_bEnterCountR++;
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if (other.gameObject.layer != this.m_nOvrGrabHandLayerNo)
		{
			return;
		}
		OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
		OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
		if (other.gameObject == left_controller.grip_collider.gameObject)
		{
			this.m_bEnterCountL--;
			this.m_bHitL = false;
			this.m_bGrabSubL = false;
			this.m_bEnterCountL = 0;
		}
		if (other.gameObject == right_controller.grip_collider.gameObject)
		{
			this.m_bEnterCountR--;
			this.m_bHitR = false;
			this.m_bGrabSubR = false;
			this.m_bEnterCountR = 0;
		}
	}

	private void OnTriggerStay(Collider other)
	{
		if (other.gameObject.layer != this.m_nOvrGrabHandLayerNo)
		{
			return;
		}
		OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
		OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
		if (other.gameObject == left_controller.grip_collider.gameObject)
		{
			this.m_bHitL = true;
		}
		if (other.gameObject == right_controller.grip_collider.gameObject)
		{
			this.m_bHitR = true;
		}
	}

	private void FixedUpdate()
	{
		this.m_bHitR = (this.m_bHitL = false);
		if (this.m_bGrabToKinematic && this.m_rb != null && this.isThrow)
		{
			this.isThrow = false;
			this.m_rb.isKinematic = false;
			Vector3 a = this.nowVelocity / this.fThrowDeltaTime;
			Vector3 a2 = this.nowAngularVelocity / this.fThrowDeltaTime;
			this.m_rb.AddForce(a * this.m_fGrabThrowRate, ForceMode.VelocityChange);
			this.m_rb.AddTorque(a2 * this.m_fGrabThrowRate, ForceMode.VelocityChange);
		}
	}

	private void Update()
	{
		OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
		OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
		if (this.m_bHitL)
		{
			if (!this.m_bGrabR)
			{
				if (left_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
				{
					this.m_bGrabL = true;
					Transform transform = left_controller.grip_collider.gameObject.transform;
					if (this.m_goDummyChild != null)
					{
						UnityEngine.Object.Destroy(this.m_goDummyChild);
					}
					this.m_goDummyChild = new GameObject("DummyGrabChild");
					this.m_goDummyChild.transform.position = this.m_trTarget.position;
					this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
					this.m_goDummyChild.transform.SetParent(transform, true);
				}
			}
			else if (this.m_bGrabR && this.m_bScaleEnable)
			{
				if (left_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
				{
					this.m_bGrabSubL = true;
					this.m_fDistanceHandBack = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
					this.m_vBackScale = this.m_trTarget.localScale;
				}
				if (this.m_bGrabSubL)
				{
					if (left_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
					{
						float num = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
						float d = num / this.m_fDistanceHandBack;
						this.m_trTarget.localScale = this.m_vBackScale * d;
					}
					else
					{
						this.m_bGrabSubL = false;
					}
				}
			}
			else
			{
				this.m_bGrabSubL = false;
			}
		}
		if (this.m_bGrabL)
		{
			if (left_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
			{
				if (this.m_goDummyChild != null)
				{
					this.m_trTarget.position = Vector3.Slerp(this.m_trTarget.position, this.m_goDummyChild.transform.position, Time.deltaTime * this.m_fEaseRate);
					this.m_trTarget.rotation = Quaternion.Slerp(this.m_trTarget.rotation, this.m_goDummyChild.transform.rotation, Time.deltaTime * this.m_fEaseRate);
				}
			}
			else
			{
				if (this.m_goDummyChild != null)
				{
					UnityEngine.Object.Destroy(this.m_goDummyChild);
				}
				this.m_bGrabL = false;
				if (this.m_bHitR && right_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
				{
					this.m_bGrabR = true;
					Transform transform2 = right_controller.grip_collider.gameObject.transform;
					if (this.m_goDummyChild != null)
					{
						UnityEngine.Object.Destroy(this.m_goDummyChild);
					}
					this.m_goDummyChild = new GameObject("DummyGrabChild");
					this.m_goDummyChild.transform.position = this.m_trTarget.position;
					this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
					this.m_goDummyChild.transform.SetParent(transform2, true);
				}
			}
		}
		if (this.m_bHitR)
		{
			if (!this.m_bGrabL)
			{
				if (right_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
				{
					this.m_bGrabR = true;
					Transform transform3 = right_controller.grip_collider.gameObject.transform;
					if (this.m_goDummyChild != null)
					{
						UnityEngine.Object.Destroy(this.m_goDummyChild);
					}
					this.m_goDummyChild = new GameObject("DummyGrabChild");
					this.m_goDummyChild.transform.position = this.m_trTarget.position;
					this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
					this.m_goDummyChild.transform.SetParent(transform3, true);
				}
			}
			else if (this.m_bGrabL && this.m_bScaleEnable)
			{
				if (right_controller.controller_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
				{
					this.m_bGrabSubR = true;
					this.m_fDistanceHandBack = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
					this.m_vBackScale = this.m_trTarget.localScale;
				}
				if (this.m_bGrabSubR)
				{
					if (right_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
					{
						float num2 = Vector3.Distance(left_controller.grip_collider.transform.position, right_controller.grip_collider.transform.position);
						float d2 = num2 / this.m_fDistanceHandBack;
						this.m_trTarget.localScale = this.m_vBackScale * d2;
					}
					else
					{
						this.m_bGrabSubR = false;
					}
				}
			}
			else
			{
				this.m_bGrabSubR = false;
			}
		}
		if (this.m_bGrabR)
		{
			if (right_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
			{
				if (this.m_goDummyChild != null)
				{
					this.m_trTarget.position = Vector3.Slerp(this.m_trTarget.position, this.m_goDummyChild.transform.position, Time.deltaTime * this.m_fEaseRate);
					this.m_trTarget.rotation = Quaternion.Slerp(this.m_trTarget.rotation, this.m_goDummyChild.transform.rotation, Time.deltaTime * this.m_fEaseRate);
				}
			}
			else
			{
				if (this.m_goDummyChild != null)
				{
					UnityEngine.Object.Destroy(this.m_goDummyChild);
				}
				this.m_bGrabR = false;
				if (this.m_bHitL && left_controller.controller_buttons.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
				{
					this.m_bGrabL = true;
					Transform transform4 = left_controller.grip_collider.gameObject.transform;
					if (this.m_goDummyChild != null)
					{
						UnityEngine.Object.Destroy(this.m_goDummyChild);
					}
					this.m_goDummyChild = new GameObject("DummyGrabChild");
					this.m_goDummyChild.transform.position = this.m_trTarget.position;
					this.m_goDummyChild.transform.rotation = this.m_trTarget.rotation;
					this.m_goDummyChild.transform.SetParent(transform4, true);
				}
			}
		}
		Vector3 vector = Vector3.zero;
		Quaternion nowRotation = Quaternion.identity;
		if (this.m_bGrabToKinematic)
		{
			if (this.m_rb != null)
			{
				if (this.m_bGrabL || this.m_bGrabR)
				{
					OvrMgr.OvrObject ovr_obj = GameMain.Instance.OvrMgr.ovr_obj;
					vector = ((!this.m_bGrabL) ? ovr_obj.right_controller.hand_trans.position : ovr_obj.left_controller.hand_trans.position);
					nowRotation = ((!this.m_bGrabL) ? ovr_obj.right_controller.hand_trans.rotation : ovr_obj.left_controller.hand_trans.rotation);
					this.beforeRotation = this.nowRotation;
					this.nowVelocity = vector - this.m_vBackPos;
					this.nowRotation = nowRotation;
					if (!this.isBeforeGrabL && !this.isBeforeGrabR)
					{
						this.m_rb.isKinematic = true;
						this.beforeRotation = this.nowRotation;
					}
					this.fThrowDeltaTime = Time.deltaTime;
				}
				else if (this.isBeforeGrabL || this.isBeforeGrabR)
				{
					this.isThrow = true;
				}
			}
			this.isBeforeGrabL = this.m_bGrabL;
			this.isBeforeGrabR = this.m_bGrabR;
		}
		this.m_vBackPos = vector;
	}

	public Vector3 nowVelocity { get; private set; }

	public Quaternion nowRotation { get; private set; }

	public Quaternion beforeRotation { get; private set; }

	public Vector3 nowAngularVelocity
	{
		get
		{
			float num;
			Vector3 a;
			(this.nowRotation * Quaternion.Inverse(this.beforeRotation)).ToAngleAxis(out num, out a);
			num *= 0.0174532924f;
			return a * num;
		}
	}

	public bool isBeforeGrabL { get; private set; }

	public bool isBeforeGrabR { get; private set; }

	private bool isThrow { get; set; }

	private float fThrowDeltaTime { get; set; }

	public Transform m_trTarget;

	public float m_fEaseRate = 10f;

	public bool m_bScaleEnable = true;

	public bool m_bGrabToKinematic;

	public float m_fGrabThrowRate = 10f;

	private int m_nOvrGrabHandLayerNo = 18;

	private bool m_bHitL;

	private bool m_bGrabL;

	private bool m_bGrabSubL;

	private bool m_bHitR;

	private bool m_bGrabR;

	private bool m_bGrabSubR;

	private int m_bEnterCountL;

	private int m_bEnterCountR;

	private Rigidbody m_rb;

	private GameObject m_goDummyChild;

	private float m_fDistanceHandBack;

	private Vector3 m_vBackScale;

	private Vector3 m_vBackPos;
}
