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

public class VRChoices : MonoBehaviour
{
	private void Start()
	{
		this.m_EventText = GameMain.Instance.OvrMgr.OvrCamera.OvrEventText;
		NDebug.Assert(this.m_EventText != null, "EventTextが取得できませんでした。");
	}

	public void Add(string f_strText, string f_strLabel, int f_nMaidNo, VRChoices.SelectPair.Type f_type, string f_strRegionSplitOfColon, float f_fTimeOut)
	{
		ushort num = 0;
		if (!string.IsNullOrEmpty(f_strRegionSplitOfColon))
		{
			VRChoices.<Add>c__AnonStorey0 <Add>c__AnonStorey = new VRChoices.<Add>c__AnonStorey0();
			<Add>c__AnonStorey.strSplit = f_strRegionSplitOfColon.Split(new char[]
			{
				':'
			});
			int nS;
			for (nS = 0; nS < <Add>c__AnonStorey.strSplit.Length; nS++)
			{
				int num2 = Array.FindIndex<KeyValuePair<string, VRChoices.Region>>(this.m_RegionPair, (KeyValuePair<string, VRChoices.Region> a) => a.Key == <Add>c__AnonStorey.strSplit[nS]);
				if (num2 != -1)
				{
					num |= (ushort)this.m_RegionPair[num2].Value;
				}
				else
				{
					num |= (ushort)this.m_RegionPair[0].Value;
					Debug.LogError("選択肢個所：" + <Add>c__AnonStorey.strSplit[nS] + " はありません。");
				}
			}
			if (f_nMaidNo == -1)
			{
				f_nMaidNo = 0;
			}
		}
		if (GameMain.Instance.OvrMgr.OvrCamera.IsNoHandController && f_type == VRChoices.SelectPair.Type.Touch)
		{
			f_type = VRChoices.SelectPair.Type.TouchToLook;
		}
		this.m_listPair.Add(new VRChoices.SelectPair(f_strText, f_strLabel, f_nMaidNo, f_type, num, f_fTimeOut));
	}

	public void Clear()
	{
		if (this.m_TouchMgr != null)
		{
			this.m_TouchMgr.Clear();
		}
		if (this.m_LookMgr != null)
		{
			this.m_LookMgr.Clear();
		}
		this.m_listPair.Clear();
		this.m_dicMaidLookTouch.Clear();
		this.m_TextSelectPair.Clear();
		this.TouchLookTimeCountDownStop();
		this.m_bTimeOutCounting = false;
		this.m_nBackMaidNo = -1;
		this.m_BackRegion = VRChoices.Region.Non;
		this.m_BackType = VRChoices.SelectPair.Type.Text;
		this.m_PairNow = null;
		GameMain.Instance.OvrMgr.OvrCamera.HideDummyHand(OvrCamera.DUMMY_HAND.CHOICES_TOUCH);
		if (this.m_SelectorText != null)
		{
			this.m_SelectorText.SelectFinalize(null);
			this.m_SelectorText = null;
		}
	}

	public void Show(int f_nTimeOutMS, string f_strTimeOutLabel, int f_nRandom)
	{
		System.Random random = new System.Random((int)(Time.realtimeSinceStartup * 10000f));
		if (f_nRandom != 0)
		{
			int num = this.m_listPair.Count - f_nRandom;
			for (int i = num; i > 0; i--)
			{
				this.m_listPair.RemoveAt(random.Next(0, this.m_listPair.Count - 1));
			}
		}
		this.m_SelectorText = VRSelectorMenu.CreateSelector();
		NDebug.Assert(this.m_SelectorText != null, "VR選択肢が取得できませんでした。");
		bool flag = false;
		bool flag2 = false;
		for (int j = 0; j < this.m_listPair.Count; j++)
		{
			VRChoices.SelectPair selectPair = this.m_listPair[j];
			if (selectPair.type == VRChoices.SelectPair.Type.Look || selectPair.type == VRChoices.SelectPair.Type.Touch || selectPair.type == VRChoices.SelectPair.Type.TouchToLook)
			{
				if (selectPair.nMaidNo == -1)
				{
					NDebug.Assert("VR選択肢、見る触るなのにメイド番号が指定されていません。", false);
				}
				List<VRChoices.SelectPair> list;
				if (!this.m_dicMaidLookTouch.TryGetValue(selectPair.nMaidNo, out list))
				{
					this.m_dicMaidLookTouch.Add(selectPair.nMaidNo, list = new List<VRChoices.SelectPair>());
				}
				list.Add(selectPair);
				if (selectPair.type == VRChoices.SelectPair.Type.Touch)
				{
					flag = true;
				}
				if (selectPair.type == VRChoices.SelectPair.Type.Look || selectPair.type == VRChoices.SelectPair.Type.TouchToLook)
				{
					flag2 = true;
				}
			}
			else
			{
				this.m_TextSelectPair.Add(selectPair);
			}
		}
		if (this.m_dicMaidLookTouch.Count != 0)
		{
			foreach (KeyValuePair<int, List<VRChoices.SelectPair>> keyValuePair in this.m_dicMaidLookTouch)
			{
				int key = keyValuePair.Key;
				Maid maid = GameMain.Instance.CharacterMgr.GetMaid(key);
				if (maid == null || !maid.Visible)
				{
					NDebug.Assert("VR選択肢でメイドが居ないか、有効ではありません。", false);
				}
				else if (maid.IsBusy)
				{
					NDebug.Assert("VR選択肢でメイドがBusyです。", false);
				}
				MaidColliderCollect.AddCollider(maid, MaidColliderCollect.ColliderType.Touch);
			}
			if (flag)
			{
				if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
				{
					SelectMessage_UI.Message_Start(string.Empty);
					GameMain.Instance.OvrMgr.OvrCamera.ShowUI(false);
				}
				else
				{
					this.m_SelectorText.SetSelectType(VRChoices.SelectPair.Type.Touch);
					this.m_SelectorText.SelectStart(new List<VRChoices.SelectPair>(), new UnityAction<VRChoices.SelectPair>(this.OnLookTouchDummySelected));
				}
				this.m_TouchMgr = new VRTouchMgr();
			}
			if (flag2)
			{
				if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
				{
					SelectMessage_UI.Message_Start(string.Empty);
					GameMain.Instance.OvrMgr.OvrCamera.ShowUI(false);
				}
				else
				{
					this.m_SelectorText.SetSelectType(VRChoices.SelectPair.Type.Look);
					this.m_SelectorText.SelectStart(new List<VRChoices.SelectPair>(), new UnityAction<VRChoices.SelectPair>(this.OnLookTouchDummySelected));
				}
				this.m_LookMgr = new VRLookMgr();
			}
		}
		if (this.m_TextSelectPair.Count != 0)
		{
			this.m_SelectorText.SetSelectType(VRChoices.SelectPair.Type.Text);
			this.m_SelectorText.SelectStart(this.m_TextSelectPair, new UnityAction<VRChoices.SelectPair>(this.OnTextSelected));
		}
		if (f_nTimeOutMS != 0 && !string.IsNullOrEmpty(f_strTimeOutLabel))
		{
			this.TimeOutCountDownStart(f_nTimeOutMS, f_strTimeOutLabel);
		}
		this.m_bFinalizing = false;
	}

	private void OnTextSelected(VRChoices.SelectPair f_SelectedPair)
	{
		this.Finalize(f_SelectedPair.strLabel);
	}

	private void OnLookTouchDummySelected(VRChoices.SelectPair f_SelectedPair)
	{
	}

	public void Finalize(string f_strJumpLabel)
	{
		this.m_strSelectedLabel = f_strJumpLabel;
		this.m_bFinalizing = true;
		foreach (KeyValuePair<int, List<VRChoices.SelectPair>> keyValuePair in this.m_dicMaidLookTouch)
		{
			int key = keyValuePair.Key;
			Maid maid = GameMain.Instance.CharacterMgr.GetMaid(key);
			if (maid != null)
			{
				MaidColliderCollect.RemoveColliderAll(maid);
			}
		}
		this.Clear();
		this.OnFadeEnd();
	}

	private void OnFadeEnd()
	{
		if (!string.IsNullOrEmpty(this.m_strSelectedLabel))
		{
			GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(this.m_strSelectedLabel);
			GameMain.Instance.ScriptMgr.adv_kag.Exec();
		}
	}

	private VRChoices.SelectPair GetLookTouchPos(out int f_nMaidNo, out VRChoices.Region f_region, out VRChoices.SelectPair.Type f_type)
	{
		f_nMaidNo = -1;
		f_region = VRChoices.Region.Non;
		f_type = VRChoices.SelectPair.Type.Text;
		if (this.m_TouchMgr != null)
		{
			this.m_TouchMgr.FrameStart();
		}
		if (this.m_LookMgr != null)
		{
			this.m_LookMgr.FrameStart();
		}
		foreach (KeyValuePair<int, List<VRChoices.SelectPair>> keyValuePair in this.m_dicMaidLookTouch)
		{
			int key = keyValuePair.Key;
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				VRChoices.SelectPair pair = keyValuePair.Value[i];
				if (pair.type == VRChoices.SelectPair.Type.Touch)
				{
					List<KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>> list = this.m_TouchPosPair.FindAll((KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region> a) => ((ushort)a.Value & pair.uRegion) != 0);
					for (int j = 0; j < list.Count; j++)
					{
						KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region> keyValuePair2 = list[j];
						if (!keyValuePair2.Equals(default(KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>)))
						{
							VRTouchMgr.ETouchPos key2 = keyValuePair2.Key;
							for (int k = 0; k <= 1; k++)
							{
								if (this.m_TouchMgr.GetBehaviorTouch((VRTouchMgr.EHand)k, key2, VRTouchMgr.ETouchBehavior.触る, key))
								{
									f_nMaidNo = key;
									f_region = keyValuePair2.Value;
									f_type = VRChoices.SelectPair.Type.Touch;
									this.m_TouchMgr.FrameEnd();
									return pair;
								}
							}
						}
						else
						{
							NDebug.Assert("Region指定が不正です。", false);
						}
					}
				}
				else if (pair.type == VRChoices.SelectPair.Type.Look || pair.type == VRChoices.SelectPair.Type.TouchToLook)
				{
					List<KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>> list2 = this.m_TouchPosPair.FindAll((KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region> a) => ((ushort)a.Value & pair.uRegion) != 0);
					for (int l = 0; l < list2.Count; l++)
					{
						KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region> keyValuePair3 = list2[l];
						if (!keyValuePair3.Equals(default(KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>)))
						{
							VRTouchMgr.ETouchPos key3 = keyValuePair3.Key;
							VRLookMgr.State state;
							if (this.m_LookMgr.GetBehaviorTouch(key3, VRLookMgr.ELookBehavior.見る, key, out state))
							{
								f_nMaidNo = key;
								f_region = keyValuePair3.Value;
								f_type = VRChoices.SelectPair.Type.Look;
								if (pair.type == VRChoices.SelectPair.Type.TouchToLook)
								{
									f_type = VRChoices.SelectPair.Type.TouchToLook;
									GameMain.Instance.OvrMgr.OvrCamera.ShowDummyHand(OvrCamera.DUMMY_HAND.CHOICES_TOUCH, state.vHitPos, false);
								}
								this.m_LookMgr.FrameEnd();
								return pair;
							}
						}
						else
						{
							NDebug.Assert("Region指定が不正です。", false);
						}
					}
					GameMain.Instance.OvrMgr.OvrCamera.HideDummyHand(OvrCamera.DUMMY_HAND.CHOICES_TOUCH);
				}
			}
		}
		if (this.m_TouchMgr != null)
		{
			this.m_TouchMgr.FrameEnd();
		}
		if (this.m_LookMgr != null)
		{
			this.m_LookMgr.FrameEnd();
		}
		return null;
	}

	private void TouchLookTimeCountDownStart(string f_strText, float f_fTimeOut)
	{
		if (f_fTimeOut < 0f)
		{
			this.m_fTouchLookNowTime = (this.m_fTouchLookMaxTime = this.m_fTouchLookDefaultMaxTime);
		}
		else
		{
			this.m_fTouchLookMaxTime = f_fTimeOut;
			this.m_fTouchLookNowTime = f_fTimeOut;
		}
		this.m_EventText.SetGaugeRate(0f);
		this.m_EventText.Show(VREventText.TYPE.CHOICES, f_strText, 0f);
	}

	private bool TouchLookTimeCountDown()
	{
		if (!this.m_bFinalizing)
		{
			this.m_fTouchLookNowTime -= Time.deltaTime;
			if (this.m_fTouchLookNowTime <= 0f)
			{
				this.m_fTouchLookNowTime = 0f;
				this.TouchLookTimeOut();
				this.m_EventText.SetGaugeRate(1f);
				return true;
			}
			this.m_EventText.SetGaugeRate(1f - this.m_fTouchLookNowTime / this.m_fTouchLookMaxTime);
		}
		return false;
	}

	private void TouchLookTimeCountDownStop()
	{
		this.m_EventText.Hide(VREventText.TYPE.CHOICES);
	}

	private void TouchLookTimeOut()
	{
		Debug.Log("VRChoices:TouchLookTimeOut");
		this.TouchLookTimeCountDownStop();
		this.Finalize(this.m_PairNow.strLabel);
	}

	private void TimeOutCountDownStart(int f_nTimeOutMS, string f_strTimeOutLabel)
	{
		this.m_strTimeOutLabel = f_strTimeOutLabel;
		this.m_fTimeOutMaxTime = (float)f_nTimeOutMS / 1000f;
		this.m_fTimeOutNowTime = this.m_fTimeOutMaxTime;
		this.m_bTimeOutCounting = true;
	}

	private bool TimeOutCountDown()
	{
		if (!this.m_bFinalizing && this.m_bTimeOutCounting)
		{
			this.m_fTimeOutNowTime -= Time.deltaTime;
			if (this.m_fTimeOutNowTime <= 0f)
			{
				Debug.Log("VRChoices:AllTimeOut");
				this.Finalize(this.m_strTimeOutLabel);
				return true;
			}
		}
		return false;
	}

	private void Update()
	{
		GameMain.Instance.OvrMgr.OvrCamera.HideDummyHand(OvrCamera.DUMMY_HAND.CHOICES_TOUCH);
		if (!this.m_bFinalizing)
		{
			if (this.m_bTimeOutCounting && this.TimeOutCountDown())
			{
				return;
			}
			int num = -1;
			VRChoices.Region region = VRChoices.Region.Non;
			VRChoices.SelectPair.Type type = VRChoices.SelectPair.Type.Text;
			this.m_PairNow = this.GetLookTouchPos(out num, out region, out type);
			if (this.m_PairNow != null)
			{
				if ((this.m_nBackMaidNo == -1 && num != -1) || this.m_nBackMaidNo != num || this.m_BackType != type || this.m_BackRegion != region)
				{
					this.TouchLookTimeCountDownStop();
					if (this.m_PairNow == null)
					{
						NDebug.Assert("VR選択肢の登録が不正です。", false);
					}
					this.TouchLookTimeCountDownStart(this.m_PairNow.strText, this.m_PairNow.fTimeOut);
				}
				else if (this.m_nBackMaidNo == num)
				{
					if (this.m_BackType == type && this.m_BackRegion == region)
					{
						if (this.TouchLookTimeCountDown())
						{
							return;
						}
					}
					else
					{
						NDebug.Assert("VR選択肢が不正です。", false);
					}
				}
				else if (this.m_nBackMaidNo != -1 && num == -1)
				{
					this.m_PairNow = null;
					this.TouchLookTimeCountDownStop();
				}
			}
			else
			{
				this.TouchLookTimeCountDownStop();
			}
			this.m_nBackMaidNo = num;
			this.m_BackRegion = region;
			this.m_BackType = type;
		}
	}

	private readonly KeyValuePair<string, VRChoices.Region>[] m_RegionPair = new KeyValuePair<string, VRChoices.Region>[]
	{
		new KeyValuePair<string, VRChoices.Region>("頭", VRChoices.Region.Head),
		new KeyValuePair<string, VRChoices.Region>("胸", VRChoices.Region.Bust),
		new KeyValuePair<string, VRChoices.Region>("尻", VRChoices.Region.Hip),
		new KeyValuePair<string, VRChoices.Region>("手", VRChoices.Region.Hand)
	};

	private readonly List<KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>> m_TouchPosPair = new List<KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>>
	{
		new KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>(VRTouchMgr.ETouchPos.頭頂部, VRChoices.Region.Head),
		new KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>(VRTouchMgr.ETouchPos.胸, VRChoices.Region.Bust),
		new KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>(VRTouchMgr.ETouchPos.お尻, VRChoices.Region.Hip),
		new KeyValuePair<VRTouchMgr.ETouchPos, VRChoices.Region>(VRTouchMgr.ETouchPos.手, VRChoices.Region.Hand)
	};

	private List<VRChoices.SelectPair> m_listPair = new List<VRChoices.SelectPair>();

	private Dictionary<int, List<VRChoices.SelectPair>> m_dicMaidLookTouch = new Dictionary<int, List<VRChoices.SelectPair>>();

	private List<VRChoices.SelectPair> m_TextSelectPair = new List<VRChoices.SelectPair>();

	private VRTouchMgr m_TouchMgr;

	private VRLookMgr m_LookMgr;

	private float m_fTouchLookDefaultMaxTime = 2f;

	private float m_fTouchLookMaxTime = 2f;

	private float m_fTouchLookNowTime;

	private VREventText m_EventText;

	private bool m_bFinalizing = true;

	private bool m_bTimeOutCounting;

	private float m_fTimeOutMaxTime;

	private float m_fTimeOutNowTime;

	private string m_strTimeOutLabel;

	private VRSelectorMenu m_SelectorText;

	private string m_strSelectedLabel;

	private int m_nBackMaidNo = -1;

	private VRChoices.Region m_BackRegion;

	private VRChoices.SelectPair.Type m_BackType;

	private VRChoices.SelectPair m_PairNow;

	public enum Region
	{
		Non,
		Head,
		Bust,
		Hip = 4,
		Hand = 8
	}

	public class SelectPair
	{
		public SelectPair(string f_strText, string f_strLabel, int f_nMaidNo, VRChoices.SelectPair.Type f_type, ushort f_sRegionFlag, float f_fTimeOut = -1f)
		{
			this.strText = f_strText;
			this.strLabel = f_strLabel;
			this.nMaidNo = f_nMaidNo;
			this.type = f_type;
			this.uRegion = f_sRegionFlag;
			this.fTimeOut = f_fTimeOut;
		}

		public string strText;

		public string strLabel;

		public VRChoices.SelectPair.Type type;

		public ushort uRegion;

		public int nMaidNo;

		public float fTimeOut;

		public enum Type
		{
			Text,
			Touch,
			Look,
			TouchToLook
		}
	}
}
