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

namespace wf
{
	public class CsvCommonIdManager
	{
		public CsvCommonIdManager(string csvTopCommonName, string typeName, CsvCommonIdManager.Type type, Func<int, bool> customCheckFunction = null)
		{
			this.idMap = new SortedDictionary<int, KeyValuePair<string, string>>();
			this.nameMap = new Dictionary<string, int>();
			this.type = type;
			AFileSystemBase fileSystem = GameUty.FileSystem;
			List<string> pathList = GameUty.PathList;
			string fileName = csvTopCommonName + "_enabled_list";
			string str = csvTopCommonName + "_list";
			this.enabledIdList = new HashSet<int>();
			CsvCommonIdManager.ReadEnabledIdList(fileSystem, pathList, fileName, ref this.enabledIdList);
			using (AFileBase afileBase = fileSystem.FileOpen(str + ".nei"))
			{
				using (CsvParser csvParser = new CsvParser())
				{
					bool condition = csvParser.Open(afileBase);
					NDebug.Assert(condition, str + ".nei\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);
							if (type == CsvCommonIdManager.Type.IdAndUniqueName)
							{
								string cellAsString = csvParser.GetCellAsString(num++, i);
								string cellAsString2 = csvParser.GetCellAsString(num++, i);
								if (this.idMap.ContainsKey(cellAsInteger) || this.nameMap.ContainsKey(cellAsString) || string.IsNullOrEmpty(cellAsString))
								{
									if (this.idMap.ContainsKey(cellAsInteger))
									{
										Debug.LogError(string.Concat(new object[]
										{
											typeName,
											"のID[",
											cellAsInteger,
											"]が重複しています"
										}));
									}
									else if (this.nameMap.ContainsKey(cellAsString))
									{
										Debug.LogError(typeName + "の名前[" + cellAsString + "]が重複しています");
									}
									else
									{
										Debug.LogError(string.Concat(new object[]
										{
											typeName,
											"の名前が空です\n",
											i + 1,
											"行目"
										}));
									}
								}
								else if (customCheckFunction == null || customCheckFunction(cellAsInteger))
								{
									this.idMap.Add(cellAsInteger, new KeyValuePair<string, string>(cellAsString, cellAsString2));
									this.nameMap.Add(cellAsString, cellAsInteger);
								}
							}
							else
							{
								string cellAsString3 = csvParser.GetCellAsString(num++, i);
								if (this.idMap.ContainsKey(cellAsInteger))
								{
									if (this.idMap.ContainsKey(cellAsInteger))
									{
										Debug.LogError(string.Concat(new object[]
										{
											typeName,
											"のID[",
											cellAsInteger,
											"]が重複しています"
										}));
									}
								}
								else if (customCheckFunction == null || customCheckFunction(cellAsInteger))
								{
									this.idMap.Add(cellAsInteger, new KeyValuePair<string, string>(string.Empty, cellAsString3));
								}
							}
						}
					}
				}
			}
		}

		public CsvCommonIdManager(Dictionary<int, KeyValuePair<string, string>> idMap, Dictionary<string, int> nameMap, HashSet<int> enabledIdList, CsvCommonIdManager.Type type)
		{
			this.idMap = new SortedDictionary<int, KeyValuePair<string, string>>(idMap);
			this.nameMap = new Dictionary<string, int>(nameMap);
			this.enabledIdList = new HashSet<int>(enabledIdList);
			this.type = type;
		}

		public static void ReadEnabledIdList(CsvCommonIdManager.FileSystemType fileSystemType, bool readPathList, string fileName, ref HashSet<int> destList)
		{
			AFileSystemBase fileSystem = null;
			List<string> pathList = null;
			if (fileSystemType == CsvCommonIdManager.FileSystemType.Normal)
			{
				fileSystem = GameUty.FileSystem;
				if (readPathList)
				{
					pathList = GameUty.PathList;
				}
			}
			else if (fileSystemType == CsvCommonIdManager.FileSystemType.Old)
			{
				fileSystem = GameUty.FileSystemOld;
				if (readPathList)
				{
					pathList = GameUty.PathListOld;
				}
			}
			CsvCommonIdManager.ReadEnabledIdList(fileSystem, pathList, fileName, ref destList);
		}

		public static void ReadEnabledIdList(AFileSystemBase fileSystem, List<string> pathList, string fileName, ref HashSet<int> destList)
		{
			if (destList == null)
			{
				destList = new HashSet<int>();
			}
			string str = fileName;
			fileName += ".nei";
			if (!fileSystem.IsExistentFile(fileName))
			{
				if (pathList != null)
				{
					foreach (string str2 in pathList)
					{
						CsvCommonIdManager.ReadEnabledIdList(fileSystem, null, str + "_" + str2, ref destList);
					}
				}
				return;
			}
			using (AFileBase afileBase = fileSystem.FileOpen(fileName))
			{
				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 (!destList.Contains(cellAsInteger))
							{
								destList.Add(cellAsInteger);
							}
						}
					}
				}
			}
			if (pathList != null)
			{
				foreach (string str3 in pathList)
				{
					CsvCommonIdManager.ReadEnabledIdList(fileSystem, null, str + "_" + str3, ref destList);
				}
			}
		}

		public readonly SortedDictionary<int, KeyValuePair<string, string>> idMap;

		public readonly Dictionary<string, int> nameMap;

		public readonly HashSet<int> enabledIdList;

		public readonly CsvCommonIdManager.Type type;

		public enum Type
		{
			IdAndUniqueName,
			IdOnly
		}

		public enum FileSystemType
		{
			Normal,
			Old
		}
	}
}
