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

public class OvrHandAnimator : MonoBehaviour
{
	protected virtual void Start()
	{
		for (int i = 0; i < this.m_aryFingerData.Length; i++)
		{
			this.m_aryFingerData[i] = new OvrHandAnimator.FingerMorph();
		}
		List<Transform> list = new List<Transform>();
		this.FindChild(list, this.m_trRootGrasp);
		for (int j = 0; j < this.m_listRoot.Length; j++)
		{
			List<OvrHandAnimator.BonePair> list2 = new List<OvrHandAnimator.BonePair>();
			List<Transform> list3 = new List<Transform>();
			this.FindChild(list3, this.m_trFingerRoot[j]);
			using (List<Transform>.Enumerator enumerator = list3.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Transform trBase = enumerator.Current;
					Transform transform = list.Find((Transform a) => a.name == trBase.name);
					if (transform != null)
					{
						list2.Add(new OvrHandAnimator.BonePair(trBase, transform));
					}
					else
					{
						Debug.LogWarning("指ペアが見つかりません。" + trBase.name);
					}
				}
			}
			this.m_listRoot[j] = list2;
		}
	}

	public virtual void Init(AVRControllerButtons f_CtrlBtn)
	{
		this.m_CtrlBtn = f_CtrlBtn;
	}

	protected virtual void FindChild(List<Transform> f_list, Transform f_trParent)
	{
		for (int i = 0; i < f_trParent.childCount; i++)
		{
			this.FindChild(f_list, f_trParent.GetChild(i));
		}
		f_list.Add(f_trParent);
	}

	public virtual void FingerRateTarget(OvrHandAnimator.Finger f_eFinger, float f_fRate)
	{
		this.m_fTargetRate[(int)f_eFinger] = f_fRate;
	}

	protected virtual void FingerRateUpdate(OvrHandAnimator.Finger f_eFinger, float f_fRate)
	{
		OvrHandAnimator.FingerMorph fingerMorph = this.m_aryFingerData[(int)f_eFinger];
		fingerMorph.fTarget = f_fRate;
		float num = fingerMorph.fTarget - fingerMorph.fNow;
		float num2 = num * this.m_fEaseRate;
		fingerMorph.fNow += num2;
		List<OvrHandAnimator.BonePair> list = this.m_listRoot[(int)f_eFinger];
		for (int i = 0; i < list.Count; i++)
		{
			OvrHandAnimator.BonePair bonePair = list[i];
			bonePair.trMain.localRotation = Quaternion.Slerp(bonePair.qBackRot, bonePair.trGrasp.localRotation, fingerMorph.fNow);
		}
	}

	protected virtual void Update()
	{
		if (this.m_CtrlBtn != null)
		{
			if (this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.REST) || this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.XA) || this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.YB))
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Thumb, 1f);
			}
			else
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Thumb, 0f);
			}
			if (this.m_CtrlBtn.GetPress(AVRControllerButtons.BTN.TRIGGER))
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Index, 1f);
			}
			else if (this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.TRIGGER))
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Index, 0.5f);
			}
			else
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Index, 0f);
			}
			if (this.m_CtrlBtn.GetPress(AVRControllerButtons.BTN.GRIP))
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Middle, 1f);
				this.FingerRateTarget(OvrHandAnimator.Finger.Ring, 1f);
				this.FingerRateTarget(OvrHandAnimator.Finger.Little, 1f);
			}
			else
			{
				this.FingerRateTarget(OvrHandAnimator.Finger.Middle, 0f);
				this.FingerRateTarget(OvrHandAnimator.Finger.Ring, 0f);
				this.FingerRateTarget(OvrHandAnimator.Finger.Little, 0f);
			}
		}
		for (int i = 0; i < 5; i++)
		{
			this.FingerRateUpdate((OvrHandAnimator.Finger)i, this.m_fTargetRate[i]);
		}
	}

	public Transform m_trRootMain;

	public Transform m_trRootGrasp;

	public Transform[] m_trFingerRoot = new Transform[5];

	public float m_fEaseRate = 0.1f;

	public float[] m_fTargetRate = new float[5];

	protected List<OvrHandAnimator.BonePair>[] m_listRoot = new List<OvrHandAnimator.BonePair>[5];

	protected OvrHandAnimator.FingerMorph[] m_aryFingerData = new OvrHandAnimator.FingerMorph[5];

	protected AVRControllerButtons m_CtrlBtn;

	public enum Finger
	{
		Thumb,
		Index,
		Middle,
		Ring,
		Little,
		Max
	}

	protected class BonePair
	{
		public BonePair(Transform main, Transform grasp)
		{
			this.trMain = main;
			this.trGrasp = grasp;
			this.vBackPos = this.trMain.localPosition;
			this.qBackRot = this.trMain.localRotation;
		}

		public Transform trMain;

		public Transform trGrasp;

		public Vector3 vBackPos;

		public Quaternion qBackRot;
	}

	protected class FingerMorph
	{
		public float fNow;

		public float fTarget;
	}
}
