using System;
using System.Collections.Generic;
using MaidStatus;
using PlayerStatus;

public class FreeModeItemEveryday : AbstractFreeModeItem
{
	private FreeModeItemEveryday(FreeModeItemEveryday.ScnearioType type, int item_id)
	{
		this.scnerioType = type;
		this.item_id_ = item_id;
		this.everyday_event_data_ = FreeModeItemEveryday.DataDic[type].everyday_event_data_dic_[this.item_id_];
		this.is_enabled_ = FreeModeItemEveryday.IsEnabledFlag(type, this.everyday_event_data_.check_flag_name);
	}

	public static List<FreeModeItemEveryday> CreateItemEverydayList(FreeModeItemEveryday.ScnearioType type, MaidStatus.Status maidStatus = null)
	{
		FreeModeItemEveryday.CreateCsvData(type);
		List<FreeModeItemEveryday> list = new List<FreeModeItemEveryday>();
		foreach (KeyValuePair<int, FreeModeItemEveryday.EverydayEventData> keyValuePair in FreeModeItemEveryday.DataDic[type].everyday_event_data_dic_)
		{
			if (maidStatus != null && !DailyMgr.IsLegacy)
			{
				if (maidStatus.subCharaData != null)
				{
					if (keyValuePair.Value.subHerionID != maidStatus.subCharaData.id)
					{
						continue;
					}
				}
				else if (!keyValuePair.Value.personalIdList.Contains(maidStatus.personal.id))
				{
					continue;
				}
			}
			if (keyValuePair.Value.gameMode != AbstractFreeModeItem.GameMode.CM3D2 || DailyMgr.IsLegacy)
			{
				if (keyValuePair.Value.gameMode != AbstractFreeModeItem.GameMode.COM3D || !DailyMgr.IsLegacy)
				{
					if (!GameMain.Instance.CharacterMgr.status.lockNTRPlay || !keyValuePair.Value.netorare)
					{
						if (type == FreeModeItemEveryday.ScnearioType.Story)
						{
							string check_flag_name = keyValuePair.Value.check_flag_name;
							if (!FreeModeItemEveryday.IsEnabledFlag(FreeModeItemEveryday.ScnearioType.Story, check_flag_name))
							{
								continue;
							}
						}
						list.Add(new FreeModeItemEveryday(type, keyValuePair.Key));
					}
				}
			}
		}
		return list;
	}

	public static void CheckAllMaidEveryDayFlag()
	{
		FreeModeItemEveryday.CheckAllMaidEveryDayFlag(FreeModeItemEveryday.ScnearioType.Story);
		FreeModeItemEveryday.CheckAllMaidEveryDayFlag(FreeModeItemEveryday.ScnearioType.Nitijyou);
	}

	public static void CheckAllMaidEveryDayFlag(FreeModeItemEveryday.ScnearioType type)
	{
		FreeModeItemEveryday.CreateCsvData(type);
		CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
		PlayerStatus.Status status = characterMgr.status;
		List<string> list = new List<string>();
		foreach (string text in FreeModeItemEveryday.DataDic[type].flag_name_set_)
		{
			if (status.GetFlag(FreeModeItemEveryday.DataDic[type].FixingFlagText + text) == 0)
			{
				list.Add(text);
			}
		}
		for (int i = 0; i < characterMgr.GetStockMaidCount(); i++)
		{
			Maid stockMaid = characterMgr.GetStockMaid(i);
			if (!(stockMaid == null))
			{
				MaidStatus.Status status2 = stockMaid.status;
				for (int j = 0; j < list.Count; j++)
				{
					if (status2.GetFlag(list[j]) != 0)
					{
						characterMgr.status.SetFlag(FreeModeItemEveryday.DataDic[type].FixingFlagText + list[j], 1);
					}
				}
			}
		}
	}

	public static void CheckEveryDayFlag(string flag_name, FreeModeItemEveryday.ScnearioType type)
	{
		FreeModeItemEveryday.CreateCsvData(type);
		if (FreeModeItemEveryday.DataDic[type].flag_name_set_.Contains(flag_name) && !FreeModeItemEveryday.IsEnabledFlag(type, flag_name))
		{
			GameMain.Instance.CharacterMgr.status.SetFlag(FreeModeItemEveryday.DataDic[type].FixingFlagText + flag_name, 1);
		}
	}

	public static bool IsEnabledFlag(FreeModeItemEveryday.ScnearioType type, string flag_name)
	{
		string flagName = FreeModeItemEveryday.DataDic[type].FixingFlagText + flag_name;
		return GameMain.Instance.CharacterMgr.status.GetFlag(flagName) != 0;
	}

	public override int item_id
	{
		get
		{
			return this.item_id_;
		}
	}

	public override string title
	{
		get
		{
			return this.everyday_event_data_.name;
		}
	}

	public override string text
	{
		get
		{
			return this.everyday_event_data_.info_text;
		}
	}

	public override string[] condition_texts
	{
		get
		{
			return this.everyday_event_data_.condition_texts;
		}
	}

	public override bool is_enabled
	{
		get
		{
			return this.is_enabled_;
		}
	}

	public override string play_file_name
	{
		get
		{
			return this.everyday_event_data_.call_file_name;
		}
	}

	public override AbstractFreeModeItem.ItemType type
	{
		get
		{
			if (this.scnerioType == FreeModeItemEveryday.ScnearioType.Story)
			{
				return AbstractFreeModeItem.ItemType.MainStory;
			}
			return AbstractFreeModeItem.ItemType.Normal;
		}
	}

	private static void CreateCsvData(FreeModeItemEveryday.ScnearioType type)
	{
		string fileName = string.Empty;
		string fixingFlagText;
		if (type == FreeModeItemEveryday.ScnearioType.Nitijyou)
		{
			fileName = "recollection_normal2.nei";
			fixingFlagText = "シーン鑑賞_一般_フラグ_";
		}
		else
		{
			if (type != FreeModeItemEveryday.ScnearioType.Story)
			{
				return;
			}
			fileName = "recollection_story.nei";
			fixingFlagText = "シーン鑑賞_メイン_フラグ_";
		}
		if (!FreeModeItemEveryday.DataDic.ContainsKey(type))
		{
			FreeModeItemEveryday.DataDic.Add(type, new FreeModeItemEveryday.ScnerioData(fixingFlagText));
		}
		if (FreeModeItemEveryday.DataDic[type].everyday_event_data_dic_ != null)
		{
			return;
		}
		FreeModeItemEveryday.DataDic[type].everyday_event_data_dic_ = new SortedDictionary<int, FreeModeItemEveryday.EverydayEventData>();
		FreeModeItemEveryday.CreateCsvData(type, fileName, AbstractFreeModeItem.GameMode.COM3D);
		if (GameUty.IsEnabledCompatibilityMode && type == FreeModeItemEveryday.ScnearioType.Nitijyou)
		{
			FreeModeItemEveryday.CreateLeagacyDisableIdList();
			FreeModeItemEveryday.CreateCsvData(type, fileName, AbstractFreeModeItem.GameMode.CM3D2);
		}
	}

	private static void CreateCsvData(FreeModeItemEveryday.ScnearioType type, string fileName, AbstractFreeModeItem.GameMode gameMode)
	{
		HashSet<int> hashSet;
		AFileBase afileBase;
		if (gameMode == AbstractFreeModeItem.GameMode.CM3D2)
		{
			hashSet = AbstractFreeModeItem.GetEnabledOldIdList();
			NDebug.Assert(GameUty.FileSystemOld.IsExistentFile(fileName), fileName + "\nopen failed.");
			afileBase = GameUty.FileSystemOld.FileOpen(fileName);
		}
		else
		{
			if (gameMode != AbstractFreeModeItem.GameMode.COM3D)
			{
				return;
			}
			hashSet = AbstractFreeModeItem.GetEnabledIdList();
			NDebug.Assert(GameUty.FileSystem.IsExistentFile(fileName), fileName + "\nopen failed.");
			afileBase = GameUty.FileSystem.FileOpen(fileName);
		}
		if (hashSet == null || hashSet.Count <= 0)
		{
			return;
		}
		using (afileBase)
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, fileName + "\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i))
					{
						int cellAsInteger = csvParser.GetCellAsInteger(0, i);
						if (hashSet.Contains(cellAsInteger))
						{
							FreeModeItemEveryday.EverydayEventData everydayEventData = new FreeModeItemEveryday.EverydayEventData();
							everydayEventData.id = cellAsInteger;
							int num = 1;
							if (gameMode != AbstractFreeModeItem.GameMode.CM3D2 || type != FreeModeItemEveryday.ScnearioType.Nitijyou || !FreeModeItemEveryday.disableIdList.Contains(everydayEventData.id))
							{
								everydayEventData.gameMode = gameMode;
								everydayEventData.name = csvParser.GetCellAsString(num++, i);
								everydayEventData.call_file_name = csvParser.GetCellAsString(num++, i);
								everydayEventData.check_flag_name = csvParser.GetCellAsString(num++, i);
								if (gameMode == AbstractFreeModeItem.GameMode.COM3D)
								{
									everydayEventData.netorare = (csvParser.GetCellAsString(num++, i) == "○");
								}
								everydayEventData.info_text = csvParser.GetCellAsString(num++, i);
								List<string> list = new List<string>();
								for (int j = 0; j < 9; j++)
								{
									if (csvParser.IsCellToExistData(num, i))
									{
										list.Add(csvParser.GetCellAsString(num, i));
									}
									num++;
								}
								everydayEventData.subHerionID = csvParser.GetCellAsInteger(num++, i);
								while (csvParser.IsCellToExistData(num, 0))
								{
									if (csvParser.GetCellAsString(num, i) == "○")
									{
										string cellAsString = csvParser.GetCellAsString(num, 0);
										Personal.Data data = Personal.GetData(cellAsString);
										everydayEventData.personalIdList.Add(data.id);
									}
									num++;
								}
								everydayEventData.condition_texts = list.ToArray();
								if (!FreeModeItemEveryday.DataDic[type].everyday_event_data_dic_.ContainsKey(everydayEventData.id))
								{
									if (!FreeModeItemEveryday.DataDic[type].flag_name_set_.Contains(everydayEventData.check_flag_name))
									{
										FreeModeItemEveryday.DataDic[type].flag_name_set_.Add(everydayEventData.check_flag_name);
									}
									FreeModeItemEveryday.DataDic[type].everyday_event_data_dic_.Add(everydayEventData.id, everydayEventData);
								}
							}
						}
					}
				}
			}
		}
	}

	private static void CreateLeagacyDisableIdList()
	{
		AFileBase afileBase = GameUty.FileSystem.FileOpen("recollection_legacy_disable.nei");
		using (afileBase)
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool flag = csvParser.Open(afileBase);
				FreeModeItemEveryday.disableIdList = new List<int>();
				int max_cell_y = csvParser.max_cell_y;
				for (int i = 1; i < max_cell_y; i++)
				{
					FreeModeItemEveryday.disableIdList.Add(csvParser.GetCellAsInteger(0, i));
				}
			}
		}
	}

	private static Dictionary<FreeModeItemEveryday.ScnearioType, FreeModeItemEveryday.ScnerioData> DataDic = new Dictionary<FreeModeItemEveryday.ScnearioType, FreeModeItemEveryday.ScnerioData>();

	private static List<int> disableIdList = new List<int>();

	private bool is_enabled_;

	private int item_id_;

	private FreeModeItemEveryday.EverydayEventData everyday_event_data_;

	private FreeModeItemEveryday.ScnearioType scnerioType;

	public enum ScnearioType
	{
		Story,
		Nitijyou
	}

	private class ScnerioData
	{
		public ScnerioData(string FixingFlagText)
		{
			this.FixingFlagText = FixingFlagText;
		}

		public SortedDictionary<int, FreeModeItemEveryday.EverydayEventData> everyday_event_data_dic_;

		public HashSet<string> flag_name_set_ = new HashSet<string>();

		public string FixingFlagText = "シーン鑑賞_一般_フラグ_";
	}

	private class EverydayEventData
	{
		public AbstractFreeModeItem.GameMode gameMode;

		public int id;

		public string name;

		public string call_file_name;

		public string check_flag_name;

		public string info_text;

		public bool netorare;

		public int subHerionID;

		public string[] condition_texts;

		public List<int> personalIdList = new List<int>();
	}
}
