using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class VRMiniGameManager : MonoBehaviour
{
	public static VRMiniGameManager Instance
	{
		get
		{
			if (VRMiniGameManager.m_Instance == null)
			{
				VRMiniGameManager.m_Instance = UnityEngine.Object.FindObjectOfType<VRMiniGameManager>();
			}
			return VRMiniGameManager.m_Instance;
		}
	}

	public VRMiniGameScoreBoard ScoreBoard
	{
		get
		{
			return this.m_MiniGameScoreBoard;
		}
	}

	public int score
	{
		get
		{
			return this.m_MiniGameScoreBoard.score;
		}
		set
		{
			this.m_MiniGameScoreBoard.score = value;
		}
	}

	public int life
	{
		get
		{
			return this.m_MiniGameScoreBoard.life;
		}
		set
		{
			this.m_MiniGameScoreBoard.life = value;
		}
	}

	public AVRControllerButtons VRController
	{
		get
		{
			return this.m_VRController;
		}
		set
		{
			this.m_VRController = value;
		}
	}

	public VRMiniGameManager.VRMiniGameState MiniGameState
	{
		get
		{
			return this.m_MiniGameState;
		}
	}

	private void Start()
	{
		VRMiniGameManager.m_Instance = this;
	}

	public void Init(VRMiniGameManager.VRMiniGameType type)
	{
		if (this.m_MiniGameState != VRMiniGameManager.VRMiniGameState.None)
		{
			return;
		}
		Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
		this.callScriptFile = ((!(maid == null)) ? ScriptManager.ReplacePersonal(maid, this.callScriptFile) : this.callScriptFile);
		string str = this.callScriptFile;
		GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(str + ".ks", string.Empty);
		Dictionary<string, string> tag_backup = GameMain.Instance.ScriptMgr.adv_kag.tag_backup;
		string scriptArgFileEnd;
		string scriptArgLabelEnd;
		if (tag_backup != null && tag_backup.TryGetValue("file", out scriptArgFileEnd) && tag_backup.TryGetValue("label", out scriptArgLabelEnd))
		{
			this.m_ScriptArgLabelEnd = scriptArgLabelEnd;
			this.m_ScriptArgFileEnd = scriptArgFileEnd;
		}
		GameMain.Instance.OvrMgr.OvrCamera.HandLimitMode = AVRControllerBehavior.LIMIT_MODE.HAND_ONLY;
		this.m_MiniGameType = type;
		GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
		if (vrdeviceTypeID == GameMain.VRDeviceType.NON || vrdeviceTypeID == GameMain.VRDeviceType.RIFT || vrdeviceTypeID == GameMain.VRDeviceType.FOVE)
		{
			this.m_MiniGameState = VRMiniGameManager.VRMiniGameState.Playing;
		}
		else
		{
			this.m_MiniGameState = VRMiniGameManager.VRMiniGameState.Setup;
		}
	}

	public void CreateScoreBoard(Vector3 ScoreBoardPosition, Quaternion ScoreBoardRot)
	{
		this.m_MiniGameScoreBoard = VRMiniGameScoreBoard.Create(ScoreBoardPosition, ScoreBoardRot);
		this.m_MiniGameScoreBoard.transform.position = ScoreBoardPosition;
		this.m_MiniGameScoreBoard.transform.rotation = ScoreBoardRot;
	}

	public void StartRightLeftHand(Vector3 UIPos, Quaternion UIRot, UnityAction<AVRController> callback)
	{
		GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
		if (vrdeviceTypeID != GameMain.VRDeviceType.RIFT_TOUCH && vrdeviceTypeID != GameMain.VRDeviceType.VIVE)
		{
			this.ClearQueueVoice();
			this.m_MiniGameState = VRMiniGameManager.VRMiniGameState.Playing;
			callback(null);
			return;
		}
		VRDecideMiniGameHand ui = VRDecideMiniGameHand.Create(UIPos, UIRot);
		callback = (UnityAction<AVRController>)Delegate.Combine(callback, new UnityAction<AVRController>(delegate(AVRController controller)
		{
			if (controller)
			{
				this.m_VRController = controller.VRControllerButtons;
			}
			ui.textCountDown.text = "Start!";
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0032.ogg", false);
			this.ClearQueueVoice();
			this.m_MiniGameState = VRMiniGameManager.VRMiniGameState.Playing;
			UICanvasFade component = ui.GetComponent<UICanvasFade>();
			component.FadeOut(0.5f, false, delegate()
			{
				UnityEngine.Object.Destroy(ui.gameObject);
			});
		}));
		ui.StartDecideHand(callback);
	}

	public void Result(UnityAction callback)
	{
		if (this.m_MiniGameState == VRMiniGameManager.VRMiniGameState.Result)
		{
			return;
		}
		this.m_MiniGameState = VRMiniGameManager.VRMiniGameState.Result;
		base.StartCoroutine(this.Coroutine_WaitVoice(delegate
		{
			this.ResultJudge(callback);
		}));
	}

	private void ResultJudge(UnityAction callback)
	{
		int num = 0;
		if (this.m_MiniGameType == VRMiniGameManager.VRMiniGameType.Wanage)
		{
			num = 1;
		}
		else if (this.m_MiniGameType == VRMiniGameManager.VRMiniGameType.Darts)
		{
			num = 2;
		}
		else if (this.m_MiniGameType == VRMiniGameManager.VRMiniGameType.Kamihikouki)
		{
			num = 3;
		}
		GameMain.Instance.OvrMgr.OvrCamera.ForceHideUI(false);
		GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
		if (vrdeviceTypeID != GameMain.VRDeviceType.NON)
		{
			GameMain.Instance.OvrMgr.OvrCamera.UIPosReset(0f);
		}
		VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.NONE);
		this.m_MiniGameScoreBoard.transform.SetParent(VRCanvasManager.Instance.CanvasParent.transform, false);
		this.m_MiniGameScoreBoard.transform.localPosition = Vector3.zero;
		this.m_MiniGameScoreBoard.transform.localScale = Vector3.one * 0.001f;
		this.m_MiniGameScoreBoard.transform.localRotation = Quaternion.identity;
		this.m_MiniGameScoreBoard.GetComponent<CanvasGroup>().interactable = true;
		Camera componentInChildren = EventSystem.current.gameObject.GetComponentInChildren<Camera>();
		this.m_MiniGameScoreBoard.GetComponent<Canvas>().worldCamera = componentInChildren;
		List<VRMiniGameManager.MiniGameResultData> list = new List<VRMiniGameManager.MiniGameResultData>();
		string text = "vrcom_mini_game.nei";
		if (!GameUty.FileSystem.IsExistentFile(text))
		{
			NDebug.Assert("表がありません。" + text, false);
		}
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, text + "\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i))
					{
						int num2 = 0;
						int cellAsInteger = csvParser.GetCellAsInteger(num2++, i);
						if (cellAsInteger == num)
						{
							int cellAsInteger2 = csvParser.GetCellAsInteger(num2++, i);
							int cellAsInteger3 = csvParser.GetCellAsInteger(num2++, i);
							string cellAsString = csvParser.GetCellAsString(num2++, i);
							string cellAsString2 = csvParser.GetCellAsString(num2++, i);
							string cellAsString3 = csvParser.GetCellAsString(num2++, i);
							list.Add(new VRMiniGameManager.MiniGameResultData(cellAsInteger2, cellAsInteger3, cellAsString, cellAsString3, cellAsInteger3 <= this.score, cellAsString2));
						}
					}
				}
			}
		}
		string message = string.Empty;
		bool flag = false;
		for (int j = 0; j < list.Count; j++)
		{
			if (list[j].IsGet)
			{
				flag = true;
				break;
			}
		}
		bool flag2 = false;
		if (this.m_MiniGameType == VRMiniGameManager.VRMiniGameType.Wanage)
		{
			flag2 = (this.score == 10);
		}
		else if (this.m_MiniGameType == VRMiniGameManager.VRMiniGameType.Darts)
		{
			flag2 = (this.score == 600);
		}
		else if (this.m_MiniGameType == VRMiniGameManager.VRMiniGameType.Kamihikouki)
		{
			flag2 = (this.score == 550);
		}
		List<VRMiniGameManager.MiniGameResultData> list2 = new List<VRMiniGameManager.MiniGameResultData>();
		for (int k = 0; k < list.Count; k++)
		{
			VRMiniGameManager.MiniGameResultData miniGameResultData = list[k];
			if (miniGameResultData.IsGet)
			{
				if (!string.IsNullOrEmpty(miniGameResultData.FileName))
				{
					bool flag3 = false;
					string[] array = miniGameResultData.FileName.Split(new char[]
					{
						','
					});
					for (int l = 0; l < array.Length; l++)
					{
						if (GameMain.Instance.CharacterMgr.status.IsHavePartsItem(array[l] + ".menu"))
						{
							Debug.Log("[ミニゲーム\u3000リザルト]" + array[l] + "は既に持っている");
							flag3 = true;
						}
					}
					if (!flag3)
					{
						list2.Add(miniGameResultData);
					}
				}
			}
		}
		VRMiniGameManager.VoiceType voiceType = VRMiniGameManager.VoiceType.Result4;
		if (!flag)
		{
			voiceType = VRMiniGameManager.VoiceType.Result4;
			message = "何も獲得できませんでした……";
		}
		else if (list2.Count > 0)
		{
			voiceType = VRMiniGameManager.VoiceType.Result2;
			if (list2.Count == 1)
			{
				message = "衣装「" + list2[0].ItemName + "」をゲットしました！";
			}
			else
			{
				message = list2.Count.ToString() + "つの衣装をゲットしました！";
			}
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0033.ogg", false);
		}
		else if (!flag2)
		{
			voiceType = VRMiniGameManager.VoiceType.Result3;
			message = "今回の点数は" + this.score.ToString() + "点でした。";
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0032.ogg", false);
		}
		else
		{
			voiceType = VRMiniGameManager.VoiceType.Result1;
			message = "最高点数！今回の点数は" + this.score + "点でした！";
			GameMain.Instance.SoundMgr.PlaySe("SE_vr0034.ogg", false);
		}
		for (int m = 0; m < list2.Count; m++)
		{
			VRMiniGameManager.MiniGameResultData miniGameResultData2 = list2[m];
			if (!string.IsNullOrEmpty(miniGameResultData2.FileName))
			{
				string[] array2 = miniGameResultData2.FileName.Split(new char[]
				{
					','
				});
				for (int n = 0; n < array2.Length; n++)
				{
					GameMain.Instance.CharacterMgr.status.AddHavePartsItem(array2[n] + ".menu");
				}
			}
		}
		if (flag)
		{
			for (int num3 = 0; num3 < list.Count; num3++)
			{
				if (list[num3].ItemScore <= this.score)
				{
					if (!string.IsNullOrEmpty(list[num3].ItemFlag))
					{
						GameMain.Instance.CharacterMgr.status.SetFlag(list[num3].ItemFlag, 1);
					}
				}
			}
		}
		callback = (UnityAction)Delegate.Combine(callback, new UnityAction(delegate()
		{
			this.AddQueueVoice(voiceType);
			this.FinishMiniGame();
		}));
		this.m_MiniGameScoreBoard.Result(message, callback);
	}

	public void FinishMiniGame()
	{
		GameMain.Instance.OvrMgr.OvrCamera.HandLimitMode = AVRControllerBehavior.LIMIT_MODE.NORMAL;
		UnityEngine.Object.Destroy(this.m_MiniGameScoreBoard.gameObject);
		GameMain.Instance.OvrMgr.OvrCamera.ShowUI(false);
		VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
		this.m_MiniGameState = VRMiniGameManager.VRMiniGameState.None;
		base.StartCoroutine(this.Coroutine_WaitVoice(delegate
		{
			if (!string.IsNullOrEmpty(this.m_ScriptArgFileEnd) && !string.IsNullOrEmpty(this.m_ScriptArgLabelEnd))
			{
				Debug.Log("OnEndScene JumpTo " + this.m_ScriptArgLabelEnd);
				GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(this.m_ScriptArgFileEnd + ".ks", string.Empty);
				GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(this.m_ScriptArgLabelEnd);
				GameMain.Instance.ScriptMgr.adv_kag.Exec();
			}
		}));
	}

	private IEnumerator Coroutine_WaitVoice(UnityAction callback)
	{
		while (GameMain.Instance.SoundMgr.isVoicePlaying())
		{
			yield return null;
		}
		yield return new WaitForSeconds(0.75f);
		callback();
		yield break;
	}

	private void Update()
	{
		if (0 < this.voiceQueueList.Count && !GameMain.Instance.SoundMgr.isVoicePlaying())
		{
			string label_name = this.voiceQueueList[0];
			this.voiceQueueList.RemoveAt(0);
			GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(this.callScriptFile + ".ks", label_name);
			GameMain.Instance.ScriptMgr.adv_kag.Exec();
			this.voicePlayTime = Time.time;
		}
		if (this.voiceQueueList.Count == 0 && !GameMain.Instance.SoundMgr.isVoicePlaying() && this.voicePlayTime + this.voiceWaitTime <= Time.time && this.m_MiniGameState == VRMiniGameManager.VRMiniGameState.Playing)
		{
			this.AddQueueVoice(VRMiniGameManager.VoiceType.Wait);
			this.voicePlayTime = Time.time + UnityEngine.Random.Range(0f, this.voiceWaitTime / 2f);
		}
	}

	public void AddQueueVoice(VRMiniGameManager.VoiceType type)
	{
		Debug.Log("[ ミニゲームマネージャ ]ボイス：" + type);
		string text = string.Empty;
		switch (type)
		{
		case VRMiniGameManager.VoiceType.Wait:
			text = "*game_random";
			break;
		case VRMiniGameManager.VoiceType.Hit:
			text = "*good_random";
			break;
		case VRMiniGameManager.VoiceType.Miss:
			text = "*bad_random";
			break;
		case VRMiniGameManager.VoiceType.Result1:
			text = "*result_1";
			break;
		case VRMiniGameManager.VoiceType.Result2:
			text = "*result_2";
			break;
		case VRMiniGameManager.VoiceType.Result3:
			text = "*result_3";
			break;
		case VRMiniGameManager.VoiceType.Result4:
			text = "*result_4";
			break;
		default:
			switch (type)
			{
			case VRMiniGameManager.VoiceType.StartKamihikouki:
				text = "*kamihikouki_start";
				break;
			case VRMiniGameManager.VoiceType.StartDarts:
				text = "*darts_start";
				break;
			case VRMiniGameManager.VoiceType.StartWanage:
				text = "*wanage_start";
				break;
			}
			break;
		}
		if (VRMiniGameManager.VoiceType.Remaining0 <= type && type <= VRMiniGameManager.VoiceType.Remaining10)
		{
			text = "*remaining_" + ((int)(VRMiniGameManager.VoiceType.Remaining7 - (VRMiniGameManager.VoiceType.Remaining10 - type))).ToString();
		}
		else
		{
			this.StopVoice();
			this.ClearQueueVoice();
		}
		if (string.IsNullOrEmpty(text))
		{
			Debug.LogError("ミニゲームにてAddQueueVoice関数が呼ばれましたが、タグの指定が不明です");
			return;
		}
		if (!GameMain.Instance.SoundMgr.isVoicePlaying() && this.voiceQueueList.Count == 0)
		{
			GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(this.callScriptFile + ".ks", text);
			GameMain.Instance.ScriptMgr.adv_kag.Exec();
		}
		else
		{
			this.voiceQueueList.Add(text);
		}
	}

	private void ClearQueueVoice()
	{
		this.voiceQueueList.Clear();
	}

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

	private static VRMiniGameManager m_Instance;

	private VRMiniGameScoreBoard m_MiniGameScoreBoard;

	private AVRControllerButtons m_VRController;

	private VRMiniGameManager.VRMiniGameType m_MiniGameType;

	private VRMiniGameManager.VRMiniGameState m_MiniGameState;

	private string m_ScriptArgLabelEnd = string.Empty;

	private string m_ScriptArgFileEnd = string.Empty;

	[SerializeField]
	[Tooltip("メイドスクリプトファイル")]
	private string callScriptFile = "001_?_minigame";

	private List<string> voiceQueueList = new List<string>();

	private float voicePlayTime;

	private float voiceWaitTime = 10f;

	public enum VRMiniGameType
	{
		Wanage,
		Darts,
		Kamihikouki
	}

	public enum VRMiniGameState
	{
		None,
		Setup,
		Playing,
		Result
	}

	private class MiniGameResultData
	{
		public MiniGameResultData(int itemID, int itemScore, string fileName, string itemFlag, bool isGet, string itemName)
		{
			this.ItemID = itemID;
			this.ItemScore = itemScore;
			this.FileName = fileName;
			this.ItemFlag = itemFlag;
			this.IsGet = isGet;
			this.ItemName = itemName;
		}

		public int ItemID;

		public int ItemScore;

		public string FileName;

		public string ItemFlag;

		public bool IsGet;

		public string ItemName;
	}

	public enum VoiceType
	{
		StartKamihikouki,
		StartDarts,
		StartWanage,
		Remaining0,
		Remaining1,
		Remaining2,
		Remaining3,
		Remaining4,
		Remaining5,
		Remaining6,
		Remaining7,
		Remaining8,
		Remaining9,
		Remaining10,
		Wait,
		Hit,
		Miss,
		Result1,
		Result2,
		Result3,
		Result4
	}
}
