using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

public class ConfigManager : MonoBehaviour
{
	public UIPopupList subtitleTypePopupList
	{
		get
		{
			return this.subtitleType;
		}
	}

	public void Init(ConfigCblManager cblConfigMgr)
	{
		this.cblConfigMgr = cblConfigMgr;
		ConfigManager.CategoryButton[] array = this.categoryTabButtons;
		for (int i = 0; i < array.Length; i++)
		{
			ConfigManager.CategoryButton categoryData = array[i];
			if (categoryData.type == ConfigManager.CategoryButton.Type.System)
			{
				this.OnClickCategoryTabButton(categoryData);
			}
			EventDelegate.Add(categoryData.tab.onClick, delegate()
			{
				this.OnClickCategoryTabButton(categoryData);
			});
			if (categoryData.type == ConfigManager.CategoryButton.Type.ChuBLip)
			{
				categoryData.tab.gameObject.SetActive(false);
			}
		}
		KeyValuePair<UIGrid, GameObject> rootItem = this.GetRootItem<UIGrid>(this.deviceSelect.gameObject);
		UIGrid key = rootItem.Key;
		GameObject value = rootItem.Value;
		if (value != null)
		{
			value.gameObject.SetActive(false);
		}
		if (key != null)
		{
			key.repositionNow = true;
		}
		this.systemFrame.height -= 72;
		EventDelegate.Add(this.closeButton.onClick, new EventDelegate.Callback(BaseMgr<ConfigMgr>.Instance.CloseConfigPanel));
		EventDelegate.Add(this.vrConfigButton.onClick, delegate()
		{
			BaseMgr<ConfigMgr>.Instance.CloseConfigPanel();
			ConfigVRCtrl.Open(null);
		});
		Action<ConfigSelectButton> action = delegate(ConfigSelectButton button)
		{
			button.Initialize();
			this.selectButtons.Add(button);
		};
		Action<ConfigIntegerSlider> intSliderInit = delegate(ConfigIntegerSlider slider)
		{
			slider.Initialize();
			this.intSliders.Add(slider);
		};
		Action<ConfigIntegerSlider, AudioSourceMgr.Type> action2 = delegate(ConfigIntegerSlider slider, AudioSourceMgr.Type type)
		{
			intSliderInit(slider);
			slider.onGetValue = (() => GameMain.Instance.SoundMgr.GetVolume(type));
			slider.onSetValue = delegate(int setValue)
			{
				GameMain.Instance.SoundMgr.SetVolume(type, setValue);
				GameMain.Instance.SoundMgr.Apply();
			};
		};
		Action<WFCheckBox, AudioSourceMgr.Type> action3 = delegate(WFCheckBox checkBox, AudioSourceMgr.Type type)
		{
			checkBox.onClick.Add(delegate(WFCheckBox clickCheckBox)
			{
				GameMain.Instance.SoundMgr.SetThreeD(type, clickCheckBox.check);
				GameMain.Instance.SoundMgr.Apply();
			});
		};
		action(this.systemButtonVisible);
		this.systemButtonVisible.onGetValue = (() => GameMain.Instance.CMSystem.SysButtonShowAlways);
		this.systemButtonVisible.onSetValue = delegate(bool setValue)
		{
			GameMain.Instance.CMSystem.SysButtonShowAlways = setValue;
		};
		action(this.screenMode);
		this.screenMode.onGetValue = (() => GameMain.Instance.CMSystem.FullScreen);
		this.screenMode.onSetValue = delegate(bool setValue)
		{
			GameMain.Instance.CMSystem.FullScreen = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		};
		action(this.drawFPS);
		this.drawFPS.onGetValue = (() => GameMain.Instance.CMSystem.ViewFps);
		this.drawFPS.onSetValue = delegate(bool setValue)
		{
			GameMain.Instance.CMSystem.ViewFps = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		};
		action(this.sequentialVoice);
		this.sequentialVoice.onGetValue = (() => GameMain.Instance.CMSystem.MsgVoiceNoStop);
		this.sequentialVoice.onSetValue = delegate(bool setValue)
		{
			GameMain.Instance.CMSystem.MsgVoiceNoStop = setValue;
		};
		action(this.ejaculationSe);
		this.ejaculationSe.onGetValue = (() => GameMain.Instance.CMSystem.EjaculationSeEnabled);
		this.ejaculationSe.onSetValue = delegate(bool setValue)
		{
			GameMain.Instance.CMSystem.EjaculationSeEnabled = this.enabled;
		};
		foreach (Size<int> size in this.MakeResolutionPopupList())
		{
			this.screenResolution.items.Add(string.Format("{0}x{1}", size.width, size.height));
		}
		EventDelegate.Add(this.screenResolution.onChange, delegate()
		{
			string currentPopupListValue = this.GetCurrentPopupListValue();
			Size<int> screenSize = this.ResolutionTextToSize(currentPopupListValue);
			GameMain.Instance.CMSystem.SetScreenSize(screenSize);
			GameMain.Instance.CMSystem.ConfigScreenApply();
		});
		this.CreateEnumPopupList<CMSystem.SSSuperSizeType>(this.captureMagnification, delegate(CMSystem.SSSuperSizeType setValue)
		{
			GameMain.Instance.CMSystem.ScreenShotSuperSize = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		});
		this.CreateEnumPopupList<CMSystem.AntiAliasType>(this.antiAlias, delegate(CMSystem.AntiAliasType setValue)
		{
			GameMain.Instance.CMSystem.Antialias = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		});
		this.CreateEnumPopupList<CMSystem.ShadowQualityType>(this.shadowQuality, delegate(CMSystem.ShadowQualityType setValue)
		{
			GameMain.Instance.CMSystem.ShadowQuality = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		});
		this.CreateEnumPopupList<CMSystem.TextureQualityType>(this.textureQuality, delegate(CMSystem.TextureQualityType setValue)
		{
			GameMain.Instance.CMSystem.TextureQuality = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		});
		if (this.subtitleType != null)
		{
			if (GameUty.supportMultiLanguage)
			{
				IEnumerator enumerator2 = Enum.GetValues(typeof(SubtitleDisplayManager.DisplayType)).GetEnumerator();
				try
				{
					while (enumerator2.MoveNext())
					{
						object obj = enumerator2.Current;
						this.subtitleType.AddItem(SubtitleDisplayManager.EnumConvert.GetTerm((SubtitleDisplayManager.DisplayType)obj));
					}
				}
				finally
				{
					IDisposable disposable;
					if ((disposable = (enumerator2 as IDisposable)) != null)
					{
						disposable.Dispose();
					}
				}
				EventDelegate.Add(this.subtitleType.onChange, delegate()
				{
					string currentPopupListValue = this.GetCurrentPopupListValue();
					IEnumerator enumerator3 = Enum.GetValues(typeof(SubtitleDisplayManager.DisplayType)).GetEnumerator();
					try
					{
						while (enumerator3.MoveNext())
						{
							object obj2 = enumerator3.Current;
							string term = SubtitleDisplayManager.EnumConvert.GetTerm((SubtitleDisplayManager.DisplayType)obj2);
							if (currentPopupListValue == term)
							{
								GameMain.Instance.CMSystem.SubtitleType = (SubtitleDisplayManager.DisplayType)obj2;
								break;
							}
						}
					}
					finally
					{
						IDisposable disposable2;
						if ((disposable2 = (enumerator3 as IDisposable)) != null)
						{
							disposable2.Dispose();
						}
					}
				});
			}
			else
			{
				Transform transform = this.subtitleType.transform;
				while (transform.parent == null || transform.parent.GetComponent<UIGrid>() == null)
				{
					transform = transform.parent;
				}
				if (transform != null)
				{
					transform.gameObject.SetActive(false);
				}
			}
		}
		intSliderInit(this.manAlpha);
		this.manAlpha.onGetValue = (() => GameMain.Instance.CMSystem.ManAlpha);
		this.manAlpha.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.CMSystem.ManAlpha = setValue;
			GameMain.Instance.CMSystem.ConfigScreenApply();
		};
		intSliderInit(this.bloom);
		this.bloom.onGetValue = (() => GameMain.Instance.CMSystem.BloomValue);
		this.bloom.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.CMSystem.BloomValue = setValue;
		};
		intSliderInit(this.messageAlpha);
		this.messageAlpha.onGetValue = (() => GameMain.Instance.CMSystem.MsgWndAlpha);
		this.messageAlpha.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.CMSystem.MsgWndAlpha = setValue;
		};
		intSliderInit(this.textSpeed);
		this.textSpeed.onGetValue = (() => GameMain.Instance.CMSystem.MsgTextSpeed);
		this.textSpeed.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.CMSystem.MsgTextSpeed = setValue;
		};
		intSliderInit(this.textAutoSpeed);
		this.textAutoSpeed.onGetValue = (() => GameMain.Instance.CMSystem.MsgAutoSpeed);
		this.textAutoSpeed.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.CMSystem.MsgAutoSpeed = setValue;
		};
		intSliderInit(this.volumeMaster);
		this.volumeMaster.onGetValue = (() => GameMain.Instance.SoundMgr.GetVolumeAll());
		this.volumeMaster.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.SoundMgr.SetVolumeAll(setValue);
			GameMain.Instance.SoundMgr.Apply();
		};
		action2(this.volumeSystem, AudioSourceMgr.Type.System);
		action2(this.volumeBGM, AudioSourceMgr.Type.Bgm);
		intSliderInit(this.volumeDance);
		this.volumeDance.onGetValue = (() => GameMain.Instance.SoundMgr.GetVolumeDance());
		this.volumeDance.onSetValue = delegate(int setValue)
		{
			GameMain.Instance.SoundMgr.SetVolumeDance(setValue);
			GameMain.Instance.SoundMgr.Apply();
		};
		action2(this.volumeVoiceMaster, AudioSourceMgr.Type.Voice);
		action2(this.volumeVoiceHeroine, AudioSourceMgr.Type.VoiceHeroine);
		action3(this.threeDEffectVoiceHeroine, AudioSourceMgr.Type.VoiceHeroine);
		action2(this.volumeSubVoice, AudioSourceMgr.Type.VoiceSub);
		action3(this.threeDEffectSubVoice, AudioSourceMgr.Type.VoiceSub);
		action2(this.volumeExtraVoice, AudioSourceMgr.Type.VoiceExtra);
		action3(this.threeDEffectExtraVoice, AudioSourceMgr.Type.VoiceExtra);
		action2(this.volumeMobVoice, AudioSourceMgr.Type.VoiceMob);
		action3(this.threeDEffectMobVoice, AudioSourceMgr.Type.VoiceMob);
		action2(this.volumeSE, AudioSourceMgr.Type.Se);
		action3(this.threeDEffectSE, AudioSourceMgr.Type.Se);
		action2(this.volumeEnv, AudioSourceMgr.Type.Env);
		action3(this.threeDEffectEnv, AudioSourceMgr.Type.Env);
	}

	public void LoadConfig()
	{
		foreach (ConfigSelectButton configSelectButton in this.selectButtons)
		{
			configSelectButton.UpdateButton();
		}
		foreach (ConfigIntegerSlider configIntegerSlider in this.intSliders)
		{
			configIntegerSlider.UpdateSlider();
		}
		SoundMgr soundMgr = GameMain.Instance.SoundMgr;
		this.threeDEffectVoiceHeroine.check = soundMgr.GetThreeD(AudioSourceMgr.Type.VoiceHeroine);
		this.threeDEffectSubVoice.check = soundMgr.GetThreeD(AudioSourceMgr.Type.VoiceSub);
		this.threeDEffectExtraVoice.check = soundMgr.GetThreeD(AudioSourceMgr.Type.VoiceExtra);
		this.threeDEffectMobVoice.check = soundMgr.GetThreeD(AudioSourceMgr.Type.VoiceMob);
		this.threeDEffectSE.check = soundMgr.GetThreeD(AudioSourceMgr.Type.Se);
		this.threeDEffectEnv.check = soundMgr.GetThreeD(AudioSourceMgr.Type.Env);
		Size<int> screenSizeNow = GameMain.Instance.CMSystem.GetScreenSizeNow();
		this.screenResolution.value = string.Format("{0}x{1}", screenSizeNow.width, screenSizeNow.height);
		this.captureMagnification.value = GameMain.Instance.CMSystem.ScreenShotSuperSize.ToString();
		this.antiAlias.value = GameMain.Instance.CMSystem.Antialias.ToString();
		this.shadowQuality.value = GameMain.Instance.CMSystem.ShadowQuality.ToString();
		this.textureQuality.value = GameMain.Instance.CMSystem.TextureQuality.ToString();
	}

	public void SetCloseButtonActive(bool active)
	{
		this.closeButton.gameObject.SetActive(active);
	}

	private void OnClickCategoryTabButton(ConfigManager.CategoryButton data)
	{
		ConfigManager.CategoryButton.Type type = data.type;
		if (type != ConfigManager.CategoryButton.Type.System)
		{
			if (type != ConfigManager.CategoryButton.Type.Sound)
			{
				if (type == ConfigManager.CategoryButton.Type.ChuBLip)
				{
					UIRect uirect = this.systemWidget;
					float alpha = 0f;
					this.soundWidget.alpha = alpha;
					uirect.alpha = alpha;
					this.cblConfigMgr.visible = true;
				}
			}
			else
			{
				this.systemWidget.alpha = 0f;
				this.soundWidget.alpha = 1f;
				this.cblConfigMgr.visible = false;
			}
		}
		else
		{
			this.systemWidget.alpha = 1f;
			this.soundWidget.alpha = 0f;
			this.cblConfigMgr.visible = false;
		}
		foreach (ConfigManager.CategoryButton categoryButton in this.categoryTabButtons)
		{
			categoryButton.tab.SetSelect(categoryButton.type == data.type);
		}
	}

	private void CreateEnumPopupList<enumT>(UIPopupList addPopupList, Action<enumT> applyAction)
	{
		IEnumerator enumerator = Enum.GetValues(typeof(enumT)).GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				if (obj.ToString() != "MAX")
				{
					addPopupList.items.Add(obj.ToString());
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		EventDelegate.Add(addPopupList.onChange, delegate()
		{
			string currentPopupListValue = this.GetCurrentPopupListValue();
			if (this.IsValidatePopupValue<enumT>(currentPopupListValue))
			{
				enumT obj2 = (enumT)((object)Enum.Parse(typeof(enumT), currentPopupListValue));
				applyAction(obj2);
			}
		});
	}

	private string GetCurrentPopupListValue()
	{
		return Regex.Replace(UIPopupList.current.value, "[\\r\\n]+$", string.Empty);
	}

	private List<Size<int>> MakeResolutionPopupList()
	{
		List<Size<int>> screenSizeList = GameMain.Instance.CMSystem.GetScreenSizeList();
		Size<int> screenSizeNow = GameMain.Instance.CMSystem.GetScreenSizeNow();
		if (!screenSizeList.Contains(screenSizeNow))
		{
			screenSizeList.Add(screenSizeNow);
		}
		return screenSizeList;
	}

	private Size<int> ResolutionTextToSize(string value)
	{
		Size<int> result = default(Size<int>);
		int num = value.IndexOf("x");
		if (num < 0)
		{
			Debug.LogError(string.Format("不適切な解像度が設定されています。設定された解像度={0}", value));
		}
		string s = value.Substring(0, num);
		int width;
		if (int.TryParse(s, out width))
		{
			result.width = width;
		}
		else
		{
			Debug.LogError(string.Format("不適切な解像度が設定されています。設定された解像度={0}", value));
		}
		string s2 = value.Substring(num + 1);
		int height;
		if (int.TryParse(s2, out height))
		{
			result.height = height;
		}
		else
		{
			Debug.LogError(string.Format("不適切な解像度が設定されています。設定された解像度={0}", value));
		}
		return result;
	}

	private bool IsValidatePopupValue<T>(string value)
	{
		if (!Enum.IsDefined(typeof(T), value))
		{
			Debug.LogError(string.Format("ポップアップリストから不適切な値が選択されました。選択されたボタン名={0}", value));
			return false;
		}
		return true;
	}

	private KeyValuePair<T, GameObject> GetRootItem<T>(GameObject obj) where T : class
	{
		T t = (T)((object)null);
		Transform transform = obj.transform;
		for (;;)
		{
			t = transform.parent.GetComponent<T>();
			if (!(transform.parent == null) && t != null)
			{
				break;
			}
			transform = transform.parent;
		}
		if (transform != null)
		{
			transform.gameObject.SetActive(false);
		}
		return new KeyValuePair<T, GameObject>(t, transform.gameObject);
	}

	[SerializeField]
	[Header("タブボタンのデータ")]
	private ConfigManager.CategoryButton[] categoryTabButtons;

	[SerializeField]
	[Header("閉じるボタン")]
	private UIButton closeButton;

	[SerializeField]
	[Header("VRボタン")]
	private UIButton vrConfigButton;

	[SerializeField]
	[Header("システムタブ表示用Widget")]
	private UIWidget systemWidget;

	[SerializeField]
	[Header("システムフレーム")]
	private UISprite systemFrame;

	[SerializeField]
	[Header("サウンドタブ表示用Widget")]
	private UIWidget soundWidget;

	[SerializeField]
	[Header("システムボタン常時表示")]
	private ConfigSelectButton systemButtonVisible;

	[SerializeField]
	[Header("スクリーンモード")]
	private ConfigSelectButton screenMode;

	[SerializeField]
	[Header("FPS表示")]
	private ConfigSelectButton drawFPS;

	[SerializeField]
	[Header("ボイス継続")]
	private ConfigSelectButton sequentialVoice;

	[SerializeField]
	[Header("射精音の再生")]
	private ConfigSelectButton ejaculationSe;

	[SerializeField]
	[Header("男透明度")]
	private ConfigIntegerSlider manAlpha;

	[SerializeField]
	[Header("ブルーム")]
	private ConfigIntegerSlider bloom;

	[SerializeField]
	[Header("メッセージウィンドウ透明度")]
	private ConfigIntegerSlider messageAlpha;

	[SerializeField]
	[Header("テキストスピード")]
	private ConfigIntegerSlider textSpeed;

	[SerializeField]
	[Header("テキストオートスピード")]
	private ConfigIntegerSlider textAutoSpeed;

	[SerializeField]
	[Header("CBLデバイス選択")]
	private UIPopupList deviceSelect;

	[SerializeField]
	[Header("画面解像度")]
	private UIPopupList screenResolution;

	[SerializeField]
	[Header("キャプチャ倍率")]
	private UIPopupList captureMagnification;

	[SerializeField]
	[Header("アンチエイリアス")]
	private UIPopupList antiAlias;

	[SerializeField]
	[Header("影のクオリティ")]
	private UIPopupList shadowQuality;

	[SerializeField]
	[Header("テクスチャのクオリティ")]
	private UIPopupList textureQuality;

	[SerializeField]
	[Header("翻訳版用-ADVの表示タイプ")]
	private UIPopupList subtitleType;

	[SerializeField]
	[Header("音量-マスター")]
	private ConfigIntegerSlider volumeMaster;

	[SerializeField]
	[Header("音量-システム")]
	private ConfigIntegerSlider volumeSystem;

	[SerializeField]
	[Header("音量-BGM")]
	private ConfigIntegerSlider volumeBGM;

	[SerializeField]
	[Header("音量-ダンス")]
	private ConfigIntegerSlider volumeDance;

	[SerializeField]
	[Header("音量-ボイスマスター")]
	private ConfigIntegerSlider volumeVoiceMaster;

	[SerializeField]
	[Header("音量-ボイスヒロイン")]
	private ConfigIntegerSlider volumeVoiceHeroine;

	[SerializeField]
	[Header("3D-ボイスヒロイン")]
	private WFCheckBox threeDEffectVoiceHeroine;

	[SerializeField]
	[Header("音量-ボイスサブ")]
	private ConfigIntegerSlider volumeSubVoice;

	[SerializeField]
	[Header("3D-ボイスサブ")]
	private WFCheckBox threeDEffectSubVoice;

	[SerializeField]
	[Header("音量-ボイスエキストラ")]
	private ConfigIntegerSlider volumeExtraVoice;

	[SerializeField]
	[Header("3D-ボイスエキストラ")]
	private WFCheckBox threeDEffectExtraVoice;

	[SerializeField]
	[Header("音量-ボイスモブ")]
	private ConfigIntegerSlider volumeMobVoice;

	[SerializeField]
	[Header("3D-ボイスモブ")]
	private WFCheckBox threeDEffectMobVoice;

	[SerializeField]
	[Header("音量-SE")]
	private ConfigIntegerSlider volumeSE;

	[SerializeField]
	[Header("3D-SE")]
	private WFCheckBox threeDEffectSE;

	[SerializeField]
	[Header("音量-環境音")]
	private ConfigIntegerSlider volumeEnv;

	[SerializeField]
	[Header("3D-環境音")]
	private WFCheckBox threeDEffectEnv;

	private ConfigCblManager cblConfigMgr;

	private List<ConfigSelectButton> selectButtons = new List<ConfigSelectButton>();

	private List<ConfigIntegerSlider> intSliders = new List<ConfigIntegerSlider>();

	[Serializable]
	private struct CategoryButton
	{
		public ConfigManager.CategoryButton.Type type;

		public UIWFTabButton tab;

		public enum Type
		{
			System = 10,
			Sound = 20,
			ChuBLip = 30
		}
	}
}
