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

namespace Kasizuki
{
	public static class RoomData
	{
		public static int Count
		{
			get
			{
				RoomData.CreateData();
				return RoomData.commonIdManager.idMap.Count;
			}
		}

		public static bool Contains(string name)
		{
			return RoomData.commonIdManager.nameMap.ContainsKey(name);
		}

		public static bool Contains(int id)
		{
			return RoomData.commonIdManager.idMap.ContainsKey(id);
		}

		public static int uniqueNameToId(string uniqueName)
		{
			RoomData.CreateData();
			NDebug.Assert(RoomData.commonIdManager.nameMap.ContainsKey(uniqueName), "傅き.部屋データ\nユニーク名[" + uniqueName + "]をIDに変換できませんでした");
			return RoomData.commonIdManager.nameMap[uniqueName];
		}

		public static string IdToUniqueName(int id)
		{
			RoomData.CreateData();
			NDebug.Assert(RoomData.commonIdManager.idMap.ContainsKey(id), "傅き.部屋データ\nID[" + id + "]をユニーク名に変換できませんでした");
			return RoomData.commonIdManager.idMap[id].Key;
		}

		public static RoomData.Data GetData(int id)
		{
			RoomData.CreateData();
			NDebug.Assert(RoomData.basicDatas.ContainsKey(id), "傅き.部屋データ\nID[" + id + "]のデータは存在しません");
			return RoomData.basicDatas[id];
		}

		public static RoomData.Data GetData(string uniqueName)
		{
			return RoomData.GetData(RoomData.uniqueNameToId(uniqueName));
		}

		public static bool IsEnabled(string uniqueName)
		{
			RoomData.CreateData();
			return RoomData.commonIdManager.enabledIdList.Contains(RoomData.uniqueNameToId(uniqueName));
		}

		public static bool IsEnabled(int id)
		{
			RoomData.CreateData();
			return RoomData.commonIdManager.enabledIdList.Contains(id);
		}

		public static List<RoomData.Data> GetAllDatas(bool onlyEnabled)
		{
			RoomData.CreateData();
			List<RoomData.Data> list = new List<RoomData.Data>();
			foreach (KeyValuePair<int, KeyValuePair<string, string>> keyValuePair in RoomData.commonIdManager.idMap)
			{
				if (!onlyEnabled || RoomData.commonIdManager.enabledIdList.Contains(keyValuePair.Key))
				{
					list.Add(RoomData.basicDatas[keyValuePair.Key]);
				}
			}
			return list;
		}

		public static List<RoomData.Data> GetDatas(Func<RoomData.Data, bool> customCheckFunction)
		{
			RoomData.CreateData();
			List<RoomData.Data> list = new List<RoomData.Data>();
			foreach (KeyValuePair<int, KeyValuePair<string, string>> keyValuePair in RoomData.commonIdManager.idMap)
			{
				RoomData.Data data = RoomData.basicDatas[keyValuePair.Key];
				if (customCheckFunction(data))
				{
					list.Add(data);
				}
			}
			return list;
		}

		public static void CreateData()
		{
			if (RoomData.commonIdManager != null)
			{
				return;
			}
			RoomData.commonIdManager = new CsvCommonIdManager("kasizuki_room", "傅き.部屋データ", CsvCommonIdManager.Type.IdAndUniqueName, null);
			RoomData.basicDatas = new Dictionary<int, RoomData.Data>();
			string[] array = new string[]
			{
				"list"
			};
			KeyValuePair<AFileBase, CsvParser>[] array2 = new KeyValuePair<AFileBase, CsvParser>[array.Length];
			for (int i = 0; i < array2.Length; i++)
			{
				string text = "kasizuki_room_" + array[i] + ".nei";
				AFileBase afileBase = GameUty.FileSystem.FileOpen(text);
				CsvParser csvParser = new CsvParser();
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, text + "\nopen failed.");
				array2[i] = new KeyValuePair<AFileBase, CsvParser>(afileBase, csvParser);
			}
			foreach (KeyValuePair<int, KeyValuePair<string, string>> keyValuePair in RoomData.commonIdManager.idMap)
			{
				int key = keyValuePair.Key;
				RoomData.Data value = new RoomData.Data(key, array2[0].Value);
				RoomData.basicDatas.Add(key, value);
			}
			foreach (KeyValuePair<AFileBase, CsvParser> keyValuePair2 in array2)
			{
				keyValuePair2.Value.Dispose();
				keyValuePair2.Key.Dispose();
			}
		}

		private const string csvTopCommonName = "kasizuki_room";

		private const string typeNameForErrorLog = "傅き.部屋データ";

		private static CsvCommonIdManager commonIdManager;

		private static Dictionary<int, RoomData.Data> basicDatas;

		public class Data
		{
			public Data(int uniqueID, CsvParser csv)
			{
				for (int i = 0; i < csv.max_cell_y; i++)
				{
					if (csv.IsCellToExistData(0, i) && csv.GetCellAsInteger(0, i) == uniqueID)
					{
						int num = 1;
						this.ID = uniqueID;
						this.uniqueName = csv.GetCellAsString(num++, i);
						this.isExistUpwardRoom = csv.IsCellToExistData(num, i);
						if (this.isExistUpwardRoom)
						{
							this.upwardRoomID = csv.GetCellAsInteger(num, i);
						}
						num++;
						this.facilityTypeID = csv.GetCellAsInteger(num++, i);
						this.facilityDefaultName = csv.GetCellAsString(num++, i);
						this.drawName = csv.GetCellAsString(num++, i);
						this.thumbnailName = csv.GetCellAsString(num++, i);
						this.explanatoryText = csv.GetCellAsString(num++, i);
						bool flag = FacilityDataTable.GetFacilityStatus(this.facilityTypeID, true) != null;
						bool flag2 = FacilityDataTable.GetFacilityTypeID(this.facilityDefaultName) != -1;
						if (!flag && !flag2)
						{
							Debug.LogWarning("表データ内の施設ID、施設名がどちらも正しくない");
						}
						else if (flag && !flag2)
						{
							Debug.LogWarning("表データ内の施設名が正しくない");
							this.facilityDefaultName = FacilityDataTable.GetFacilityStatus(this.facilityTypeID, true).name;
						}
						else if (!flag && flag2)
						{
							Debug.LogWarning("表データ内の施設IDが正しくない");
							this.facilityTypeID = FacilityDataTable.GetFacilityTypeID(this.facilityDefaultName);
						}
						string cellAsString = csv.GetCellAsString(num++, i);
						if (!string.IsNullOrEmpty(cellAsString))
						{
							try
							{
								string[] array = cellAsString.Split(new char[]
								{
									','
								});
								for (int j = 0; j < array.Length; j++)
								{
									array[j] = array[j].Trim();
								}
								this.strConditionsArraySystemFlag = array;
							}
							catch (Exception ex)
							{
								NDebug.Assert(ex.ToString(), false);
								Debug.LogError(ex);
							}
						}
						Dictionary<ManDataType, bool> dictionary = new Dictionary<ManDataType, bool>();
						int num2 = Enum.GetNames(typeof(ManDataType)).Length;
						for (int k = 0; k < num2; k++)
						{
							string cellAsString2 = csv.GetCellAsString(num, 0);
							ManDataType manType = ManData.GetData(cellAsString2).manType;
							dictionary.Add(manType, csv.GetCellAsString(num++, i) == "○");
						}
						this.enableManTypeDic = new ReadOnlyDictionary<ManDataType, bool>(dictionary);
						FacilityDataTable.FacilityDefaultData facilityDefaultData = FacilityDataTable.GetFacilityDefaultData(this.facilityTypeID, true);
						this.isEnableNTR = facilityDefaultData.isEnableNTR;
						this.isOnlyNTR = facilityDefaultData.isOnlyNTR;
					}
				}
			}

			public RoomData.Data GetUpwardRoomData()
			{
				if (this.isExistUpwardRoom)
				{
					if (RoomData.IsEnabled(this.upwardRoomID))
					{
						return RoomData.GetData(this.upwardRoomID);
					}
					string message = string.Concat(new object[]
					{
						"傅き.部屋データ\n部屋[",
						this.uniqueName,
						"]の上位ID[",
						this.upwardRoomID,
						"]のデータは存在しません"
					});
					NDebug.Assert(message, false);
				}
				return null;
			}

			public RoomData.Data GetSubordinateRoomData()
			{
				List<RoomData.Data> datas = RoomData.GetDatas((RoomData.Data data) => data.upwardRoomID == this.ID);
				RoomData.Data result = null;
				if (datas != null && datas.Count == 1)
				{
					result = datas[0];
				}
				return result;
			}

			public List<PlayData.Data> GetPlayDatas()
			{
				List<PlayData.Data> datas = PlayData.GetDatas((PlayData.Data playData) => playData.roomID == this.ID);
				RoomData.Data subordinateRoomData = this.GetSubordinateRoomData();
				if (subordinateRoomData != null)
				{
					foreach (PlayData.Data item in subordinateRoomData.GetPlayDatas())
					{
						if (!datas.Contains(item))
						{
							datas.Add(item);
						}
					}
				}
				return datas;
			}

			public bool IsExistFacility()
			{
				if (!FacilityDataTable.IsExistFacilityData(this.facilityTypeID, true))
				{
					string message = string.Concat(new object[]
					{
						"傅き.部屋データ\n部屋データ[",
						this.uniqueName,
						"]に対応する施設のID[",
						this.facilityTypeID,
						"は存在しない施設データです"
					});
					Debug.LogWarning(message);
					return false;
				}
				FacilityManager facilityMgr = GameMain.Instance.FacilityMgr;
				if (facilityMgr == null)
				{
					string message2 = "施設マネージャクラスが見つかりませんでした";
					Debug.LogWarning(message2);
					return false;
				}
				return facilityMgr.IsExistTypeFacility(this.facilityTypeID);
			}

			public readonly int ID;

			public readonly string uniqueName;

			public readonly int upwardRoomID;

			public readonly int facilityTypeID;

			public readonly string facilityDefaultName;

			public readonly string drawName;

			public readonly string thumbnailName;

			public readonly string explanatoryText;

			public readonly string[] strConditionsArraySystemFlag;

			public readonly ReadOnlyDictionary<ManDataType, bool> enableManTypeDic;

			public readonly bool isExistUpwardRoom;

			public readonly bool isEnableNTR;

			public readonly bool isOnlyNTR;
		}
	}
}
