using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class PaperAirplaneManager : MonoBehaviour
{
	private int totalScore
	{
		get
		{
			return this.miniGameManager.score / 10;
		}
		set
		{
			this.miniGameManager.ScoreBoard.score = value * 10;
		}
	}

	private void Awake()
	{
		this.playBGMName = Path.ChangeExtension(this.playBGMName, "ogg");
		this.throwPlaySeName = Path.ChangeExtension(this.throwPlaySeName, "ogg");
		this.throwMissPlaySeName = Path.ChangeExtension(this.throwMissPlaySeName, "ogg");
		this.explosionSeName = Path.ChangeExtension(this.explosionSeName, "ogg");
	}

	private void Start()
	{
		this.targetObject.onEndAnimationVanish = new Action(this.OnEndAnimationVanish);
		base.StartCoroutine(this.WaitVrStart());
	}

	private IEnumerator WaitVrStart()
	{
		while (GameMain.Instance.OvrMgr == null)
		{
			yield return null;
		}
		yield return null;
		GameMain.Instance.MainCamera.FadeIn(0.5f, false, null, true, true, default(Color));
		base.StartCoroutine(this.WaitFadeJob());
		yield break;
	}

	private IEnumerator WaitFadeJob()
	{
		while (GameMain.Instance.MainCamera.IsFadeProc())
		{
			yield return null;
		}
		this.AwakeMiniGame();
		yield break;
	}

	public void AwakeMiniGame()
	{
		GameMain.Instance.SoundMgr.PlayBGM(this.playBGMName, 0.5f, true);
		Vector3 vector = this.playerPosition.position;
		this.useCamera = GameMain.Instance.OvrMgr.OvrCamera;
		vector += this.playerPosition.forward * this.playerPositionDistance;
		vector = new Vector3(vector.x, this.useCamera.GetHeightStandAndSit(CameraMain.STAND_SIT.STAND), vector.z);
		this.useCamera.SetPos(vector);
		this.useCamera.SetRotation(Quaternion.LookRotation(this.appearancePosition[0].position - vector).eulerAngles);
		vector = this.maidPosition.position;
		GameMain.Instance.CharacterMgr.SetCharaAllPos(vector);
		Vector3 charaAllRot = new Vector3(0f, Quaternion.LookRotation(this.useCamera.GetPos() - vector).eulerAngles.y, 0f);
		GameMain.Instance.CharacterMgr.SetCharaAllRot(charaAllRot);
		this.is2DMode = false;
		if (this.devideType == GameMain.VRDeviceType.NON || this.devideType == GameMain.VRDeviceType.RIFT || this.devideType == GameMain.VRDeviceType.FOVE)
		{
			this.is2DMode = true;
		}
		this.useCamera.ForceHideUI(true);
		this.gaugeManager.ChangeDeviceMode(this.devideType);
		this.miniGameManager = VRMiniGameManager.Instance;
		VRMiniGameManager.VRMiniGameType type = VRMiniGameManager.VRMiniGameType.Kamihikouki;
		this.miniGameManager.Init(type);
		this.miniGameManager.CreateScoreBoard(this.scoreＢoardPosition.position, Quaternion.LookRotation(this.scoreＢoardPosition.position - this.useCamera.GetPos()));
		this.miniGameManager.ScoreBoard.gameObject.SetActive(false);
		if (!this.is2DMode)
		{
			Vector3 vector2 = this.useCamera.GetPos();
			vector2 -= Vector3.right * 1.5f;
			Quaternion uirot = Quaternion.LookRotation(vector2 - this.useCamera.GetPos());
			this.miniGameManager.StartRightLeftHand(vector2, uirot, delegate(AVRController con)
			{
				this.StartMiniGame(!con.m_bHandL);
			});
		}
		else
		{
			this.StartMiniGame(true);
		}
	}

	public void StartMiniGame(bool main_hand_right)
	{
		if (!this.is2DMode)
		{
			this.useCtrl = GameMain.Instance.OvrMgr.OvrCamera.GetVRControllerButtons(!main_hand_right);
			if (this.devideType == GameMain.VRDeviceType.RIFT_TOUCH)
			{
				this.grabber.SetGripTransform((!main_hand_right) ? "Rift_Left" : "Rift_Right");
			}
			else
			{
				this.grabber.SetGripTransform("HTC");
			}
		}
		else
		{
			this.useCtrl = this.dummyController;
			if (this.devideType == GameMain.VRDeviceType.NON)
			{
				this.grabber.SetGripTransform("2D");
			}
			else
			{
				this.grabber.SetGripTransform("Rift_Non");
			}
		}
		this.grabber.m_vrController = this.useCtrl;
		this.throwObject.onThrowObjectEvent = new WfGrabbableObject.OnThrowObjectEvent(this.OnThrowObject);
		this.throwObject.onCollisionEnter = new Action<WfGrabbableObject, Collision>(this.OnThrowObjectCollisionEnter);
		this.throwObject.onThrowObjectCorrectionVelocityEvent = new WfGrabbableObject.OnThrowObjectCorrectionVelocityEvent(this.OnThrowObjectCorrectionVelocity);
		int totalScore = 0;
		this.totalScore = totalScore;
		this.throwCount = totalScore;
		this.addPoint = 1;
		this.contactTime = 0f;
		this.throwFlag = (this.mistakeThrow = (this.isFinish = false));
		this.StopVoice();
		this.miniGameManager.ScoreBoard.gameObject.SetActive(true);
		this.gaugeManager.Stop();
		this.gaugeManager.onMouseClickDownEvent = delegate(VRMiniGameGaugeeManager eventObject)
		{
			if (this.gaugeManager.isPlay || !this.throwObject.IsGrabbed)
			{
				return;
			}
			this.gaugeManager.Play();
		};
		this.gaugeManager.onMouseClickUpEvent = new VRMiniGameGaugeeManager.OnMouseClickEvent(this.onMouseClickUpEvent);
		this.gaugeManager.enabledMouseClickEvent = true;
		this.GrabPaperAirplane();
		this.PopupTargetObject();
	}

	private void PopupTargetObject()
	{
		this.PopupTargetObject(this.throwCount);
	}

	private void PopupTargetObject(int throwCount)
	{
		if (0 <= throwCount && throwCount <= 10)
		{
			VRMiniGameManager.VoiceType type = (VRMiniGameManager.VoiceType)Enum.Parse(typeof(VRMiniGameManager.VoiceType), "Remaining" + (10 - throwCount).ToString());
			Debug.Log("throwCountボイス再生:" + throwCount);
			this.miniGameManager.AddQueueVoice(type);
		}
		Debug.Log(throwCount + 1 + "回目の投擲です");
		Vector3 pos = Vector3.zero;
		if (0 <= throwCount && throwCount < this.appearancePosition.Length)
		{
			Transform transform = this.appearancePosition[throwCount];
			int childCount = transform.childCount;
			pos = transform.GetChild(UnityEngine.Random.Range(0, childCount - 1)).position;
		}
		else
		{
			Debug.LogError("投擲回数は" + throwCount + "回目ですが、的位置の設定が見当たりません");
			pos = Vector3.zero;
		}
		this.targetObject.PopupPosition(pos);
	}

	private void GrabPaperAirplane()
	{
		Physics.gravity = this.customGravity;
		this.throwFlag = (this.mistakeThrow = false);
		this.contactTime = 0f;
		Rigidbody component = this.grabber.GetComponent<Rigidbody>();
		Rigidbody rigidbody = component;
		Vector3 zero = Vector3.zero;
		component.angularVelocity = zero;
		rigidbody.velocity = zero;
		this.throwObject.rigidBody.isKinematic = false;
		this.throwObject.rigidBody.useGravity = true;
		this.throwObject.StopLookRotationAnime();
		this.throwObject.transform.SetParent(base.transform, false);
		this.grabber.GrabBegin(this.throwObject);
	}

	private void OnThrowObjectCorrectionVelocity(ref Vector3 linearVelocity, ref Vector3 angularVelocity)
	{
		this.targetObject.OnThrowObjectCorrectionVelocity(this.throwObject.transform, ref linearVelocity, ref angularVelocity);
	}

	private void OnThrowObject(WfGrabbableObject throwObject, bool mistakeThrow)
	{
		this.throwFlag = true;
		this.mistakeThrow = mistakeThrow;
		if (!mistakeThrow)
		{
			this.throwCount++;
			this.PlaySe(this.throwPlaySeName);
		}
		else
		{
			this.PlaySe(this.throwMissPlaySeName);
		}
	}

	private void OnThrowObjectCollisionEnter(WfGrabbableObject throwObject, Collision collision)
	{
		if (!this.throwFlag)
		{
			return;
		}
		this.throwFlag = false;
		Physics.gravity = new Vector3(0f, -9.81f, 0f);
		throwObject.StopLookRotationAnime();
		if (collision.gameObject.layer == PaperAirplaneManager.MiniGameObjectLayerNumber)
		{
			throwObject.rigidBody.isKinematic = true;
			throwObject.rigidBody.useGravity = false;
			Rigidbody rigidBody = throwObject.rigidBody;
			Vector3 zero = Vector3.zero;
			throwObject.rigidBody.angularVelocity = zero;
			rigidBody.velocity = zero;
			Transform transform = collision.transform;
			while (transform != null)
			{
				PaperAirplaneTarget component = transform.GetComponent<PaperAirplaneTarget>();
				if (component != null)
				{
					component.Die();
					throwObject.transform.SetParent(component.modelTransform, true);
					break;
				}
				transform = transform.parent;
			}
			this.OnHit();
		}
		else if (this.mistakeThrow)
		{
			this.contactTime = Time.fixedTime;
		}
		else
		{
			this.OnMiss();
		}
	}

	private void OnHit()
	{
		this.PlaySe(this.explosionSeName);
		this.miniGameManager.AddQueueVoice(VRMiniGameManager.VoiceType.Hit);
		this.totalScore += this.addPoint;
		this.addPoint++;
		this.miniGameManager.ScoreBoard.life--;
	}

	private void OnMiss()
	{
		this.miniGameManager.AddQueueVoice(VRMiniGameManager.VoiceType.Miss);
		this.targetObject.Vanish();
		this.addPoint = 1;
		this.miniGameManager.ScoreBoard.life--;
	}

	private void OnEndAnimationVanish()
	{
		if (this.throwCount >= 10)
		{
			Physics.gravity = this.backupGravity;
			this.isFinish = true;
			this.gaugeManager.isVisible = false;
			this.miniGameManager.AddQueueVoice(VRMiniGameManager.VoiceType.Remaining0);
			this.miniGameManager.Result(null);
		}
		else
		{
			this.GrabPaperAirplane();
			this.PopupTargetObject();
		}
	}

	private void OnDestroy()
	{
		Physics.gravity = this.backupGravity;
	}

	private void FixedUpdate()
	{
		if (this.useCamera == null || this.isFinish)
		{
			return;
		}
		if (this.is2DMode)
		{
			this.dummyController.transform.position = this.useCamera.m_goCenterEye.transform.position + this.useCamera.m_goCenterEye.transform.forward * 0.2f;
			this.dummyController.transform.rotation = Quaternion.LookRotation(this.dummyController.transform.position - this.useCamera.m_goCenterEye.transform.position);
		}
	}

	private void onMouseClickUpEvent(VRMiniGameGaugeeManager eventObject)
	{
		if (!this.gaugeManager.isPlay || !this.throwObject.IsGrabbed)
		{
			return;
		}
		Vector3 a = this.dummyController.transform.position - this.useCamera.m_goCenterEye.transform.position;
		Vector3 eulerAngles = Quaternion.LookRotation(this.dummyController.transform.position - this.useCamera.m_goCenterEye.transform.position).eulerAngles;
		float d = this.gaugeRangeMin + (this.gaugeRangeMax - this.gaugeRangeMin) * this.gaugeManager.gaugeValue;
		this.grabber.GrabEnd(a * d, eulerAngles);
		this.gaugeManager.Stop();
	}

	private void Update()
	{
		if (this.useCamera == null || this.useCtrl == null || this.isFinish)
		{
			return;
		}
		if (this.throwObject.transform.position.y <= -3f)
		{
			if (-1000f < this.throwObject.transform.position.y)
			{
				if (!this.mistakeThrow)
				{
					this.throwObject.transform.position = new Vector3(0f, -1000f, 0f);
					this.throwObject.rigidBody.isKinematic = false;
					this.throwObject.rigidBody.useGravity = true;
					this.throwObject.StopLookRotationAnime();
					this.OnMiss();
				}
				else
				{
					this.GrabPaperAirplane();
				}
			}
		}
		else if (0f < this.contactTime && Time.fixedTime - this.contactTime >= 1.5f)
		{
			this.GrabPaperAirplane();
		}
	}

	private void PlaySe(string seFileName)
	{
		if (!string.IsNullOrEmpty(seFileName) && GameMain.Instance != null && GameMain.Instance.SoundMgr != null)
		{
			GameMain.Instance.SoundMgr.PlaySe(seFileName, false);
		}
	}

	private void StopVoice()
	{
		GameMain.Instance.SoundMgr.VoiceStopAll();
	}

	private GameMain.VRDeviceType devideType
	{
		get
		{
			return GameMain.Instance.VRDeviceTypeID;
		}
	}

	public static readonly int MiniGameObjectLayerNumber = 24;

	[SerializeField]
	public Vector3 customGravity = Physics.gravity;

	[SerializeField]
	private VRGrabber grabber;

	[SerializeField]
	private WfGrabbableObject throwObject;

	[SerializeField]
	private PaperAirplaneTarget targetObject;

	[SerializeField]
	private Transform[] appearancePosition;

	[SerializeField]
	[Tooltip("主人公の基準位置")]
	private Transform playerPosition;

	[SerializeField]
	[Tooltip("主人公の基準位置からの距離")]
	private float playerPositionDistance;

	[SerializeField]
	[Tooltip("メイドの位置")]
	private Transform maidPosition;

	[SerializeField]
	[Tooltip("スコアボードの位置")]
	private Transform scoreＢoardPosition;

	[SerializeField]
	[Tooltip("再生BGM")]
	private string playBGMName = "BGM_vr0002";

	[SerializeField]
	[Tooltip("投擲成功の時呼ばれるSE")]
	private string throwPlaySeName = "SE_vrminigame003_01";

	[SerializeField]
	[Tooltip("投擲失敗の時呼ばれるSE")]
	private string throwMissPlaySeName = "SE_vrminigame003_01";

	[SerializeField]
	[Tooltip("対象にhitした時呼ばれるSE")]
	private string explosionSeName = string.Empty;

	[SerializeField]
	[Tooltip("2D用ダミーコントローラー")]
	private AVRControllerButtons dummyController;

	[SerializeField]
	[Tooltip("2D用ゲージマネージャー")]
	private VRMiniGameGaugeeManager gaugeManager;

	[SerializeField]
	[Tooltip("2D用ゲージの最小値")]
	private float gaugeRangeMin;

	[SerializeField]
	[Tooltip("2D用ゲージの最大値")]
	private float gaugeRangeMax;

	private OvrCamera useCamera;

	private AVRControllerButtons useCtrl;

	private VRMiniGameManager miniGameManager;

	private int throwCount;

	private bool throwFlag;

	private bool mistakeThrow;

	private float contactTime;

	private int addPoint = 1;

	private bool is2DMode;

	private Vector3 backupGravity = Physics.gravity;

	private bool isFinish;
}
