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

public class VRMiniGameWanage : MonoBehaviour
{
	private void Start()
	{
		base.GetComponent<Rigidbody>().maxAngularVelocity = 12.566371f;
		base.GetComponent<Rigidbody>().sleepThreshold = 0.5f;
		this.m_VRController = VRMiniGameManager.Instance.VRController;
		GameObject[] array = GameObject.FindGameObjectsWithTag("MiniGameTarget");
		this.m_TargetObject = array[UnityEngine.Random.Range(0, array.Length - 1)];
		VRMiniGameSetup miniGameSetup = UnityEngine.Object.FindObjectOfType<VRMiniGameSetup>();
		if (miniGameSetup.Is2DMode)
		{
			VRMiniGameGaugeeManager gaugeManager = miniGameSetup.GaugeManager;
			gaugeManager.onMouseClickDownEvent = (VRMiniGameGaugeeManager.OnMouseClickEvent)Delegate.Combine(gaugeManager.onMouseClickDownEvent, new VRMiniGameGaugeeManager.OnMouseClickEvent(delegate(VRMiniGameGaugeeManager manager)
			{
				if (miniGameSetup.GaugeManager.isPlay)
				{
					return;
				}
				if (!this.m_IsGrab)
				{
					return;
				}
				miniGameSetup.GaugeManager.Play();
			}));
			VRMiniGameGaugeeManager gaugeManager2 = miniGameSetup.GaugeManager;
			gaugeManager2.onMouseClickUpEvent = (VRMiniGameGaugeeManager.OnMouseClickEvent)Delegate.Combine(gaugeManager2.onMouseClickUpEvent, new VRMiniGameGaugeeManager.OnMouseClickEvent(delegate(VRMiniGameGaugeeManager manager)
			{
				if (!miniGameSetup.GaugeManager.isPlay)
				{
					return;
				}
				if (!this.m_IsGrab)
				{
					return;
				}
				OvrCamera useCamera = miniGameSetup.UseCamera;
				float num = 0.1f;
				float num2 = 7f;
				float gaugeValue = miniGameSetup.GaugeManager.gaugeValue;
				float d = num + (num2 - num) * gaugeValue;
				Vector3 vector = this.m_VRController.transform.position - useCamera.m_goCenterEye.transform.position;
				vector = (Quaternion.AngleAxis(-30f, useCamera.m_goCenterEye.transform.right) * vector).normalized * d;
				Vector3[] deviationValue = this.GetDeviationValue(this.m_VelocityArray);
				vector += this.CalcThrowVec(deviationValue);
				Vector3[] deviationValue2 = this.GetDeviationValue(this.m_EulerAngleArray);
				Vector3 vector2 = this.CalcThrowVec(deviationValue2);
				vector2 = Vector3.Scale(vector2, this.transform.up);
				this.GrabEnd(vector, vector2);
				miniGameSetup.GaugeManager.Stop();
			}));
		}
		else if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.VIVE)
		{
			if (this.m_VRController.HandSide)
			{
				this.m_Pivot.localPosition = new Vector3(0f, -0.01f, -0.02f);
			}
			else
			{
				this.m_Pivot.localPosition = new Vector3(0f, 0.01f, -0.02f);
			}
			this.m_Pivot.localEulerAngles = Vector3.zero;
		}
		else
		{
			this.m_Pivot.localPosition = Vector3.zero;
		}
	}

	private void Update()
	{
		this.m_Particle.transform.rotation = Quaternion.AngleAxis(-90f, Vector3.right);
		if (VRMiniGameManager.Instance.MiniGameState != VRMiniGameManager.VRMiniGameState.Playing)
		{
			return;
		}
		if (this.m_IsRespawn)
		{
			return;
		}
		if (base.transform.position.y < -5f)
		{
			if (this.m_IsJudgeScore)
			{
				Debug.Log("[ ミニゲーム ]投擲したが、奈落に落ちた。");
				this.m_IsJudgeScore = false;
			}
			this.Refresh();
		}
		if (this.m_IsJudgeScore)
		{
			if (base.GetComponent<Rigidbody>().velocity.sqrMagnitude < 1f)
			{
				Transform transform = this.m_ColliderJudgeScore.transform;
				Collider[] array = Physics.OverlapBox(transform.position, transform.lossyScale * 0.5f, transform.rotation);
				bool flag = false;
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i].CompareTag("MiniGameTarget"))
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					Debug.Log("[ ミニゲーム ]投擲に成功！！");
					VRMiniGameManager.Instance.score++;
					VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Hit);
					this.m_IsJudgeScore = false;
					this.m_Particle.Play();
					base.StartCoroutine(this.Coroutine_Refresh(2f));
				}
			}
			if (base.GetComponent<Rigidbody>().IsSleeping())
			{
				VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Miss);
				Debug.Log("[ ミニゲーム ]投擲したが、失敗した。");
				this.m_IsJudgeScore = false;
				base.StartCoroutine(this.Coroutine_Refresh(2f));
			}
		}
		if (this.m_IsDropped && !this.m_IsJudgeScore && base.GetComponent<Rigidbody>().IsSleeping())
		{
			Debug.Log("[ ミニゲーム ]投擲したが、勢いが弱かった");
			this.m_IsJudgeScore = false;
			base.StartCoroutine(this.Coroutine_Refresh(2f));
		}
		if (!this.m_VRController)
		{
			return;
		}
		if (this.m_IsGrab)
		{
			this.UpdateGrabTransform();
			if (this.m_VRController.GetPressDown(AVRControllerButtons.BTN.TRIGGER))
			{
				base.GetComponent<Rigidbody>().useGravity = false;
				if (this.m_TargetObject == null)
				{
					GameObject[] array2 = GameObject.FindGameObjectsWithTag("MiniGameTarget");
					this.m_TargetObject = array2[UnityEngine.Random.Range(0, array2.Length - 1)];
				}
			}
			if (this.m_VRController.GetPress(AVRControllerButtons.BTN.TRIGGER))
			{
			}
			if (this.m_VRController.GetPressUp(AVRControllerButtons.BTN.TRIGGER))
			{
				Vector3[] deviationValue = this.GetDeviationValue(this.m_VelocityArray);
				Vector3 vector = this.CalcThrowVec(deviationValue);
				vector = Vector3.Scale(vector, base.transform.up);
				Vector3[] deviationValue2 = this.GetDeviationValue(this.m_VelocityArray);
				Vector3 vector2 = this.CalcThrowVec(deviationValue2);
				vector2 *= this.m_Power;
				this.GrabEnd(vector2, vector);
			}
		}
	}

	private void Refresh()
	{
		if (!this.m_IsDropped)
		{
			VRMiniGameManager.VoiceType type = (VRMiniGameManager.VoiceType)Enum.Parse(typeof(VRMiniGameManager.VoiceType), "Remaining" + VRMiniGameManager.Instance.life.ToString());
			VRMiniGameManager.Instance.AddQueueVoice(type);
		}
		if (VRMiniGameManager.Instance.life <= 0)
		{
			VRMiniGameSetup vrminiGameSetup = UnityEngine.Object.FindObjectOfType<VRMiniGameSetup>();
			vrminiGameSetup.GaugeManager.isVisible = false;
			VRMiniGameManager.Instance.Result(delegate
			{
				Debug.Log("輪投げを終了。");
			});
			return;
		}
		Rigidbody component = base.GetComponent<Rigidbody>();
		component.velocity = Vector3.zero;
		component.useGravity = false;
		component.isKinematic = false;
		this.m_IsGrab = true;
		this.m_IsDropped = false;
		this.m_IsRespawn = false;
		this.m_GrabNum = 0;
		for (int i = 0; i < this.m_VelocityArray.Length; i++)
		{
			this.m_VelocityArray[i] = Vector3.zero;
		}
	}

	private IEnumerator Coroutine_Refresh(float tim)
	{
		this.m_IsRespawn = true;
		yield return new WaitForSeconds(tim);
		this.Refresh();
		yield break;
	}

	private void UpdateGrabTransform()
	{
		Transform transform = this.m_VRController.transform;
		Vector3 a = transform.localToWorldMatrix.MultiplyPoint3x4(Vector3.forward * this.m_Magnitude);
		Vector3 vector = a - base.transform.position;
		Vector3 zero = Vector3.zero;
		zero.x = Mathf.DeltaAngle(base.transform.eulerAngles.x, transform.transform.eulerAngles.x);
		zero.y = Mathf.DeltaAngle(base.transform.eulerAngles.y, transform.transform.eulerAngles.y);
		zero.z = Mathf.DeltaAngle(base.transform.eulerAngles.z, transform.transform.eulerAngles.z);
		base.transform.position += vector;
		base.transform.eulerAngles += zero;
		base.GetComponent<Rigidbody>().velocity = Vector3.zero;
		base.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
		if (vector.magnitude > 1f)
		{
			return;
		}
		this.UpdateVectorArray(this.m_VelocityArray, vector * 60f);
		this.UpdateVectorArray(this.m_EulerAngleArray, zero);
	}

	private void UpdateVectorArray(Vector3[] array, Vector3 vec)
	{
		for (int i = array.Length - 1; i > 0; i--)
		{
			array[i] = array[i - 1];
		}
		array[0] = vec;
	}

	private void GrabEnd(Vector3 Velocity, Vector3 AngularVelocity)
	{
		base.GetComponent<Rigidbody>().useGravity = true;
		this.m_IsGrab = false;
		this.m_GrabNum--;
		if (this.m_GrabNum > 0)
		{
			return;
		}
		if (this.m_IsJudgeScore)
		{
			return;
		}
		if (this.m_TargetObject)
		{
			Vector3 a = this.m_TargetObject.transform.position - base.transform.position;
			float num = -4.905f;
			float y = Velocity.y;
			float num2 = -a.y;
			float num3 = (-y - Mathf.Sqrt(y * y - 4f * num * num2)) / (2f * num);
			if (num3 > 0f)
			{
				Vector3 b = a / num3;
				b.y = Velocity.y;
				float sqrMagnitude = (Velocity - b).sqrMagnitude;
				Debug.DrawLine(base.transform.position, base.transform.position + Velocity, Color.red, 5f);
				Debug.DrawLine(base.transform.position, base.transform.position + b, Color.green, 5f);
				Velocity = Vector3.Lerp(Velocity, b, this.m_DebugPower / sqrMagnitude);
				Debug.DrawLine(base.transform.position, base.transform.position + Velocity, Color.yellow, 5f);
			}
		}
		base.GetComponent<Rigidbody>().AddForce(Velocity, ForceMode.VelocityChange);
		base.GetComponent<Rigidbody>().AddTorque(AngularVelocity, ForceMode.VelocityChange);
		base.GetComponent<Rigidbody>().useGravity = true;
		AudioSource component = base.GetComponent<AudioSource>();
		int num4 = (int)this.m_SoundThreshold.Evaluate(Velocity.magnitude);
		num4 = ((num4 >= 0) ? num4 : 0);
		if (num4 == 0)
		{
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0091.ogg", false);
		}
		else if (num4 == 1)
		{
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0092.ogg", false);
		}
		else if (num4 == 2)
		{
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0093.ogg", false);
		}
		else
		{
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0094.ogg", false);
		}
		if (num4 == 0)
		{
			this.m_IsDropped = true;
		}
		else
		{
			VRMiniGameManager.Instance.life--;
			this.m_IsJudgeScore = true;
		}
	}

	private Vector3 CalcThrowVec(Vector3[] VecArray)
	{
		Vector3 vector = Vector3.zero;
		for (int i = 0; i < VecArray.Length; i++)
		{
			vector += VecArray[i];
		}
		if (VecArray.Length > 0)
		{
			vector /= (float)VecArray.Length;
		}
		Vector3 result = vector;
		if (VecArray.Length > 1)
		{
			result = result.normalized * VecArray[VecArray.Length - 1].magnitude;
		}
		return result;
	}

	private Vector3[] GetDeviationValue(Vector3[] array)
	{
		List<Vector3> list = new List<Vector3>();
		list.Add(array[0]);
		for (int i = 1; i < array.Length; i++)
		{
			if (Vector3.Dot(array[i], array[0]) >= 0f)
			{
				list.Add(array[i]);
			}
		}
		Vector3[] array2 = list.ToArray();
		float[] array3 = new float[array2.Length];
		for (int j = 0; j < array3.Length; j++)
		{
			Vector3 vector = array2[j];
			array3[j] = Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
		}
		float num = 0f;
		for (int k = 0; k < array3.Length; k++)
		{
			num += array3[k];
		}
		num /= (float)array3.Length;
		float[] array4 = new float[array3.Length];
		for (int l = 0; l < array4.Length; l++)
		{
			float num2 = num - array3[l];
			array4[l] = num2 * num2;
		}
		float num3 = 0f;
		for (int m = 0; m < array4.Length; m++)
		{
			num3 += array4[m];
		}
		num3 /= (float)array4.Length;
		float num4 = Mathf.Sqrt(num3);
		float[] array5 = new float[array4.Length];
		for (int n = 0; n < array5.Length; n++)
		{
			float num5 = (array3[n] - num) * 10f;
			num5 /= num4;
			num5 = 50f + num5;
			array5[n] = num5;
		}
		list.Clear();
		int num6 = 0;
		for (int num7 = 0; num7 < array5.Length; num7++)
		{
			if (array5[num7] >= 50f)
			{
				list.Add(array2[num7]);
				num6++;
			}
		}
		return list.ToArray();
	}

	private Vector3[] m_VelocityArray = new Vector3[9];

	private Vector3[] m_EulerAngleArray = new Vector3[9];

	[SerializeField]
	[Tooltip("投げた時の力の倍率。")]
	private float m_Power = 1f;

	[SerializeField]
	[Tooltip("掴んだ相手との距離。\nメートル単位で指定。")]
	private float m_Magnitude = 0.1f;

	[SerializeField]
	[Tooltip("スコア判定を行うためのゲームオブジェクト。\nトランスフォームのパラメータがBoxColliderとして働きます。")]
	private GameObject m_ColliderJudgeScore;

	[SerializeField]
	[Range(0f, 1000f)]
	[Tooltip("投げた時にターゲットへ向かう補正の力。")]
	private float m_DebugPower;

	[Space(16f)]
	[SerializeField]
	[Tooltip("投げた時の力を、効果音のインデックスに対応させる。\n横軸：投げる力\n縦軸：対応する効果音のインデックス\n効果音は Sound Throwing が使われます。")]
	private AnimationCurve m_SoundThreshold;

	private bool m_IsJudgeScore;

	private int m_GrabNum;

	private GameObject m_TargetObject;

	[SerializeField]
	private ParticleSystem m_Particle;

	[SerializeField]
	private Transform m_Pivot;

	private AVRControllerButtons m_VRController;

	private bool m_IsGrab = true;

	private bool m_IsDropped;

	private bool m_IsRespawn;
}
