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

public class VRFaceShortcutConfig : MonoBehaviour
{
	public void CacheItemTemplate<T>(string path) where T : VRFaceShortcutConfig.Item
	{
		Transform childObject = VRFaceShortcutConfig.GetChildObject<Transform>(this.m_Grid.gameObject, path);
		T t = childObject.gameObject.AddComponent<T>();
		Type typeFromHandle = typeof(T);
		this.m_ItemTemplateList.Add(typeFromHandle, t);
		childObject.gameObject.SetActive(false);
	}

	public T CreateItem<T>(string itemLabel) where T : VRFaceShortcutConfig.Item
	{
		if (this.m_ItemDic.ContainsKey(itemLabel))
		{
			string message = "[VRFaceShortcutConfig] 既に「" + itemLabel + "」という項目が存在しています";
			Debug.LogError(message);
			NDebug.Assert(message, false);
		}
		VRFaceShortcutConfig.Item item;
		if (!this.m_ItemTemplateList.TryGetValue(typeof(T), out item))
		{
			string message2 = "[VRFaceShortcutConfig] " + typeof(T).ToString() + "型のUI項目はキャッシュされていません";
			Debug.LogError(message2);
			NDebug.Assert(message2, false);
		}
		GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(item.gameObject);
		gameObject.transform.SetParent(this.m_Grid.transform, false);
		T component = gameObject.GetComponent<T>();
		component.gameObject.SetActive(true);
		component.label.text = itemLabel;
		this.m_ItemDic.Add(itemLabel, component);
		return component;
	}

	private void Awake()
	{
		this.CacheItemTemplate<VRFaceShortcutConfig.ItemSlider>("temp slider");
		this.CacheItemTemplate<VRFaceShortcutConfig.ItemSelectButton>("temp select");
		this.CacheItemTemplate<VRFaceShortcutConfig.ParentMultiButton>("temp multi select");
		this.CreateDefaultItems();
		this.CreateViveItems();
		NGUIWindow fadeWindowThis = this.m_FadeWindowThis;
		fadeWindowThis.OnClose = (Action)Delegate.Combine(fadeWindowThis.OnClose, new Action(delegate()
		{
			ControllerShortcutSettingData.config.Write();
		}));
	}

	private void CreateDefaultItems()
	{
		OVRLipSync.VariableData variable = GameMain.Instance.LipSyncMgr.Variable;
		VRFaceShortcutConfig.ItemSlider itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("リップシンク：マイク感度");
		itemSlider.min = 0f;
		itemSlider.max = 10f;
		itemSlider.Set(variable.MicSensitivity);
		itemSlider.onValueChanged = delegate(float value)
		{
			GameMain.Instance.LipSyncMgr.Variable.MicSensitivity = value;
		};
		itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("リップシンク：自分の声の大きさ");
		itemSlider.min = 0f;
		itemSlider.max = 10f;
		itemSlider.Set(variable.MicVolume);
		itemSlider.onValueChanged = delegate(float value)
		{
			GameMain.Instance.LipSyncMgr.Variable.MicVolume = value;
		};
		VRFaceShortcutConfig.ItemSelectButton itemSelectButton = this.CreateItem<VRFaceShortcutConfig.ItemSelectButton>("リップシンク：ミュートにする");
		itemSelectButton.textLeft.text = "ON";
		itemSelectButton.textRight.text = "OFF";
		itemSelectButton.Set(variable.Mute);
		itemSelectButton.onClickIsLeft = delegate(bool isLeft)
		{
			variable.Mute = isLeft;
		};
		itemSelectButton = this.CreateItem<VRFaceShortcutConfig.ItemSelectButton>("手元ショートカット：表情の変化");
		itemSelectButton.textLeft.text = "即座に変更する";
		itemSelectButton.textRight.text = "選択して決定する";
		itemSelectButton.Set(ControllerShortcutSettingData.config.isDirectMode);
		itemSelectButton.onClickIsLeft = delegate(bool isLeft)
		{
			ControllerShortcutSettingData.config.isDirectMode = isLeft;
		};
		itemSelectButton = this.CreateItem<VRFaceShortcutConfig.ItemSelectButton>("手元ショートカット：手元のUI");
		itemSelectButton.textLeft.text = "常に表示する";
		itemSelectButton.textRight.text = "決定時に隠す";
		itemSelectButton.Set(ControllerShortcutSettingData.config.isEveryShowMode);
		itemSelectButton.onClickIsLeft = delegate(bool isLeft)
		{
			ControllerShortcutSettingData.config.isEveryShowMode = isLeft;
		};
		itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("体向きの頭への追従");
		itemSlider.min = 0f;
		itemSlider.max = 1f;
		itemSlider.Set(ControllerShortcutSettingData.config.maintainPelvisPosition);
		itemSlider.onValueChanged = delegate(float value)
		{
			ControllerShortcutSettingData.config.maintainPelvisPosition = value;
		};
		itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("体位置の頭への追従");
		itemSlider.min = 0f;
		itemSlider.max = 1f;
		itemSlider.Set(ControllerShortcutSettingData.config.bodyPosStiffness);
		itemSlider.onValueChanged = delegate(float value)
		{
			ControllerShortcutSettingData.config.bodyPosStiffness = value;
		};
		itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("体回転の頭への追従");
		itemSlider.min = 0f;
		itemSlider.max = 1f;
		itemSlider.Set(ControllerShortcutSettingData.config.bodyRotStiffness);
		itemSlider.onValueChanged = delegate(float value)
		{
			ControllerShortcutSettingData.config.bodyRotStiffness = value;
		};
		itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("胸部回転の頭への追従");
		itemSlider.min = 0f;
		itemSlider.max = 1f;
		itemSlider.Set(ControllerShortcutSettingData.config.chestRotationWeight);
		itemSlider.onValueChanged = delegate(float value)
		{
			ControllerShortcutSettingData.config.chestRotationWeight = value;
		};
		itemSlider = this.CreateItem<VRFaceShortcutConfig.ItemSlider>("カメラの画角");
		itemSlider.min = 0f;
		itemSlider.max = 1f;
		itemSlider.Set(ControllerShortcutSettingData.config.selfCameraFOV);
		itemSlider.onValueChanged = delegate(float value)
		{
			ControllerShortcutSettingData.config.selfCameraFOV = value;
		};
		VRFaceShortcutConfig.ParentMultiButton parentMultiButton = this.CreateItem<VRFaceShortcutConfig.ParentMultiButton>("手のスカートへの当たり判定\n※VR CONFIGより優先されます。");
		parentMultiButton.textRight.text = "OFF";
		parentMultiButton.textLeft.text = "常時ON";
		parentMultiButton.textCenter.text = "握時ON";
		if (GameMain.Instance.CMSystem.SConfig.OvrHandHitToSkirtVAS == CMSystem.SerializeConfig.OVR_HAND_TO_SKIRT.OFF)
		{
			parentMultiButton.Set(0);
		}
		else if (GameMain.Instance.CMSystem.SConfig.OvrHandHitToSkirtVAS == CMSystem.SerializeConfig.OVR_HAND_TO_SKIRT.ON)
		{
			parentMultiButton.Set(1);
		}
		else
		{
			parentMultiButton.Set(2);
		}
		parentMultiButton.onClick = delegate(int index)
		{
			CMSystem.SerializeConfig.OVR_HAND_TO_SKIRT ovrHandHitToSkirtVAS;
			if (index == 0)
			{
				ovrHandHitToSkirtVAS = CMSystem.SerializeConfig.OVR_HAND_TO_SKIRT.OFF;
			}
			else if (index == 1)
			{
				ovrHandHitToSkirtVAS = CMSystem.SerializeConfig.OVR_HAND_TO_SKIRT.ON;
			}
			else
			{
				ovrHandHitToSkirtVAS = CMSystem.SerializeConfig.OVR_HAND_TO_SKIRT.GRAB;
			}
			GameMain.Instance.CMSystem.SConfig.OvrHandHitToSkirtVAS = ovrHandHitToSkirtVAS;
		};
	}

	private void CreateViveItems()
	{
		if (GameMain.Instance.VRDeviceTypeID != GameMain.VRDeviceType.VIVE)
		{
			return;
		}
		VRFaceShortcutConfig.ItemSelectButton itemSelectButton = this.CreateItem<VRFaceShortcutConfig.ItemSelectButton>("一番上にあるトラッカーを\n頭トラッキングに利用");
		itemSelectButton.textLeft.text = "ON";
		itemSelectButton.textRight.text = "OFF";
		itemSelectButton.Set(ControllerShortcutSettingData.config.use1TrackerForHead);
		itemSelectButton.onClickIsLeft = delegate(bool isLeft)
		{
			ControllerShortcutSettingData.config.use1TrackerForHead = isLeft;
			this.SetActiveItem("2つめと3つめのトラッカーを\n足トラッキングに利用", isLeft, false);
			this.SetActiveItem("1つめと2つめのトラッカーを\n足トラッキングに利用", !isLeft, false);
		};
	}

	private void SetActiveItem(string itemKey, bool isActive, bool isEnableError = true)
	{
		VRFaceShortcutConfig.Item item;
		if (!this.m_ItemDic.TryGetValue(itemKey, out item))
		{
			if (isEnableError)
			{
				string message = "[VRFaceShortcutConfig] コンフィグ項目に「" + itemKey + "」は存在しません";
				Debug.LogError(message);
				NDebug.Assert(message, false);
			}
			return;
		}
		if (item.gameObject.activeSelf != isActive)
		{
			item.gameObject.SetActive(isActive);
			this.m_IsUpdateReposition = true;
		}
	}

	private void LateUpdate()
	{
		if (this.m_IsUpdateReposition)
		{
			this.m_IsUpdateReposition = false;
			this.m_Grid.Reposition();
			this.m_ScrollView.UpdatePosition();
		}
	}

	private static T GetChildObject<T>(GameObject gameObject, string path)
	{
		GameObject childObject = UTY.GetChildObject(gameObject, path, false);
		if (childObject == null)
		{
			string message = string.Concat(new string[]
			{
				"[VRFaceShortcutConfig] ",
				gameObject.name,
				"/",
				path,
				"が見つかりませんでした"
			});
			NDebug.Assert(message, false);
			Debug.LogError(message);
		}
		T component = childObject.GetComponent<T>();
		if (component == null)
		{
			string message2 = string.Concat(new object[]
			{
				"[VRFaceShortcutConfig] ",
				gameObject.name,
				"/",
				path,
				"に、\n",
				typeof(T),
				"のコンポーネントがありませんでした"
			});
			NDebug.Assert(message2, false);
			Debug.LogError(message2);
		}
		return component;
	}

	[SerializeField]
	[Range(0.001f, 1f)]
	private float m_FadeSpeed = 0.25f;

	[SerializeField]
	private NGUIWindow m_FadeWindowThis;

	[SerializeField]
	private UIScrollView m_ScrollView;

	[SerializeField]
	private UIGrid m_Grid;

	private Dictionary<Type, VRFaceShortcutConfig.Item> m_ItemTemplateList = new Dictionary<Type, VRFaceShortcutConfig.Item>();

	private Dictionary<string, VRFaceShortcutConfig.Item> m_ItemDic = new Dictionary<string, VRFaceShortcutConfig.Item>();

	private bool m_IsUpdateReposition;

	public abstract class Item : MonoBehaviour
	{
		public UILabel label
		{
			get
			{
				return this.m_Label;
			}
			set
			{
				this.m_Label = value;
			}
		}

		protected virtual void Awake()
		{
			this.m_Label = VRFaceShortcutConfig.GetChildObject<UILabel>(base.gameObject, "name");
		}

		public abstract void Set(object value);

		private UILabel m_Label;
	}

	public class ItemSlider : VRFaceShortcutConfig.Item
	{
		public UISlider slider
		{
			get
			{
				return this.m_Slider;
			}
			private set
			{
				this.m_Slider = value;
			}
		}

		public float normalizedValue
		{
			get
			{
				return this.slider.value;
			}
			set
			{
				this.slider.value = value;
			}
		}

		public float min
		{
			get
			{
				return this.m_Min;
			}
			set
			{
				this.m_Min = value;
			}
		}

		public float max
		{
			get
			{
				return this.m_Max;
			}
			set
			{
				this.m_Max = value;
			}
		}

		public float value
		{
			get
			{
				return Mathf.Lerp(this.min, this.max, this.normalizedValue);
			}
			set
			{
				this.normalizedValue = Mathf.InverseLerp(this.min, this.max, value);
			}
		}

		public Action<float> onValueChanged { get; set; }

		public Action onDragFinished { get; set; }

		protected override void Awake()
		{
			base.Awake();
			this.slider = VRFaceShortcutConfig.GetChildObject<UISlider>(base.gameObject, "slider");
			EventDelegate.Add(this.slider.onChange, delegate()
			{
				if (this.onValueChanged != null)
				{
					this.onValueChanged(this.value);
				}
			});
			UISlider slider = this.slider;
			slider.onDragFinished = (UIProgressBar.OnDragFinished)Delegate.Combine(slider.onDragFinished, new UIProgressBar.OnDragFinished(delegate()
			{
				if (this.onDragFinished != null)
				{
					this.onDragFinished();
				}
			}));
		}

		public override void Set(object value)
		{
			this.value = (float)value;
		}

		private UISlider m_Slider;

		private float m_Min;

		private float m_Max = 1f;
	}

	public class ItemSelectButton : VRFaceShortcutConfig.Item
	{
		public UIButton buttonRight
		{
			get
			{
				return this.m_ButtonRight;
			}
			private set
			{
				this.m_ButtonRight = value;
			}
		}

		public UIButton buttonLeft
		{
			get
			{
				return this.m_ButtonLeft;
			}
			private set
			{
				this.m_ButtonLeft = value;
			}
		}

		public UILabel textRight
		{
			get
			{
				return this.m_TextRight;
			}
			private set
			{
				this.m_TextRight = value;
			}
		}

		public UILabel textLeft
		{
			get
			{
				return this.m_TextLeft;
			}
			private set
			{
				this.m_TextLeft = value;
			}
		}

		public Action<bool> onClickIsLeft { get; set; }

		protected override void Awake()
		{
			base.Awake();
			this.buttonRight = VRFaceShortcutConfig.GetChildObject<UIButton>(base.gameObject, "button right");
			this.buttonLeft = VRFaceShortcutConfig.GetChildObject<UIButton>(base.gameObject, "button left");
			this.textRight = this.buttonRight.GetComponentInChildren<UILabel>();
			this.textLeft = this.buttonLeft.GetComponentInChildren<UILabel>();
			EventDelegate.Add(this.buttonRight.onClick, delegate()
			{
				if (this.onClickIsLeft != null)
				{
					this.onClickIsLeft(false);
				}
				this.OnClickButton(false);
			});
			EventDelegate.Add(this.buttonLeft.onClick, delegate()
			{
				if (this.onClickIsLeft != null)
				{
					this.onClickIsLeft(true);
				}
				this.OnClickButton(true);
			});
		}

		private void OnClickButton(bool isLeft)
		{
			this.buttonLeft.defaultColor = ((!isLeft) ? VRFaceShortcutConfig.ItemSelectButton.ButtonDefaultColor : VRFaceShortcutConfig.ItemSelectButton.ButtonClickedColor);
			this.buttonRight.defaultColor = (isLeft ? VRFaceShortcutConfig.ItemSelectButton.ButtonDefaultColor : VRFaceShortcutConfig.ItemSelectButton.ButtonClickedColor);
		}

		public void Emulate(bool isLeft)
		{
			UIButton uibutton = (!isLeft) ? this.buttonRight : this.buttonLeft;
			EventDelegate.Execute(uibutton.onClick);
		}

		public override void Set(object value)
		{
			this.Emulate((bool)value);
		}

		private static Color ButtonDefaultColor = new Color(1f, 1f, 1f, 0.5f);

		private static Color ButtonClickedColor = Color.white;

		private UIButton m_ButtonRight;

		private UIButton m_ButtonLeft;

		private UILabel m_TextRight;

		private UILabel m_TextLeft;
	}

	private class ParentMultiButton : VRFaceShortcutConfig.Item
	{
		public UIButton buttonRight
		{
			get
			{
				return this.m_ButtonRight;
			}
			private set
			{
				this.m_ButtonRight = value;
			}
		}

		public UIButton buttonLeft
		{
			get
			{
				return this.m_ButtonLeft;
			}
			private set
			{
				this.m_ButtonLeft = value;
			}
		}

		public UIButton buttonCenter
		{
			get
			{
				return this.m_ButtonCenter;
			}
			private set
			{
				this.m_ButtonCenter = value;
			}
		}

		public UILabel textRight
		{
			get
			{
				return this.m_TextRight;
			}
			private set
			{
				this.m_TextRight = value;
			}
		}

		public UILabel textLeft
		{
			get
			{
				return this.m_TextLeft;
			}
			private set
			{
				this.m_TextLeft = value;
			}
		}

		public UILabel textCenter
		{
			get
			{
				return this.m_TextCenter;
			}
			private set
			{
				this.m_TextCenter = value;
			}
		}

		public Action<int> onClick { get; set; }

		protected override void Awake()
		{
			base.Awake();
			this.buttonRight = VRFaceShortcutConfig.GetChildObject<UIButton>(base.gameObject, "button right");
			this.buttonLeft = VRFaceShortcutConfig.GetChildObject<UIButton>(base.gameObject, "button left");
			this.buttonCenter = VRFaceShortcutConfig.GetChildObject<UIButton>(base.gameObject, "button center");
			this.textRight = this.buttonRight.GetComponentInChildren<UILabel>();
			this.textLeft = this.buttonLeft.GetComponentInChildren<UILabel>();
			this.textCenter = this.buttonCenter.GetComponentInChildren<UILabel>();
			EventDelegate.Add(this.buttonRight.onClick, delegate()
			{
				if (this.onClick != null)
				{
					this.onClick(0);
				}
				this.OnClickButton(0);
			});
			EventDelegate.Add(this.buttonLeft.onClick, delegate()
			{
				if (this.onClick != null)
				{
					this.onClick(1);
				}
				this.OnClickButton(1);
			});
			EventDelegate.Add(this.buttonCenter.onClick, delegate()
			{
				if (this.onClick != null)
				{
					this.onClick(2);
				}
				this.OnClickButton(2);
			});
		}

		private void OnClickButton(int index)
		{
			this.buttonRight.defaultColor = ((index != 0) ? VRFaceShortcutConfig.ParentMultiButton.ButtonDefaultColor : VRFaceShortcutConfig.ParentMultiButton.ButtonClickedColor);
			this.buttonLeft.defaultColor = ((index != 1) ? VRFaceShortcutConfig.ParentMultiButton.ButtonDefaultColor : VRFaceShortcutConfig.ParentMultiButton.ButtonClickedColor);
			this.buttonCenter.defaultColor = ((index != 2) ? VRFaceShortcutConfig.ParentMultiButton.ButtonDefaultColor : VRFaceShortcutConfig.ParentMultiButton.ButtonClickedColor);
		}

		public void Emulate(int index)
		{
			UIButton uibutton;
			if (index == 0)
			{
				uibutton = this.buttonRight;
			}
			else if (index == 1)
			{
				uibutton = this.buttonLeft;
			}
			else
			{
				uibutton = this.buttonCenter;
			}
			EventDelegate.Execute(uibutton.onClick);
		}

		public override void Set(object value)
		{
			this.Emulate((int)value);
		}

		private static Color ButtonDefaultColor = new Color(1f, 1f, 1f, 0.5f);

		private static Color ButtonClickedColor = Color.white;

		private UIButton m_ButtonRight;

		private UIButton m_ButtonLeft;

		private UIButton m_ButtonCenter;

		private UILabel m_TextRight;

		private UILabel m_TextLeft;

		private UILabel m_TextCenter;
	}
}
