using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using wf;

public class TouchAction_Mgr : PartsMgrBase
{
	public static TouchAction_Mgr Instance { get; private set; }

	protected override void Start()
	{
		TouchAction_Mgr.Instance = this;
		base.Start();
		int tmpGenericFlag = GameMain.Instance.CMSystem.GetTmpGenericFlag("ダンスOVRカメラタイプ");
		if (!base.IsActive || !GameMain.Instance.VRMode || tmpGenericFlag == 1)
		{
			base.IsActive = false;
			base.gameObject.SetActive(false);
			return;
		}
		base.transform.parent.gameObject.layer = base.gameObject.layer;
		if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.Encore)
		{
			GameMain.Instance.MainCamera.SetPos(this.m_FirstCamPos);
		}
		OvrMgr.OvrObject.Controller left_controller = GameMain.Instance.OvrMgr.ovr_obj.left_controller;
		OvrMgr.OvrObject.Controller right_controller = GameMain.Instance.OvrMgr.ovr_obj.right_controller;
		if (left_controller != null)
		{
			this.m_ControllerInput.Add(left_controller.controller, left_controller.controller_buttons);
		}
		if (right_controller != null)
		{
			this.m_ControllerInput.Add(right_controller.controller, right_controller.controller_buttons);
		}
		this.m_TouchMgr = new VRTouchMgr();
	}

	private IEnumerator ActionCheck()
	{
		Dictionary<AVRControllerButtons, bool> input_exist = new Dictionary<AVRControllerButtons, bool>();
		foreach (AVRControllerButtons key in this.m_ControllerInput.Values)
		{
			input_exist.Add(key, false);
		}
		for (;;)
		{
			yield return null;
			if (!RhythmAction_Mgr.Instance.IsPause && RhythmAction_Mgr.Instance.DanceMaid.Count > 1)
			{
				Maid vibeTarget = this.m_VibeTarget;
				if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
				{
					if (Input.GetKeyDown(KeyCode.LeftArrow) && this.m_MaidUIInfoSet[this.m_VibeTarget].PreviousUIInfo != null)
					{
						this.m_VibeTarget = this.m_MaidUIInfoSet[this.m_VibeTarget].PreviousUIInfo.TargetMaid;
					}
					else if (Input.GetKeyDown(KeyCode.RightArrow) && this.m_MaidUIInfoSet[this.m_VibeTarget].NextUIInfo != null)
					{
						this.m_VibeTarget = this.m_MaidUIInfoSet[this.m_VibeTarget].NextUIInfo.TargetMaid;
					}
				}
				else
				{
					foreach (KeyValuePair<AVRController, AVRControllerButtons> keyValuePair in this.m_ControllerInput)
					{
						AVRController key2 = keyValuePair.Key;
						if (key2.HandDanceMode)
						{
							AVRControllerButtons value = keyValuePair.Value;
							Vector2 axis = value.GetAxis();
							bool flag = Mathf.Abs(axis.x) > 0.25f;
							bool flag2 = false;
							AVRControllerButtons.TYPE type = value.Type;
							if (type != AVRControllerButtons.TYPE.TOUCH)
							{
								if (type == AVRControllerButtons.TYPE.VIVE)
								{
									if (flag && value.GetPressDown(AVRControllerButtons.BTN.STICK_PAD))
									{
										flag2 = true;
									}
								}
							}
							else if (!flag)
							{
								input_exist[value] = false;
							}
							else if (!input_exist[value])
							{
								input_exist[value] = true;
								flag2 = true;
							}
							if (flag2)
							{
								if (axis.x < 0f && this.m_MaidUIInfoSet[this.m_VibeTarget].PreviousUIInfo != null)
								{
									this.m_VibeTarget = this.m_MaidUIInfoSet[this.m_VibeTarget].PreviousUIInfo.TargetMaid;
								}
								else if (axis.x > 0f && this.m_MaidUIInfoSet[this.m_VibeTarget].NextUIInfo != null)
								{
									this.m_VibeTarget = this.m_MaidUIInfoSet[this.m_VibeTarget].NextUIInfo.TargetMaid;
								}
							}
							if (vibeTarget != this.m_VibeTarget)
							{
								break;
							}
						}
					}
				}
				if (vibeTarget != this.m_VibeTarget)
				{
					GameMain.Instance.SoundMgr.PlaySystem(SoundMgr.SeType.Hover);
					this.m_SelectFrame.localPosition = this.m_MaidUIInfoSet[this.m_VibeTarget].UIObj.transform.localPosition;
				}
			}
			foreach (Maid maid in RhythmAction_Mgr.Instance.DanceMaid)
			{
				if (!RhythmAction_Mgr.Instance.IsPause)
				{
					if (maid == this.m_VibeTarget)
					{
						bool flag3 = this.m_ControllerInput.Any((KeyValuePair<AVRController, AVRControllerButtons> e) => e.Key.HandDanceMode && e.Value.GetPressDown(AVRControllerButtons.BTN.TRIGGER));
						if (Input.GetKeyDown(KeyCode.Space) || flag3)
						{
							this.m_ActionParam[maid].IsVibeOn = !this.m_ActionParam[maid].IsVibeOn;
						}
					}
					if (!this.m_ActionParam[maid].m_VibeObj)
					{
						this.m_ActionParam[maid].m_VibeObj = maid.body0.GetSlot(34).obj_tr;
						this.m_ActionParam[maid].VibeAudio.Init(AudioSourceMgr.Type.Se, true, GameMain.Instance.SoundMgr, this.m_ActionParam[maid].m_VibeObj);
					}
					if (GameMain.Instance.MainCamera.IsFadeOut())
					{
						this.m_ActionParam[maid].VibeAudio.audiosource.Pause();
					}
					if (this.m_TouchMgr != null)
					{
						this.m_TouchMgr.FrameStart();
					}
					foreach (KeyValuePair<TouchActionParam.TouchType, TouchActionParam.ActionParam> keyValuePair2 in this.m_ActionParam[maid].TouchParam)
					{
						bool flag4 = false;
						if (keyValuePair2.Key == TouchActionParam.TouchType.セクハラ)
						{
							if (this.m_ActionParam[maid].IsVibeOn)
							{
								this.m_MaidUIInfoSet[maid].Icon.color = this.m_VibeOnColor;
								flag4 = true;
								if (!this.m_ActionParam[maid].VibeAudio.isPlay())
								{
									if (!this.m_ActionParam[maid].IsVibeFirst)
									{
										this.m_ActionParam[maid].VibeAudio.LoadPlay("SE020.ogg", 0f, false, true);
									}
									else
									{
										this.m_ActionParam[maid].VibeAudio.audiosource.UnPause();
									}
									this.m_ActionParam[maid].IsVibeFirst = true;
								}
								this.m_MaidUIInfoSet[maid].SexualEffect.gameObject.SetActive(true);
							}
							else
							{
								this.m_ActionParam[maid].VibeAudio.audiosource.Pause();
								this.m_MaidUIInfoSet[maid].SexualEffect.gameObject.SetActive(false);
								this.m_MaidUIInfoSet[maid].Icon.color = Color.white;
							}
						}
						for (int i = 0; i <= 1; i++)
						{
							if (flag4)
							{
								break;
							}
							foreach (VRTouchMgr.ETouchPos etouchPos in this.m_TouchPosList)
							{
								if (this.m_TouchMgr.GetBehaviorTouch((VRTouchMgr.EHand)i, etouchPos, VRTouchMgr.ETouchBehavior.触る, maid.ActiveSlotNo) && this.PosDealingType(keyValuePair2.Key, etouchPos))
								{
									flag4 = true;
									break;
								}
							}
						}
						keyValuePair2.Value.IsTouch = flag4;
						if (keyValuePair2.Value.IsTouch)
						{
							keyValuePair2.Value.TouchTimer += RhythmAction_Mgr.Instance.DanceDeltaTime;
							keyValuePair2.Value.NotTouchTimer = 0f;
							if (keyValuePair2.Value.TouchTimer >= 2f)
							{
								keyValuePair2.Value.TouchTimer = 0f;
								keyValuePair2.Value.Point++;
							}
						}
						else
						{
							keyValuePair2.Value.NotTouchTimer += RhythmAction_Mgr.Instance.DanceDeltaTime;
							if (keyValuePair2.Value.NotTouchTimer >= 3f)
							{
								keyValuePair2.Value.NotTouchTimer = 0f;
								keyValuePair2.Value.Point--;
								if (keyValuePair2.Value.Point < 0)
								{
									keyValuePair2.Value.Point = 0;
								}
							}
						}
					}
					if (this.m_TouchMgr != null)
					{
						this.m_TouchMgr.FrameEnd();
					}
				}
				else
				{
					this.m_ActionParam[maid].VibeAudio.audiosource.Pause();
				}
				TouchActionParam.TouchType type2 = TouchActionParam.TouchType.セクハラ;
				if (this.m_ActionParam[maid].TouchParam[TouchActionParam.TouchType.セクハラ].Point < this.m_ActionParam[maid].TouchParam[TouchActionParam.TouchType.お邪魔].Point)
				{
					type2 = TouchActionParam.TouchType.お邪魔;
				}
				this.PointFaceChenge(maid, type2, this.m_ActionParam[maid]);
			}
		}
		yield break;
	}

	private bool PosDealingType(TouchActionParam.TouchType type, VRTouchMgr.ETouchPos pos)
	{
		if (type == TouchActionParam.TouchType.セクハラ)
		{
			return this.m_SexualPos.Any((VRTouchMgr.ETouchPos e) => e == pos);
		}
		return this.m_SexualPos.All((VRTouchMgr.ETouchPos e) => e != pos);
	}

	private void PointFaceChenge(Maid maid, TouchActionParam.TouchType type, TouchActionParam param)
	{
		bool flag = type != this.m_ActionParam[maid].NowSetType;
		bool flag2 = this.m_ActionParam[maid].NowUseParam.Point == 0;
		if (flag || flag2)
		{
			this.m_ActionParam[maid].IsEventStart = false;
			if (flag || (!this.m_ActionParam[maid].MotionFaceBlendChange && flag2))
			{
				RhythmAction_Mgr.Instance.SetMaidFaceBlendStop(maid, false);
			}
			if (flag || (!this.m_ActionParam[maid].MotionFaceChange && flag2))
			{
				RhythmAction_Mgr.Instance.SetMaidFaceStop(maid, false);
			}
			this.m_ActionParam[maid].NowSetType = type;
			if (flag2)
			{
				return;
			}
		}
		TouchAction_Mgr.StateTable stateTable = this.m_EachStateTable.SingleOrDefault((TouchAction_Mgr.StateTable e) => e.Type == type);
		int point = this.GetBordarPoint(this.m_ActionParam[maid].NowUseParam, stateTable);
		TouchAction_Mgr.StateTable.ParamSet paramSet = stateTable.PointFacePair.SingleOrDefault((TouchAction_Mgr.StateTable.ParamSet e) => e.Point == point);
		RhythmAction_Mgr.Instance.SetMaidFaceBlendStop(maid, !string.IsNullOrEmpty(paramSet.FaceBlend));
		if (RhythmAction_Mgr.Instance.GetMaidFaceBlendStop(maid))
		{
			this.FaceBlendChange(maid, paramSet.FaceBlend);
		}
		if (!this.m_ActionParam[maid].IsEventStart)
		{
			this.m_ActionParam[maid].IsEventStart = true;
			base.StartCoroutine(this.FaceChangeEvent(this.m_ActionParam[maid], stateTable, maid));
		}
	}

	private void FaceBlendChange(Maid maid, string blend)
	{
		TMorph.AddBlendType addBlendType = TMorph.AddBlendType.None;
		if (blend.IndexOf("頬１") >= 0 || blend.IndexOf("頬1") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Cheek1;
		}
		if (blend.IndexOf("頬２") >= 0 || blend.IndexOf("頬2") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Cheek2;
		}
		if (blend.IndexOf("頬３") >= 0 || blend.IndexOf("頬3") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Cheek3;
		}
		if (blend.IndexOf("涙１") >= 0 || blend.IndexOf("涙1") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Tear1;
		}
		if (blend.IndexOf("涙２") >= 0 || blend.IndexOf("涙2") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Tear2;
		}
		if (blend.IndexOf("涙３") >= 0 || blend.IndexOf("涙3") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Tear3;
		}
		if (blend.IndexOf("赤面") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Blush;
		}
		if (blend.IndexOf("ショック") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.Shock;
		}
		if (blend.IndexOf("玉涙") >= 0)
		{
			addBlendType |= TMorph.AddBlendType.TearBig;
		}
		maid.body0.Face.morph.SetValueOriginalBlendSet(addBlendType);
		maid.FaceBlend("オリジナル");
	}

	private IEnumerator FaceChangeEvent(TouchActionParam param, TouchAction_Mgr.StateTable table, Maid maid)
	{
		float timer = 0f;
		TouchActionParam.ActionParam touch_param = param.NowUseParam;
		int now_point = this.GetBordarPoint(touch_param, table);
		TouchAction_Mgr.StateTable.ParamSet table_param = table.PointFacePair.SingleOrDefault((TouchAction_Mgr.StateTable.ParamSet e) => e.Point == now_point);
		TouchActionParam.TouchType now_type = this.m_ActionParam[maid].NowSetType;
		if (now_type == TouchActionParam.TouchType.セクハラ)
		{
			this.m_MaidUIInfoSet[maid].SexualEffect.gameObject.SetActive(true);
		}
		for (;;)
		{
			if (!RhythmAction_Mgr.Instance.IsPause)
			{
				timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
			}
			if (now_type != this.m_ActionParam[maid].NowSetType)
			{
				break;
			}
			string face = string.Empty;
			if (timer >= 5f)
			{
				timer = 0f;
				if (UnityEngine.Random.Range(0, 100) < table_param.Probability)
				{
					if (table_param.Face.Count<string>() > 0)
					{
						RhythmAction_Mgr.Instance.SetMaidFaceStop(maid, true);
						int num = 0;
						if (table_param.Face.Count<string>() > 1)
						{
							num = UnityEngine.Random.Range(0, table_param.Face.Count<string>());
						}
						face = table_param.Face[num];
						maid.FaceAnime(table_param.Face[num], 1f, 0);
					}
					else
					{
						RhythmAction_Mgr.Instance.SetMaidFaceStop(maid, false);
					}
				}
				else
				{
					RhythmAction_Mgr.Instance.SetMaidFaceStop(maid, false);
				}
			}
			else if (now_point != this.GetBordarPoint(touch_param, table))
			{
				int now_point2 = now_point;
				now_point = this.GetBordarPoint(touch_param, table);
				if (now_point == 0)
				{
					goto Block_10;
				}
				table_param = table.PointFacePair.SingleOrDefault((TouchAction_Mgr.StateTable.ParamSet e) => e.Point == now_point);
				timer = 0f;
				if (now_type == TouchActionParam.TouchType.セクハラ)
				{
					ParticleSystem.EmissionModule emission = this.m_MaidUIInfoSet[maid].SexualEffect.emission;
					ParticleSystem.MinMaxCurve rateOverTime = emission.rateOverTime;
					float num2 = this.m_MaidUIInfoSet[maid].SexualEffect.emission.rateOverTime.constantMax;
					if (now_point2 < now_point)
					{
						num2 *= 2f;
					}
					else
					{
						num2 /= 2f;
					}
					Debug.Log(num2);
					rateOverTime.constantMax = num2;
					emission.rateOverTime = rateOverTime;
				}
			}
			yield return null;
		}
		if (now_type == TouchActionParam.TouchType.セクハラ)
		{
			this.m_MaidUIInfoSet[maid].SexualEffect.gameObject.SetActive(false);
		}
		yield break;
		Block_10:
		if (now_type == TouchActionParam.TouchType.セクハラ)
		{
			this.m_MaidUIInfoSet[maid].SexualEffect.gameObject.SetActive(false);
		}
		yield break;
		yield break;
	}

	private int GetBordarPoint(TouchActionParam.ActionParam touch_param, TouchAction_Mgr.StateTable table)
	{
		int result = 0;
		foreach (TouchAction_Mgr.StateTable.ParamSet paramSet in table.PointFacePair)
		{
			if (touch_param.Point >= paramSet.Point)
			{
				result = paramSet.Point;
			}
		}
		return result;
	}

	public override void StartAction()
	{
		int num = 0;
		TouchAction_Mgr.VibeUIInfo previousUIInfo = null;
		foreach (Maid maid in RhythmAction_Mgr.Instance.DanceMaid)
		{
			if (!this.m_VibeTarget)
			{
				this.m_VibeTarget = maid;
			}
			TouchAction_Mgr.VibeUIInfo vibeUIInfo = new TouchAction_Mgr.VibeUIInfo();
			GameObject gameObject = Utility.CreatePrefab(base.gameObject, "SceneDance/Rhythm_Action/Prefab/" + this.m_IconName, true);
			gameObject.transform.localPosition = Vector3.right * (float)num * this.m_IconSetSpace + Vector3.up * this.m_IconSetSpace / 2f;
			vibeUIInfo.TargetMaid = maid;
			vibeUIInfo.UIObj = gameObject;
			vibeUIInfo.PreviousUIInfo = previousUIInfo;
			if (vibeUIInfo.PreviousUIInfo != null)
			{
				vibeUIInfo.PreviousUIInfo.NextUIInfo = vibeUIInfo;
			}
			vibeUIInfo.Icon = gameObject.transform.Find("IconMask/Icon").GetComponent<UI2DSprite>();
			vibeUIInfo.SexualEffect = gameObject.transform.Find("SexualEffect").GetComponent<ParticleSystem>();
			RhythmAction_Mgr.Instance.AddParticleSystem(vibeUIInfo.SexualEffect);
			vibeUIInfo.SexualEffect.gameObject.SetActive(false);
			Texture2D thumIcon = maid.GetThumIcon();
			if (thumIcon)
			{
				Sprite sprite2D = Sprite.Create(thumIcon, new Rect(0f, 0f, (float)thumIcon.width, (float)thumIcon.height), Vector2.one * 0.5f);
				vibeUIInfo.Icon.sprite2D = sprite2D;
				vibeUIInfo.Icon.width = (int)this.m_IconSetSpace;
				vibeUIInfo.Icon.height = (int)this.m_IconSetSpace;
			}
			this.m_ActionParam.Add(maid, new TouchActionParam());
			GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(GameObject.Find("AudioSe(Clone)"));
			this.m_ActionParam[maid].VibeAudio = gameObject2.GetComponent<AudioSourceMgr>();
			this.m_ActionParam[maid].VibeAudio.name = "VibeSE";
			MaidColliderCollect.AddCollider(maid, MaidColliderCollect.ColliderType.Touch);
			maid.SetProp(MPN.accvag, "accVag_VibePink_I_.menu", 0, true, false);
			maid.AllProcPropSeqStart();
			num++;
			this.m_MaidUIInfoSet.Add(maid, vibeUIInfo);
			previousUIInfo = vibeUIInfo;
		}
		base.StartCoroutine(this.ActionCheck());
	}

	public override void EndAction()
	{
		base.StopAllCoroutines();
		foreach (Maid maid in RhythmAction_Mgr.Instance.DanceMaid)
		{
			UnityEngine.Object.Destroy(this.m_ActionParam[maid].VibeAudio.gameObject);
			MaidColliderCollect.RemoveColliderAll(maid);
			maid.ResetProp(MPN.accvag, true);
			maid.AllProcProp();
			maid.FaceBlend("無し");
		}
	}

	public bool IsMaidTouch()
	{
		return this.m_ActionParam.Values.Any((TouchActionParam maid) => maid.DoTouchAction());
	}

	public TouchActionParam GetTouchParam(Maid maid)
	{
		return this.m_ActionParam[maid];
	}

	private const float m_TouchPointTime = 2f;

	private const float m_NotTouchTime = 3f;

	private const float m_FaceChangeTime = 5f;

	private const float m_EffectEmitRate = 2f;

	private Dictionary<Maid, TouchActionParam> m_ActionParam = new Dictionary<Maid, TouchActionParam>();

	[SerializeField]
	[Header("タッチ箇所一覧")]
	private List<VRTouchMgr.ETouchPos> m_TouchPosList = new List<VRTouchMgr.ETouchPos>
	{
		VRTouchMgr.ETouchPos.頭頂部,
		VRTouchMgr.ETouchPos.髪の毛,
		VRTouchMgr.ETouchPos.胸,
		VRTouchMgr.ETouchPos.お腹,
		VRTouchMgr.ETouchPos.手,
		VRTouchMgr.ETouchPos.お尻,
		VRTouchMgr.ETouchPos.腿,
		VRTouchMgr.ETouchPos.リボン,
		VRTouchMgr.ETouchPos.スカ\u30FCト
	};

	private VRTouchMgr m_TouchMgr;

	[SerializeField]
	[Header("アンコールライブ時のカメラの初期位置")]
	private Vector3 m_FirstCamPos;

	private Dictionary<Maid, TouchAction_Mgr.VibeUIInfo> m_MaidUIInfoSet = new Dictionary<Maid, TouchAction_Mgr.VibeUIInfo>();

	private Maid m_VibeTarget;

	[SerializeField]
	[Header("アイコン選択UI")]
	private Transform m_SelectFrame;

	[SerializeField]
	[Header("アイコン設置間隔")]
	private float m_IconSetSpace = 100f;

	[SerializeField]
	[Header("アイコン名")]
	private string m_IconName = "VibeMaidIcon";

	[SerializeField]
	[Header("バイブOn時のアイコンの色")]
	private Color m_VibeOnColor = Color.red;

	[SerializeField]
	[Header("セクハラに該当するタッチ箇所")]
	private VRTouchMgr.ETouchPos[] m_SexualPos;

	[SerializeField]
	[Header("各種状態テーブル")]
	private TouchAction_Mgr.StateTable[] m_EachStateTable;

	private Dictionary<AVRController, AVRControllerButtons> m_ControllerInput = new Dictionary<AVRController, AVRControllerButtons>();

	private class VibeUIInfo
	{
		public Maid TargetMaid;

		public GameObject UIObj;

		public UI2DSprite Icon;

		public ParticleSystem SexualEffect;

		public TouchAction_Mgr.VibeUIInfo PreviousUIInfo;

		public TouchAction_Mgr.VibeUIInfo NextUIInfo;
	}

	[Serializable]
	private class StateTable
	{
		[Header("状態")]
		public TouchActionParam.TouchType Type;

		[Header("ポイントごとの表情変更設定")]
		public TouchAction_Mgr.StateTable.ParamSet[] PointFacePair;

		[Serializable]
		public class ParamSet
		{
			[Header("表情変更開始ポイント")]
			public int Point;

			[Header("表情ブレンド")]
			public string FaceBlend;

			[Header("表情変更確率")]
			public int Probability;

			[Header("表情リスト")]
			public string[] Face;
		}
	}
}
