using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

public class VRHandAttachment : MonoBehaviour
{
	public VRHandAttachment.OnHandAttachment onAttachment
	{
		get
		{
			return this.m_OnAttachment;
		}
	}

	public AVRController controller
	{
		get
		{
			return this.m_Controller;
		}
		private set
		{
			this.m_Controller = value;
		}
	}

	public bool isLeftHand
	{
		get
		{
			return this.m_IsLeftHand;
		}
	}

	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;
		}
		this.coFindParent = this.CoroutineFindParent();
		base.StartCoroutine(this.coFindParent);
	}

	private IEnumerator CoroutineFindParent()
	{
		for (int i = 0; i < 4; i++)
		{
			yield return null;
		}
		while (this.controller == null)
		{
			this.controller = ((!this.m_IsLeftHand) ? GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller : GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller);
			yield return null;
		}
		this.HandAttachmentParent();
		yield break;
	}

	private void HandAttachmentParent()
	{
		GameObject gameObject = this.controller.gameObject;
		this.m_AttachmentGameObject.transform.SetParent(gameObject.transform, false);
		if (this.onAttachment != null)
		{
			this.onAttachment.Invoke(this);
		}
	}

	private void OnDisable()
	{
		if (this.m_IsQuittingApplication)
		{
			return;
		}
		if (this.coFindParent != null && this.coFindParent.MoveNext())
		{
			base.StopCoroutine(this.coFindParent);
		}
	}

	private void OnEnable()
	{
		if (this.coFindParent != null && this.coFindParent.MoveNext())
		{
			base.StartCoroutine(this.coFindParent);
		}
	}

	private void OnApplicationQuit()
	{
		this.m_IsQuittingApplication = true;
	}

	[SerializeField]
	private GameObject m_AttachmentGameObject;

	[SerializeField]
	private VRHandAttachment.OnHandAttachment m_OnAttachment = new VRHandAttachment.OnHandAttachment();

	[SerializeField]
	private bool m_IsLeftHand;

	private AVRController m_Controller;

	private IEnumerator coFindParent;

	private bool m_IsQuittingApplication;

	[Serializable]
	public class OnHandAttachment : UnityEvent<VRHandAttachment>
	{
	}
}
