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

namespace Leap.Unity
{
	public class ProximityDetector : Detector
	{
		public GameObject CurrentObject
		{
			get
			{
				return this._currentObj;
			}
		}

		private void Awake()
		{
			this.proximityWatcherCoroutine = this.proximityWatcher();
			if (this.TagName != string.Empty)
			{
				GameObject[] array = GameObject.FindGameObjectsWithTag(this.TagName);
				List<GameObject> list = new List<GameObject>(array.Length + this.TargetObjects.Length);
				for (int i = 0; i < this.TargetObjects.Length; i++)
				{
					list.Add(this.TargetObjects[i]);
				}
				for (int j = 0; j < array.Length; j++)
				{
					list.Add(array[j]);
				}
				this.TargetObjects = list.ToArray();
			}
		}

		private void OnEnable()
		{
			base.StopCoroutine(this.proximityWatcherCoroutine);
			base.StartCoroutine(this.proximityWatcherCoroutine);
		}

		private void OnDisable()
		{
			base.StopCoroutine(this.proximityWatcherCoroutine);
			this.Deactivate();
		}

		private IEnumerator proximityWatcher()
		{
			bool proximityState = false;
			for (;;)
			{
				float onSquared = this.OnDistance * this.OnDistance;
				float offSquared = this.OffDistance * this.OffDistance;
				if (this._currentObj != null)
				{
					if (this.distanceSquared(this._currentObj) > offSquared)
					{
						this._currentObj = null;
						proximityState = false;
					}
				}
				else if (this.UseLayersNotList)
				{
					Collider[] array = Physics.OverlapSphere(base.transform.position, this.OnDistance, this.Layer);
					if (array.Length > 0)
					{
						this._currentObj = array[0].gameObject;
						proximityState = true;
						this.OnProximity.Invoke(this._currentObj);
					}
				}
				else
				{
					for (int i = 0; i < this.TargetObjects.Length; i++)
					{
						GameObject gameObject = this.TargetObjects[i];
						if (this.distanceSquared(gameObject) < onSquared)
						{
							this._currentObj = gameObject;
							proximityState = true;
							this.OnProximity.Invoke(this._currentObj);
							break;
						}
					}
				}
				if (proximityState)
				{
					this.Activate();
				}
				else
				{
					this.Deactivate();
				}
				yield return new WaitForSeconds(this.Period);
			}
			yield break;
		}

		private float distanceSquared(GameObject target)
		{
			Collider component = target.GetComponent<Collider>();
			Vector3 a;
			if (component != null)
			{
				a = component.ClosestPointOnBounds(base.transform.position);
			}
			else
			{
				a = target.transform.position;
			}
			return (a - base.transform.position).sqrMagnitude;
		}

		[Tooltip("The interval in seconds at which to check this detector's conditions.")]
		public float Period = 0.1f;

		[Tooltip("Dispatched when close enough to a target.")]
		public ProximityEvent OnProximity;

		[Tooltip("The list of target objects.")]
		public GameObject[] TargetObjects;

		[Tooltip("Objects with this tag are added to the list of targets.")]
		public string TagName = string.Empty;

		[Tooltip("Use a Layer instead of the target list.")]
		public bool UseLayersNotList;

		[Tooltip("The Layer containing the objects to check.")]
		public LayerMask Layer;

		[Tooltip("The target distance in meters to activate the detector.")]
		public float OnDistance = 0.01f;

		[Tooltip("The distance in meters at which to deactivate the detector.")]
		public float OffDistance = 0.015f;

		private IEnumerator proximityWatcherCoroutine;

		private GameObject _currentObj;
	}
}
