using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Dance;
using I2.Loc;
using UnityEngine;

public class DanceSetting : MonoBehaviour
{
	private void Awake()
	{
		this.m_OrijinBgheight = this.m_SettingUIBg.height;
		if (!DanceSetting.m_IsFileCheck)
		{
			Action<string, bool> action = delegate(string file_path, bool binary_ver)
			{
				float num = -1f;
				FileInfo fileInfo = new FileInfo(file_path);
				try
				{
					if (binary_ver)
					{
						using (FileStream fileStream = new FileStream(file_path, FileMode.Open, FileAccess.Read))
						{
							BinaryFormatter binaryFormatter = new BinaryFormatter();
							string text = (string)binaryFormatter.Deserialize(fileStream);
							string text2 = (string)binaryFormatter.Deserialize(fileStream);
							float.TryParse(text2.Split(new char[]
							{
								':'
							})[1], out num);
							DanceSetting.Settings = (SettingData)binaryFormatter.Deserialize(fileStream);
							fileStream.Close();
						}
					}
					else
					{
						using (StreamReader streamReader = new StreamReader(fileInfo.OpenRead(), Encoding.UTF8))
						{
							string text3 = streamReader.ReadLine();
							if (text3 == "@Dance_Setting")
							{
								string s = streamReader.ReadLine().Split(new char[]
								{
									':'
								})[1];
								float.TryParse(s, out num);
								text3 = streamReader.ReadLine();
							}
							DanceSetting.Settings = JsonUtility.FromJson<SettingData>(text3);
							streamReader.Close();
						}
					}
				}
				catch (Exception ex)
				{
					if (num > 0f)
					{
						if (num == 1f)
						{
							Debug.Log("2017/11/22以前の設定データです");
						}
					}
					else
					{
						DanceSetting.Settings = new SettingData();
						File.Delete(file_path);
						Debug.LogWarning("ダンス設定読み込みに失敗したので初期化します。エラー箇所:" + ex.StackTrace);
					}
				}
			};
			string fullPath = Path.GetFullPath(".\\dance_setting_br.dat");
			DanceSetting.m_IsFileExist = File.Exists(fullPath);
			if (DanceSetting.m_IsFileExist)
			{
				action(fullPath, true);
			}
			else
			{
				string fullPath2 = Path.GetFullPath(".\\dance_setting.set");
				string fullPath3 = Path.GetFullPath(".\\dance_setting.dat");
				if (File.Exists(fullPath2))
				{
					action(fullPath2, false);
					File.Delete(fullPath2);
				}
				else if (File.Exists(fullPath3))
				{
					action(fullPath3, false);
					File.Delete(fullPath3);
				}
			}
			DanceSetting.m_IsFileCheck = true;
		}
	}

	private void Start()
	{
		EventDelegate.Callback callback = delegate()
		{
			GameObject gameObject = GameObject.Find("Drop-down List");
			if (gameObject)
			{
				int width = gameObject.GetComponentInChildren<UISprite>().width;
				gameObject.transform.position = UIButton.current.transform.TransformPoint(Vector3.down * this.m_PopUpOffset + Vector3.left * (float)width / 2f);
			}
		};
		IEnumerator enumerator = this.m_ButtonGroup.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				UIPopupList componentInChildren = transform.GetComponentInChildren<UIPopupList>();
				if (componentInChildren)
				{
					DanceSetting.NeedData needData = new DanceSetting.NeedData();
					if (transform.Find("Label"))
					{
						needData.Label = transform.Find("Label").GetComponent<UILabel>();
						needData.localize = needData.Label.GetComponent<Localize>();
					}
					needData.Button = transform.GetComponentInChildren<UIButton>();
					EventDelegate.Add(needData.Button.onClick, callback);
					needData.Collider = transform.GetComponentInChildren<BoxCollider>();
					this.m_SettingUIList.Add(componentInChildren, needData);
					EventDelegate.Add(componentInChildren.onChange, new EventDelegate.Callback(this.ValueSetting));
					this.SetUIValue(componentInChildren);
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	private void SetUIValue(UIPopupList list)
	{
		DanceSetting.UIInspectorSetting uiinspectorSetting = this.m_SettingList.SingleOrDefault((DanceSetting.UIInspectorSetting e) => e.PopUpUI == list);
		if (uiinspectorSetting == null)
		{
			return;
		}
		foreach (string text in list.items)
		{
			bool flag = false;
			bool flag2 = false;
			string text2 = text.Replace("\r", string.Empty);
			string text3 = text2;
			string[] array = text2.Split(new char[]
			{
				'/'
			});
			if (array != null && 1 < array.Length)
			{
				text3 = array[array.Length - 1];
				flag2 = true;
			}
			switch (uiinspectorSetting.MyType)
			{
			case DanceSetting.SettingType.Product:
				flag = this.IsProductMatch(text3);
				break;
			case DanceSetting.SettingType.Audience:
				flag = this.IsAudienceMatch(text3);
				break;
			case DanceSetting.SettingType.DepthofField:
				flag = this.IsSettingOn(text3, DanceSetting.Settings.IsDepthOfFieldOn);
				break;
			case DanceSetting.SettingType.SePlay:
				flag = this.IsSettingOn(text3, DanceSetting.Settings.IsSEPlay);
				break;
			case DanceSetting.SettingType.Voltage:
				flag = this.IsVoltageMatch(text3);
				break;
			case DanceSetting.SettingType.BlackBg:
				flag = this.IsSettingOn(text3, DanceSetting.Settings.IsblackBGon);
				break;
			case DanceSetting.SettingType.NoteEffect:
				flag = this.IsEffectLigthtMatch(text3);
				break;
			case DanceSetting.SettingType.NoteTiming:
				flag = this.IsTimingMatch(text3);
				break;
			case DanceSetting.SettingType.JudgeTiming:
				flag = this.IsJudgeTimingMatch(text3);
				break;
			case DanceSetting.SettingType.FpsCam:
				flag = this.IsSettingOn(text3, DanceSetting.Settings.FPSCamMode);
				break;
			case DanceSetting.SettingType.Cutin:
				flag = this.IsCutinMatch(text3);
				break;
			case DanceSetting.SettingType.Undress:
				flag = this.IsUndressFaceMatch(text3);
				break;
			}
			if (flag)
			{
				list.value = ((!flag2) ? text3 : text2);
				break;
			}
		}
	}

	private bool IsProductMatch(string text)
	{
		bool result = false;
		switch (DanceSetting.Settings.Product)
		{
		case Product_Mgr.ProductRank.Low:
			result = (text == "下位");
			break;
		case Product_Mgr.ProductRank.Special:
			result = (text == "上位");
			break;
		case Product_Mgr.ProductRank.Normal:
			result = (text == "通常");
			break;
		}
		return result;
	}

	private bool IsAudienceMatch(string text)
	{
		bool result = false;
		switch (DanceSetting.Settings.Audience)
		{
		case Product_Mgr.AudienceState.Few:
			result = (text == "小");
			break;
		case Product_Mgr.AudienceState.Normal:
			result = (text == "中");
			break;
		case Product_Mgr.AudienceState.Many:
			result = (text == "大");
			break;
		}
		return result;
	}

	private bool IsSettingOn(string text, bool condition)
	{
		if (condition)
		{
			return text == "ON";
		}
		return text == "OFF";
	}

	private bool IsVoltageMatch(string text)
	{
		int num = 0;
		if (int.TryParse(text, out num))
		{
			return DanceSetting.Settings.Voltage == num;
		}
		if (text == "普通")
		{
			return DanceSetting.Settings.Voltage == 3;
		}
		return text == "難しい" && DanceSetting.Settings.Voltage == 4;
	}

	private bool IsCutinMatch(string text)
	{
		return DanceSetting.Settings.CutInGrade == Appeal_Mgr.ParseGrade(text);
	}

	private bool IsTimingMatch(string text)
	{
		return DanceSetting.Settings.NoteOccurTiming.ToString() == text.Replace("+", string.Empty).Replace("秒", string.Empty);
	}

	private bool IsEffectLigthtMatch(string text)
	{
		if (text == "軽量")
		{
			return DanceSetting.Settings.IsNoteEffectLight;
		}
		return !DanceSetting.Settings.IsNoteEffectLight;
	}

	private bool IsUndressFaceMatch(string text)
	{
		if (text == "通常")
		{
			return DanceSetting.Settings.UndressFaceOn;
		}
		return !DanceSetting.Settings.UndressFaceOn;
	}

	private bool IsJudgeTimingMatch(string text)
	{
		return DanceSetting.Settings.NoteJudgeTiming == float.Parse(text.Replace("秒", string.Empty));
	}

	private void OnDestroy()
	{
		using (FileStream fileStream = new FileStream(Path.GetFullPath(".\\dance_setting_br.dat"), FileMode.Create, FileAccess.Write))
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			binaryFormatter.Serialize(fileStream, "@Dance_Setting");
			binaryFormatter.Serialize(fileStream, string.Format("Ver:{0}", 1.7f));
			binaryFormatter.Serialize(fileStream, DanceSetting.Settings);
			fileStream.Close();
		}
		DanceSetting.m_IsFileExist = true;
	}

	private void SwitchFlag(string value_text, ref bool swtch_flag)
	{
		if (value_text != null)
		{
			if (!(value_text == "ON"))
			{
				if (value_text == "OFF")
				{
					swtch_flag = false;
				}
			}
			else
			{
				swtch_flag = true;
			}
		}
	}

	private void SetProduct(string value_text)
	{
		Product_Mgr.ProductRank product = Product_Mgr.ProductRank.Low;
		if (value_text != null)
		{
			if (!(value_text == "下位"))
			{
				if (!(value_text == "通常"))
				{
					if (value_text == "上位")
					{
						product = Product_Mgr.ProductRank.Special;
					}
				}
				else
				{
					product = Product_Mgr.ProductRank.Normal;
				}
			}
			else
			{
				product = Product_Mgr.ProductRank.Low;
			}
		}
		DanceSetting.Settings.Product = product;
	}

	private void SetAudience(string value_text)
	{
		Product_Mgr.AudienceState audience = Product_Mgr.AudienceState.Normal;
		if (value_text != null)
		{
			if (!(value_text == "小"))
			{
				if (!(value_text == "中"))
				{
					if (value_text == "大")
					{
						audience = Product_Mgr.AudienceState.Many;
					}
				}
				else
				{
					audience = Product_Mgr.AudienceState.Normal;
				}
			}
			else
			{
				audience = Product_Mgr.AudienceState.Few;
			}
		}
		DanceSetting.Settings.Audience = audience;
	}

	private void SetVoltage(string value_text)
	{
		int voltage = 0;
		if (int.TryParse(value_text, out voltage))
		{
			DanceSetting.Settings.Voltage = voltage;
		}
		else if (value_text != null)
		{
			if (!(value_text == "普通"))
			{
				if (value_text == "難しい")
				{
					DanceSetting.Settings.Voltage = 4;
				}
			}
			else
			{
				DanceSetting.Settings.Voltage = 3;
			}
		}
	}

	private void SetCutInGrade(string value_text)
	{
		DanceSetting.Settings.CutInGrade = Appeal_Mgr.ParseGrade(value_text);
	}

	private void SetNoteTiming(string value_text)
	{
		DanceSetting.Settings.NoteOccurTiming = int.Parse(value_text.Replace("+", string.Empty));
	}

	private void SetEffectLight(string value_text)
	{
		if (value_text != null)
		{
			if (!(value_text == "通常"))
			{
				if (value_text == "軽量")
				{
					DanceSetting.Settings.IsNoteEffectLight = true;
				}
			}
			else
			{
				DanceSetting.Settings.IsNoteEffectLight = false;
			}
		}
	}

	private void SetUndressFace(string value_text)
	{
		if (value_text != null)
		{
			if (!(value_text == "通常"))
			{
				if (value_text == "我慢")
				{
					DanceSetting.Settings.UndressFaceOn = false;
				}
			}
			else
			{
				DanceSetting.Settings.UndressFaceOn = true;
			}
		}
	}

	private void SetJudgeTiming(string value_text)
	{
		DanceSetting.Settings.NoteJudgeTiming = float.Parse(value_text);
	}

	private void ValueSetting()
	{
		string text = UIPopupList.current.value.Replace("\r", string.Empty);
		string text2 = text;
		bool isLocalized = UIPopupList.current.isLocalized;
		if (isLocalized)
		{
			string[] array = text2.Split(new char[]
			{
				'/'
			});
			if (array != null && 1 < array.Length)
			{
				text2 = array[array.Length - 1];
			}
		}
		DanceSetting.UIInspectorSetting uiinspectorSetting = this.m_SettingList.SingleOrDefault((DanceSetting.UIInspectorSetting e) => e.PopUpUI == UIPopupList.current);
		if (uiinspectorSetting == null)
		{
			return;
		}
		switch (uiinspectorSetting.MyType)
		{
		case DanceSetting.SettingType.Product:
			this.SetProduct(text2);
			break;
		case DanceSetting.SettingType.Audience:
			this.SetAudience(text2);
			break;
		case DanceSetting.SettingType.DepthofField:
			this.SwitchFlag(text2, ref DanceSetting.Settings.IsDepthOfFieldOn);
			break;
		case DanceSetting.SettingType.SePlay:
			this.SwitchFlag(text2, ref DanceSetting.Settings.IsSEPlay);
			break;
		case DanceSetting.SettingType.Voltage:
			this.SetVoltage(text2);
			break;
		case DanceSetting.SettingType.BlackBg:
			this.SwitchFlag(text2, ref DanceSetting.Settings.IsblackBGon);
			break;
		case DanceSetting.SettingType.NoteEffect:
			this.SetEffectLight(text2);
			break;
		case DanceSetting.SettingType.NoteTiming:
			this.SetNoteTiming(text2);
			break;
		case DanceSetting.SettingType.JudgeTiming:
			this.SetJudgeTiming(text2);
			break;
		case DanceSetting.SettingType.FpsCam:
			this.SwitchFlag(text2, ref DanceSetting.Settings.FPSCamMode);
			break;
		case DanceSetting.SettingType.Cutin:
			this.SetCutInGrade(text2);
			break;
		case DanceSetting.SettingType.Undress:
			this.SetUndressFace(text2);
			break;
		}
		if (this.m_SettingUIList[UIPopupList.current].Label)
		{
			if (isLocalized && this.m_SettingUIList[UIPopupList.current].localize != null)
			{
				this.m_IsSettingChange = (this.m_SettingUIList[UIPopupList.current].localize.Term != text);
				this.m_SettingUIList[UIPopupList.current].localize.SetTerm(text);
			}
			else
			{
				this.m_IsSettingChange = (this.m_SettingUIList[UIPopupList.current].Label.text != text);
				this.m_SettingUIList[UIPopupList.current].Label.text = text;
			}
		}
	}

	public void DisSelectable()
	{
		foreach (KeyValuePair<UIPopupList, DanceSetting.NeedData> keyValuePair in this.m_SettingUIList)
		{
			if (keyValuePair.Value.Collider)
			{
				keyValuePair.Value.Collider.enabled = false;
			}
		}
	}

	public void SetUIMode(string mode = "")
	{
		RhythmAction_Mgr.DanceType nowDance = RhythmAction_Mgr.NowDance;
		if (nowDance != RhythmAction_Mgr.DanceType.Challenge)
		{
			if (nowDance != RhythmAction_Mgr.DanceType.VS)
			{
				if (mode.IndexOf("モード") >= 0)
				{
					this.m_ModeLabel.text = mode + "設定";
				}
				else
				{
					this.m_ModeLabel.text = mode + "モード設定";
				}
			}
			else
			{
				this.m_ModeLabel.text = "VSダンス設定";
			}
		}
		else
		{
			this.m_ModeLabel.text = "ダンスショー設定";
		}
		this.m_ModeLabel.GetComponent<Localize>().SetTerm("SceneDanceSelect/" + this.m_ModeLabel.text);
		int num = 0;
		foreach (DanceSetting.UIInspectorSetting uiinspectorSetting in this.m_SettingList)
		{
			GameObject gameObject = uiinspectorSetting.PopUpUI.transform.parent.gameObject;
			bool flag = uiinspectorSetting.OnDanceList.Contains(RhythmAction_Mgr.NowDance);
			if (GameMain.Instance.VRMode)
			{
				flag &= uiinspectorSetting.UseVR;
			}
			gameObject.SetActive(flag);
			if (flag)
			{
				gameObject.transform.localPosition = Vector3.up * (this.m_FirstUIposY - this.m_UISetInterval * (float)num);
				num++;
			}
		}
		this.m_SettingUIBg.height = this.m_OrijinBgheight + num * Mathf.FloorToInt(this.m_UISetInterval);
	}

	private const string m_SettingHeader = "@Dance_Setting";

	private const float m_SettingVer = 1.7f;

	[SerializeField]
	[Header("設定項目設置間隔")]
	private float m_UISetInterval;

	[SerializeField]
	private float m_FirstUIposY;

	[SerializeField]
	[Header("ポップアップ表示オフセット")]
	private float m_PopUpOffset = 27.47f;

	[SerializeField]
	[Header("ボタンUIグループ")]
	private Transform m_ButtonGroup;

	[SerializeField]
	private UILabel m_ModeLabel;

	private Dictionary<UIPopupList, DanceSetting.NeedData> m_SettingUIList = new Dictionary<UIPopupList, DanceSetting.NeedData>();

	[SerializeField]
	[Header("各UI設定")]
	private List<DanceSetting.UIInspectorSetting> m_SettingList = new List<DanceSetting.UIInspectorSetting>
	{
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.Product),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.Audience),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.DepthofField),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.SePlay),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.Voltage),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.BlackBg),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.NoteEffect),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.NoteTiming),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.JudgeTiming),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.FpsCam),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.Cutin),
		new DanceSetting.UIInspectorSetting(DanceSetting.SettingType.Undress)
	};

	private bool m_IsSettingChange;

	[SerializeField]
	[Header("設定UI背景")]
	private UISprite m_SettingUIBg;

	private int m_OrijinBgheight;

	public static SettingData Settings = new SettingData();

	private static bool m_IsFileExist = false;

	private static bool m_IsFileCheck = false;

	private class NeedData
	{
		public UILabel Label;

		public Localize localize;

		public UIButton Button;

		public BoxCollider Collider;

		public string DefoValue;
	}

	public enum SettingType
	{
		Product,
		Audience,
		DepthofField,
		SePlay,
		Voltage,
		BlackBg,
		NoteEffect,
		NoteTiming,
		JudgeTiming,
		FpsCam,
		Cutin,
		Undress,
		AllRelease
	}

	[Serializable]
	private class UIInspectorSetting
	{
		public UIInspectorSetting(DanceSetting.SettingType type)
		{
			this.MyType = type;
		}

		public DanceSetting.SettingType MyType;

		public UIPopupList PopUpUI;

		public List<RhythmAction_Mgr.DanceType> OnDanceList = new List<RhythmAction_Mgr.DanceType>();

		public bool UsePublic = true;

		public bool UseVR = true;
	}
}
