using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using wf;

public class ScenarioSelectMgr
{
	public ScenarioData[] AddedScenario
	{
		get
		{
			if (this.m_AddedScenerio == null)
			{
				this.IsEventAdded();
			}
			return this.m_AddedScenerio;
		}
	}

	public bool DeserializeFiled { get; private set; }

	public float nSaveVer { get; private set; }

	private void ReadConvertData(CsvParser csv, int cy)
	{
		this.m_PersonalConvertData.Add(csv.GetCellAsString(0, cy), csv.GetCellAsString(1, cy));
	}

	public void InitScenarioData()
	{
		if (this.m_AllScenarioData.Count > 0)
		{
			return;
		}
		HashSet<int> enabled_id_list = new HashSet<int>();
		Action<string> action = delegate(string file_name)
		{
			file_name += ".nei";
			if (!GameUty.FileSystem.IsExistentFile(file_name))
			{
				return;
			}
			using (AFileBase afileBase2 = GameUty.FileSystem.FileOpen(file_name))
			{
				using (CsvParser csvParser2 = new CsvParser())
				{
					bool condition2 = csvParser2.Open(afileBase2);
					NDebug.Assert(condition2, file_name + "\nopen failed.");
					for (int k = 1; k < csvParser2.max_cell_y; k++)
					{
						if (csvParser2.IsCellToExistData(0, k))
						{
							int cellAsInteger2 = csvParser2.GetCellAsInteger(0, k);
							if (!enabled_id_list.Contains(cellAsInteger2))
							{
								enabled_id_list.Add(cellAsInteger2);
							}
						}
					}
				}
			}
		};
		action("selectable_scenario_id");
		for (int i = 0; i < GameUty.PathList.Count; i++)
		{
			action("selectable_scenario_id_" + GameUty.PathList[i]);
		}
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("select_scenario_data.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "select_scenario_data.nei]");
				for (int j = 1; j < csvParser.max_cell_y; j++)
				{
					if (csvParser.IsCellToExistData(0, j))
					{
						int cellAsInteger = csvParser.GetCellAsInteger(0, j);
						if (enabled_id_list.Contains(cellAsInteger))
						{
							if (!this.m_AllScenarioData.ContainsKey(cellAsInteger))
							{
								this.m_AllScenarioData.Add(cellAsInteger, new ScenarioData(csvParser, j));
							}
						}
					}
				}
			}
		}
		KasaiUtility.CsvReadY("personal_convert_data.nei", new Action<CsvParser, int>(this.ReadConvertData), 2, null);
	}

	public bool IsEventAdded()
	{
		List<ScenarioData> list = (from e in this.m_AllScenarioData.Values
		where e.ImportantPlayable
		select e).ToList<ScenarioData>();
		ScenarioData[] array = (from e in list
		where !this.m_ImportantScenarioList.Contains(e)
		select e).ToArray<ScenarioData>();
		this.m_AddedScenerio = array;
		this.m_ImportantScenarioList = list;
		return array.Length > 0;
	}

	public ScenarioData GetScenarioData(string title)
	{
		foreach (ScenarioData scenarioData in this.m_AllScenarioData.Values)
		{
			if (scenarioData.NotLineTitle == title)
			{
				return scenarioData;
			}
		}
		return null;
	}

	public ScenarioData GetScenarioData(int id)
	{
		return (!this.m_AllScenarioData.ContainsKey(id)) ? null : this.m_AllScenarioData[id];
	}

	public ScenarioData[] GetAllScenarioData()
	{
		return this.m_AllScenarioData.Values.ToArray<ScenarioData>();
	}

	public bool ExistConvertData(string check_data)
	{
		return this.m_PersonalConvertData.ContainsValue(check_data);
	}

	public string GetConvertPersonal(Maid maid)
	{
		if (this.m_PersonalConvertData.ContainsKey(maid.status.personal.uniqueName))
		{
			return this.m_PersonalConvertData[maid.status.personal.uniqueName];
		}
		return string.Empty;
	}

	public void Serialize(BinaryWriter writer)
	{
		writer.Write("COM3D2_SCENARIO_SELECT");
		writer.Write(1.3f);
		writer.Write("CM3D2_SCENARIO_ID_START");
		foreach (ScenarioData scenarioData in this.m_AllScenarioData.Values)
		{
			writer.Write(scenarioData.ID.ToString());
		}
		writer.Write("CM3D2_SCENARIO_ID_END");
		writer.Write("CM3D2_SELECTABLE_START");
		foreach (ScenarioData scenarioData2 in this.m_ImportantScenarioList)
		{
			writer.Write(scenarioData2.ID.ToString());
		}
		writer.Write("CM3D2_SELECTABLE_END");
		foreach (ScenarioData scenarioData3 in this.m_AllScenarioData.Values)
		{
			scenarioData3.Serialize(writer);
		}
	}

	public void Deserialize(BinaryReader reader)
	{
		string a = reader.ReadString();
		NDebug.Assert(a == "CM3D2_SCENARIO_SELECT" || a == "COM3D2_SCENARIO_SELECT", "セーブデータファイルのヘッダーが不正です。");
		if (a == "COM3D2_SCENARIO_SELECT")
		{
			this.nSaveVer = reader.ReadSingle();
		}
		else
		{
			this.nSaveVer = -1f;
		}
		this.DeserializeFiled = false;
		int count = this.m_AllScenarioData.Count;
		foreach (ScenarioData scenarioData in this.m_AllScenarioData.Values)
		{
			scenarioData.Recet();
		}
		List<int> list = new List<int>();
		string a2 = reader.ReadString();
		if (a2 == "CM3D2_SCENARIO_ID_START")
		{
			string text = reader.ReadString();
			while (text != "CM3D2_SCENARIO_ID_END")
			{
				list.Add(int.Parse(text));
				text = reader.ReadString();
			}
			List<int> list2 = new List<int>();
			string a3 = reader.ReadString();
			if (a3 == "CM3D2_SELECTABLE_START")
			{
				string text2 = reader.ReadString();
				while (text2 != "CM3D2_SELECTABLE_END")
				{
					list2.Add(int.Parse(text2));
					text2 = reader.ReadString();
				}
			}
			else
			{
				this.DeserializeFiled = true;
				Debug.Log("ScenarioSelectMgr.cs:重要シナリオデータIDを書き込む前のバージョンのデータです(2017/10/24より前)");
			}
			foreach (int key in list)
			{
				if (this.m_AllScenarioData.ContainsKey(key))
				{
					this.m_AllScenarioData[key].Deserialize(reader);
				}
				else
				{
					ScenarioData scenarioData2 = new ScenarioData();
					scenarioData2.Deserialize(reader);
				}
			}
			this.m_ImportantScenarioList.Clear();
			foreach (int key2 in list2)
			{
				if (this.m_AllScenarioData.ContainsKey(key2))
				{
					this.m_ImportantScenarioList.Add(this.m_AllScenarioData[key2]);
				}
			}
		}
		else
		{
			Debug.Log("ScenarioSelectMgr.cs:有効IDを書き込む前のバージョンのデータです(2017/10/23より前)");
			this.DeserializeFiled = true;
			foreach (ScenarioData scenarioData3 in this.m_AllScenarioData.Values)
			{
				scenarioData3.Deserialize(reader);
			}
			this.m_ImportantScenarioList = (from e in this.m_AllScenarioData.Values
			where e.IsPlayable && e.IsImportant
			select e).ToList<ScenarioData>();
		}
	}

	public void AddedDisplay(string label)
	{
		GameObject gameObject = Utility.CreatePrefab(null, "SceneScenarioSelect/Prefab/AdditionalScenario", false);
		gameObject.GetComponent<AdditionalScenario>().Init(label);
	}

	public bool ExistScenario(int id)
	{
		return this.m_AllScenarioData.ContainsKey(id);
	}

	public const int CheckVersion = 206;

	private const float m_SaveVer = 1.3f;

	private Dictionary<int, ScenarioData> m_AllScenarioData = new Dictionary<int, ScenarioData>();

	private List<ScenarioData> m_ImportantScenarioList = new List<ScenarioData>();

	private ScenarioData[] m_AddedScenerio;

	private Dictionary<string, string> m_PersonalConvertData = new Dictionary<string, string>();
}
