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

public class uGUITutorialPanel : MonoBehaviour
{
	public int index { get; private set; }

	public static bool IsExistTutorial(string sceneName)
	{
		if (uGUITutorialPanel.m_SceneNameArray == null || uGUITutorialPanel.m_SceneNameArray.Count <= 0)
		{
			uGUITutorialPanel.m_SceneNameArray = new List<string>();
			string text = "tutorial_list.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.");
					int i = 0;
					List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
					for (i = 1; i < csvParser.max_cell_y; i++)
					{
						if (csvParser.IsCellToExistData(0, i))
						{
							string cellAsString = csvParser.GetCellAsString(0, i);
							uGUITutorialPanel.m_SceneNameArray.Add(cellAsString);
						}
					}
				}
			}
		}
		return uGUITutorialPanel.m_SceneNameArray.Contains(sceneName);
	}

	public static void OpenTutorial(string sceneName, Action closeCallback = null, bool absoluteCall = false)
	{
		if (string.IsNullOrEmpty(sceneName))
		{
			NDebug.Assert("チュートリアルの種類に空文字が指定されました", false);
		}
		if (uGUITutorialPanel.IsOpened())
		{
			NDebug.Warning("既にチュートリアル用パネルが開いています。");
			return;
		}
		if (!uGUITutorialPanel.IsExistTutorial(sceneName))
		{
			NDebug.Warning(string.Format("チュートリアル名「{0}」は存在しません。\n終了処理を実行します。", sceneName));
			if (closeCallback != null)
			{
				closeCallback();
			}
			return;
		}
		GameMain.Instance.SysShortcut.helpButtonInteractable = false;
		if (closeCallback != null)
		{
			closeCallback();
		}
	}

	public static void CloseTutorial(bool immediate = false, bool enableCallback = true)
	{
		if (!uGUITutorialPanel.IsOpened())
		{
			return;
		}
		uGUITutorialPanel.m_Instance.OnClose(immediate, enableCallback);
	}

	public static bool IsOpened()
	{
		return uGUITutorialPanel.m_Instance != null;
	}

	private void Initialize(string type, Action callback)
	{
		uGUITutorialPanel.<Initialize>c__AnonStorey1 <Initialize>c__AnonStorey = new uGUITutorialPanel.<Initialize>c__AnonStorey1();
		<Initialize>c__AnonStorey.$this = this;
		Transform uirootTrans = this.GetUIRootTrans();
		if (uirootTrans != null)
		{
			base.transform.SetParent(uirootTrans, false);
		}
		else
		{
			NDebug.Assert("SystemUI Rootが見つかりませんでした", false);
		}
		this.m_CacheSpriteArray = new Dictionary<string, Sprite>();
		this.m_CallbackClose = callback;
		this.m_ButtonOK.interactable = true;
		this.m_ButtonOK.onClick.AddListener(delegate()
		{
			<Initialize>c__AnonStorey.$this.OnClose(false, true);
		});
		this.ReadCSV(type);
		if (this.IsMultiMode())
		{
			this.m_ParentMultiMode.SetActive(true);
			this.m_ParentSingleMode.SetActive(false);
			int itemCount = this.m_strFileNameArray.Length;
			this.m_ListViewerButtons.Show<Toggle>(itemCount, delegate(int i, Toggle toggle)
			{
				KeyValuePair<string, string> keyValuePair = <Initialize>c__AnonStorey.$this.m_strFileNameArray[i];
				if (<Initialize>c__AnonStorey.$this.index == i)
				{
					toggle.isOn = true;
				}
				Text componentInChildren = toggle.GetComponentInChildren<Text>();
				if (!string.IsNullOrEmpty(keyValuePair.Value))
				{
					componentInChildren.text = keyValuePair.Value;
				}
				else
				{
					componentInChildren.text = string.Format("（！項目名無し）.{1}", i);
				}
				toggle.onValueChanged.AddListener(delegate(bool value)
				{
					if (!value)
					{
						return;
					}
					<Initialize>c__AnonStorey.OpenPage(i);
				});
			});
		}
		else
		{
			this.m_ParentMultiMode.SetActive(false);
			this.m_ParentSingleMode.SetActive(true);
		}
		this.OpenPage(0);
		<Initialize>c__AnonStorey.fade = base.GetComponent<uGUICanvasFade>();
		<Initialize>c__AnonStorey.fade.alpha = 0f;
		<Initialize>c__AnonStorey.fade.interactable = false;
		<Initialize>c__AnonStorey.fade.FadeIn(0.5f, delegate
		{
			<Initialize>c__AnonStorey.fade.interactable = true;
		});
	}

	private void OnClose(bool immediate = false, bool enableCallback = true)
	{
		if (immediate)
		{
			uGUITutorialPanel.m_Instance = null;
			UnityEngine.Object.Destroy(base.gameObject);
			if (enableCallback && this.m_CallbackClose != null)
			{
				this.m_CallbackClose();
			}
		}
		else
		{
			uGUICanvasFade component = base.GetComponent<uGUICanvasFade>();
			component.interactable = false;
			component.FadeOut(0.5f, delegate
			{
				uGUITutorialPanel.m_Instance = null;
				UnityEngine.Object.Destroy(this.gameObject);
				if (enableCallback && this.m_CallbackClose != null)
				{
					this.m_CallbackClose();
				}
			});
		}
	}

	private bool OpenPage(int pageNumber)
	{
		if (!this.IsChangeIndex(pageNumber))
		{
			Debug.LogFormat("{0} ページを開けなかった", new object[]
			{
				pageNumber
			});
			return false;
		}
		this.SetImage(this.m_strFileNameArray[pageNumber].Key);
		this.m_IsOpenedPageArray[pageNumber] = true;
		return true;
	}

	private bool TryChangeIndex(int changeIndex)
	{
		if (!this.IsChangeIndex(changeIndex))
		{
			return false;
		}
		this.index = changeIndex;
		return true;
	}

	private bool IsChangeIndex(int changeIndex)
	{
		int num = this.m_strFileNameArray.Length;
		return changeIndex >= 0 && changeIndex < num;
	}

	private void SetImage(string fileName)
	{
		Sprite sprite;
		if (this.m_CacheSpriteArray.ContainsKey(fileName))
		{
			sprite = this.m_CacheSpriteArray[fileName];
		}
		else
		{
			string text = this.strFileNamePath + fileName;
			sprite = this.CreateSprite(fileName + ".tex");
			if (sprite != null)
			{
				this.m_CacheSpriteArray.Add(fileName, sprite);
			}
			else
			{
				NDebug.Warning(string.Format("[uGUITutorialPanel]チュートリアル画像「{0}」が見つかりませんでした", fileName));
			}
		}
		if (this.IsMultiMode())
		{
			this.m_ImageMultiTutorial.sprite = sprite;
		}
		else
		{
			this.m_ImageSingleTutorial.sprite = sprite;
		}
	}

	private bool IsMultiMode()
	{
		return this.m_strFileNameArray.Length > 1;
	}

	private static bool IsTutorialPassed(string tutorialName)
	{
		string flagName = "__チュートリアルフラグ_" + tutorialName;
		Status status = GameMain.Instance.CharacterMgr.status;
		return status.GetFlag(flagName) > 0;
	}

	private static void SetTutorialFlag(string tutorialName)
	{
		string flagName = "__チュートリアルフラグ_" + tutorialName;
		Status status = GameMain.Instance.CharacterMgr.status;
		status.SetFlag(flagName, 1);
	}

	private Transform GetUIRootTrans()
	{
		Transform result = null;
		GameObject gameObject = GameObject.Find("SystemUI Root");
		if (gameObject)
		{
			result = gameObject.transform;
		}
		return result;
	}

	private void ReadCSV(string type)
	{
		string text = "tutorial_list.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.");
				int i = 0;
				List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
				for (i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i))
					{
						string cellAsString = csvParser.GetCellAsString(0, i);
						if (cellAsString == type)
						{
							break;
						}
					}
				}
				for (int j = 2; j < csvParser.max_cell_x; j += 2)
				{
					if (csvParser.IsCellToExistData(j, i))
					{
						string cellAsString2 = csvParser.GetCellAsString(j, i);
						string cellAsString3 = csvParser.GetCellAsString(j + 1, i);
						if (!string.IsNullOrEmpty(cellAsString2))
						{
							list.Add(new KeyValuePair<string, string>(cellAsString2, cellAsString3));
						}
					}
				}
				this.m_strFileNameArray = list.ToArray();
				this.m_IsOpenedPageArray = new bool[this.m_strFileNameArray.Length];
			}
		}
		if (this.m_strFileNameArray.Length <= 0)
		{
			NDebug.Warning(string.Format("チュートリアル「{0}」の表データが空でした", type));
		}
	}

	private void OnApplicationQuit()
	{
		this.m_IsQuittingApplication = true;
	}

	private void OnDestroy()
	{
		if (this.m_IsQuittingApplication)
		{
			return;
		}
		if (uGUITutorialPanel.m_Instance != null && uGUITutorialPanel.m_Instance == this)
		{
			uGUITutorialPanel.m_Instance = null;
		}
		this.m_ImageMultiTutorial.sprite = null;
		this.m_ImageSingleTutorial.sprite = null;
		if (this.m_CacheSpriteArray != null)
		{
			int count = this.m_CacheSpriteArray.Count;
			List<Sprite> list = new List<Sprite>(this.m_CacheSpriteArray.Values);
			this.m_CacheSpriteArray.Clear();
			for (int i = 0; i < count; i++)
			{
				UnityEngine.Object.DestroyImmediate(list[i].texture);
				UnityEngine.Object.DestroyImmediate(list[i]);
			}
			list.Clear();
		}
		this.m_CallbackClose = null;
	}

	private Sprite CreateSprite(string fileName)
	{
		Sprite sprite = null;
		if (GameUty.FileSystem.IsExistentFile(fileName))
		{
			Texture2D texture2D = ImportCM.CreateTexture(fileName);
			sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
			sprite.name = fileName;
		}
		return sprite;
	}

	[SerializeField]
	private Button m_ButtonOK;

	[Header("ページ数単体時のオブジェクト")]
	[SerializeField]
	private GameObject m_ParentSingleMode;

	[SerializeField]
	private Image m_ImageSingleTutorial;

	[Header("ページ数複数時のオブジェクト")]
	[SerializeField]
	private GameObject m_ParentMultiMode;

	[SerializeField]
	private Image m_ImageMultiTutorial;

	[SerializeField]
	private uGUIListViewer m_ListViewerButtons;

	private KeyValuePair<string, string>[] m_strFileNameArray;

	private bool[] m_IsOpenedPageArray;

	private readonly string strFileNamePath = "SceneTutorial/Sprites/";

	private Dictionary<string, Sprite> m_CacheSpriteArray;

	private Action m_CallbackClose;

	private static List<string> m_SceneNameArray;

	private static uGUITutorialPanel m_Instance;

	private bool m_IsQuittingApplication;
}
