using System;
using System.Collections;
using Leap.Unity.Attributes;
using UnityEngine;

namespace Leap.Unity
{
	public class ExtendedFingerDetector : Detector
	{
		private void OnValidate()
		{
			int num = 0;
			int num2 = 0;
			PointingState[] array = new PointingState[]
			{
				this.Thumb,
				this.Index,
				this.Middle,
				this.Ring,
				this.Pinky
			};
			foreach (PointingState pointingState in array)
			{
				if (pointingState != PointingState.Extended)
				{
					if (pointingState == PointingState.NotExtended)
					{
						num2++;
					}
				}
				else
				{
					num++;
				}
				this.MinimumExtendedCount = Math.Max(num, this.MinimumExtendedCount);
				this.MaximumExtendedCount = Math.Min(5 - num2, this.MaximumExtendedCount);
				this.MaximumExtendedCount = Math.Max(num, this.MaximumExtendedCount);
			}
		}

		private void Awake()
		{
			this.watcherCoroutine = this.extendedFingerWatcher();
		}

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

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

		private IEnumerator extendedFingerWatcher()
		{
			for (;;)
			{
				bool fingerState = false;
				if (this.HandModel != null && this.HandModel.IsTracked)
				{
					Hand hand = this.HandModel.GetLeapHand();
					if (hand != null)
					{
						fingerState = (this.matchFingerState(hand.Fingers[0], this.Thumb) && this.matchFingerState(hand.Fingers[1], this.Index) && this.matchFingerState(hand.Fingers[2], this.Middle) && this.matchFingerState(hand.Fingers[3], this.Ring) && this.matchFingerState(hand.Fingers[4], this.Pinky));
						int num = 0;
						for (int i = 0; i < 4; i++)
						{
							if (hand.Fingers[i].IsExtended)
							{
								num++;
							}
						}
						fingerState = (fingerState && num <= this.MaximumExtendedCount && num >= this.MinimumExtendedCount);
						if (this.HandModel.IsTracked && fingerState)
						{
							this.Activate();
						}
						else if (!this.HandModel.IsTracked || !fingerState)
						{
							this.Deactivate();
						}
					}
				}
				else if (base.IsActive)
				{
					this.Deactivate();
				}
				yield return new WaitForSeconds(this.Period);
			}
			yield break;
		}

		private bool matchFingerState(Finger finger, PointingState requiredState)
		{
			return requiredState == PointingState.Either || (requiredState == PointingState.Extended && finger.IsExtended) || (requiredState == PointingState.NotExtended && !finger.IsExtended);
		}

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

		[AutoFind(AutoFindLocations.Parents)]
		[Tooltip("The hand model to watch. Set automatically if detector is on a hand.")]
		public IHandModel HandModel;

		public PointingState Thumb = PointingState.Either;

		public PointingState Index = PointingState.Either;

		public PointingState Middle = PointingState.Either;

		public PointingState Ring = PointingState.Either;

		public PointingState Pinky = PointingState.Either;

		[Range(0f, 5f)]
		public int MinimumExtendedCount;

		[Range(0f, 5f)]
		public int MaximumExtendedCount = 5;

		private IEnumerator watcherCoroutine;
	}
}
