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

public class VRManualMenu : MonoBehaviour
{
	private void Start()
	{
		this.Init();
	}

	private void Init()
	{
		List<VRManualMenu.ManualData> list = new List<VRManualMenu.ManualData>();
		string text = "vrcom_config.nei";
		if (!GameUty.FileSystem.IsExistentFile(text))
		{
			NDebug.Assert("表がありません。" + text, false);
		}
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, text + "\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i))
					{
						int num = 0;
						int cellAsInteger = csvParser.GetCellAsInteger(num++, i);
						string cellAsString = csvParser.GetCellAsString(num++, i);
						string cellAsString2 = csvParser.GetCellAsString(num++, i);
						char[] array = csvParser.GetCellAsString(num++, i).ToCharArray();
						string cellAsString3 = csvParser.GetCellAsString(num++, i);
						bool flag = false;
						for (int j = 0; j < array.Length; j++)
						{
							if (array[j] == ((int)GameMain.Instance.VRDeviceTypeID).ToString().ToCharArray()[0])
							{
								flag = true;
							}
						}
						if (flag)
						{
							if (string.IsNullOrEmpty(cellAsString))
							{
								list.Add(new VRManualMenu.ManualData(cellAsInteger, -1, cellAsString2, cellAsString3, null));
							}
							else
							{
								list.Add(new VRManualMenu.ManualData(cellAsInteger, int.Parse(cellAsString), cellAsString2, cellAsString3, null));
							}
						}
					}
				}
			}
		}
		for (int k = 0; k < list.Count; k++)
		{
			VRManualMenu.ManualData data = list[k];
			if (data.BeforeID != -1)
			{
				this.m_ManualDataArray.Add(data);
			}
			else
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_PrefabButton);
				gameObject.transform.SetParent(this.m_ScrollArea, false);
				gameObject.SetActive(true);
				data.ButtonPair = gameObject.GetComponent<Button>();
				this.m_ManualDataArray.Add(data);
				gameObject.GetComponentInChildren<Text>().text = data.ButtonName;
				gameObject.GetComponent<Button>().onClick.RemoveAllListeners();
				gameObject.GetComponent<Button>().onClick.AddListener(delegate()
				{
					this.ChangeManualImage(data.ImageName);
					this.ButtonClickEvent(data);
				});
				if (k == 0)
				{
					gameObject.GetComponent<Button>().onClick.Invoke();
				}
			}
		}
	}

	private void ButtonClickEvent(VRManualMenu.ManualData data)
	{
		Debug.Log(data.ButtonName);
		VRManualMenu.ManualData beforeButton = this.GetBeforePageID(data.BeforeID);
		this.m_LeftButton.onClick.RemoveAllListeners();
		this.m_LeftButton.interactable = false;
		if (beforeButton != null)
		{
			this.m_LeftButton.interactable = true;
			this.m_LeftButton.onClick.AddListener(delegate()
			{
				this.ChangeManualImage(beforeButton.ImageName);
				this.ButtonClickEvent(beforeButton);
			});
		}
		VRManualMenu.ManualData nextButton = this.GetNextPageID(data.ID);
		this.m_RightButton.onClick.RemoveAllListeners();
		this.m_RightButton.interactable = false;
		if (nextButton != null)
		{
			this.m_RightButton.interactable = true;
			this.m_RightButton.onClick.AddListener(delegate()
			{
				this.ChangeManualImage(nextButton.ImageName);
				this.ButtonClickEvent(nextButton);
			});
		}
	}

	private VRManualMenu.ManualData GetBeforePageID(int beforePageID)
	{
		if (beforePageID == -1)
		{
			return null;
		}
		for (int i = 0; i < this.m_ManualDataArray.Count; i++)
		{
			VRManualMenu.ManualData manualData = this.m_ManualDataArray[i];
			if (manualData.ID == beforePageID)
			{
				return manualData;
			}
		}
		return null;
	}

	private VRManualMenu.ManualData GetNextPageID(int pageID)
	{
		if (pageID == -1)
		{
			return null;
		}
		for (int i = 0; i < this.m_ManualDataArray.Count; i++)
		{
			VRManualMenu.ManualData manualData = this.m_ManualDataArray[i];
			if (manualData.BeforeID == pageID)
			{
				return manualData;
			}
		}
		return null;
	}

	private void ChangeManualImage(string ImageName)
	{
		string path = this.m_uGUIAtlasPath + ImageName;
		base.StartCoroutine(this.Coroutine_Load<Sprite>(path, delegate(ResourceRequest req)
		{
			this.m_ImageManual.sprite = (req.asset as Sprite);
		}));
	}

	private IEnumerator Coroutine_Load<T>(string path, UnityAction<ResourceRequest> callback) where T : UnityEngine.Object
	{
		ResourceRequest res = Resources.LoadAsync<T>(path);
		while (!res.isDone)
		{
			yield return null;
		}
		callback(res);
		yield break;
	}

	[SerializeField]
	private Image m_ImageManual;

	[SerializeField]
	private Text m_TextPageNumber;

	[SerializeField]
	private Button m_LeftButton;

	[SerializeField]
	private Button m_RightButton;

	private string m_uGUIAtlasPath = "SceneVRCommunication\\Tablet\\Sprite\\";

	[SerializeField]
	[Tooltip("ボタンを並べるエリア")]
	private RectTransform m_ScrollArea;

	[SerializeField]
	[Tooltip("左に並ぶボタンのプレハブ")]
	private GameObject m_PrefabButton;

	private List<VRManualMenu.ManualData> m_ManualDataArray = new List<VRManualMenu.ManualData>();

	private class ManualData
	{
		public ManualData(int id, int beforeID, string imageName, string buttonName, Button button)
		{
			this.ID = id;
			this.BeforeID = beforeID;
			this.ImageName = imageName;
			this.ButtonName = buttonName;
			this.ButtonPair = button;
		}

		public int ID;

		public int BeforeID;

		public string ImageName;

		public string ButtonName;

		public Button ButtonPair;
	}
}
