using System;
using UnityEngine;

public class VREventNotifierGrab : VREventNotifier
{
	protected override void Start()
	{
		this.LookTimeCountDownStop();
	}

	public void Init(OvrMgr.OvrObject.Controller f_ViveController)
	{
		f_ViveController.grip_collider.on_grab_obj_callback = new OvrGripCollider.OnGrabObject(this.GrabObj);
		f_ViveController.grip_collider.on_release_obj_callback = new OvrGripCollider.OnReleaseObject(this.ReleaseObj);
	}

	private void GrabTimeCountDownStart()
	{
		this.m_fMaxTime = this.m_HitGrabTarget.GetTimeOutTime();
		this.m_fNowTime = this.m_fMaxTime;
		this.m_EventText.SetGaugeRate(0f);
		this.m_EventText.Show(VREventText.TYPE.GRAB, this.m_HitGrabTarget.GetEventName(), 0f);
	}

	private void LookTimeCountDownStop()
	{
		this.m_EventText.Hide(VREventText.TYPE.GRAB);
	}

	private void GrabTimeOut()
	{
		Debug.Log("Grab:StartScript");
		this.LookTimeCountDownStop();
		if (this.m_HitGrabTarget != null)
		{
			this.m_HitGrabTarget.StartScript();
			this.m_HitGrabTarget = null;
		}
	}

	public void GrabObj(GameObject f_goTarget)
	{
		if (SceneVRCommunication.Instance == null)
		{
			return;
		}
		if (f_goTarget == null || VREventNotifier.m_clSingle != null || GameMain.Instance.MainCamera.IsFadeProc() || !SceneVRCommunication.Instance.IsFreeMode)
		{
			return;
		}
		this.m_HitGrabTarget = f_goTarget.GetComponent<VREventTriggerGrab>();
		if (this.m_HitGrabTarget != null)
		{
			if (this.m_HitGrabTarget.rigidBody != null)
			{
				Rigidbody rigidBody = this.m_HitGrabTarget.rigidBody;
				Rigidbody rigidbody = rigidBody;
				Vector3 zero = Vector3.zero;
				rigidBody.angularVelocity = zero;
				rigidbody.velocity = zero;
				rigidBody.isKinematic = true;
			}
			if (this.m_HitGrabTarget.IsEnableEvent())
			{
				this.m_HitGrabTarget.OnEvnetCountDownEnter(base.gameObject);
				this.GrabTimeCountDownStart();
				VREventNotifier.m_clSingle = this;
				this.m_HitGrabTarget.m_dgDestroy = new VREventTriggerGrab.dgDestroy(this.ReleaseObj);
			}
			else
			{
				this.m_HitGrabTarget.m_dgDestroy = null;
				this.m_HitGrabTarget = null;
			}
		}
	}

	public void ReleaseObj(GameObject gripObject, Vector3 linearVelocity, Vector3 angularVelocity)
	{
		if (gripObject != null)
		{
			VREventTriggerGrab component = gripObject.GetComponent<VREventTriggerGrab>();
			if (component != null && component.rigidBody != null)
			{
				Rigidbody rigidBody = component.rigidBody;
				rigidBody.isKinematic = false;
				rigidBody.velocity = linearVelocity;
				rigidBody.angularVelocity = angularVelocity;
			}
		}
		if (this.m_HitGrabTarget != null)
		{
			this.m_HitGrabTarget.m_dgDestroy = null;
			this.m_HitGrabTarget.OnEventCountDownExit(base.gameObject);
			this.m_HitGrabTarget = null;
		}
		VREventNotifier.m_clSingle = null;
		this.LookTimeCountDownStop();
	}

	private void Update()
	{
		if (SceneVRCommunication.Instance == null)
		{
			return;
		}
		if (GameMain.Instance.MainCamera.IsFadeOut())
		{
			this.m_HitBackGrabTarget = false;
			if (this.m_HitGrabTarget != null)
			{
				this.m_HitGrabTarget.m_dgDestroy = null;
				this.m_HitGrabTarget = null;
			}
			this.LookTimeCountDownStop();
			return;
		}
		if (this.m_HitGrabTarget != null)
		{
			this.m_fNowTime -= Time.deltaTime;
			if (this.m_fNowTime <= 0f)
			{
				this.GrabTimeOut();
				this.m_EventText.SetGaugeRate(1f);
			}
			else
			{
				this.m_EventText.SetGaugeRate(1f - this.m_fNowTime / this.m_fMaxTime);
			}
		}
		if (this.m_HitBackGrabTarget && this.m_HitGrabTarget == null)
		{
			this.LookTimeCountDownStop();
		}
		this.m_HitBackGrabTarget = (this.m_HitGrabTarget != null);
	}

	public VREventText m_EventText;

	private float m_fMaxTime = 5f;

	private float m_fNowTime;

	private VREventTriggerGrab m_HitGrabTarget;

	private bool m_HitBackGrabTarget;
}
