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

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

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

		public static EditCharacterDatabase.Data GetData(int id)
		{
			EditCharacterDatabase.CreateData();
			NDebug.Assert(EditCharacterDatabase.basicDatas.ContainsKey(id), "NPCエディット\nID[" + id + "]のデータは存在しません");
			return EditCharacterDatabase.basicDatas[id];
		}

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

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

		public static void CreateData()
		{
			if (EditCharacterDatabase.commonIdManager != null)
			{
				return;
			}
			EditCharacterDatabase.commonIdManager = new CsvCommonIdManager("npcedit", "NPCエディット", CsvCommonIdManager.Type.IdOnly, null);
			EditCharacterDatabase.basicDatas = new Dictionary<int, EditCharacterDatabase.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 = "npcedit_" + 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 EditCharacterDatabase.commonIdManager.idMap)
			{
				EditCharacterDatabase.basicDatas.Add(keyValuePair.Key, new EditCharacterDatabase.Data(keyValuePair.Key, array2[0].Value));
			}
			foreach (KeyValuePair<AFileBase, CsvParser> keyValuePair2 in array2)
			{
				keyValuePair2.Value.Dispose();
				keyValuePair2.Key.Dispose();
			}
			HashSet<int> hashSet = new HashSet<int>();
			foreach (int num in EditCharacterDatabase.commonIdManager.enabledIdList)
			{
				EditCharacterDatabase.Data data = EditCharacterDatabase.GetData(num);
				if (string.IsNullOrEmpty(data.checkPluginName) || PluginData.IsEnabled(data.checkPluginName))
				{
					if (data.checkLoadFolders != null && 0 < data.checkLoadFolders.Length)
					{
						bool flag = false;
						foreach (string item in data.checkLoadFolders)
						{
							if (GameUty.loadArchiveList.Contains(item))
							{
								flag = true;
							}
						}
						if (!flag)
						{
							continue;
						}
					}
					hashSet.Add(num);
				}
			}
			EditCharacterDatabase.commonIdManager = new CsvCommonIdManager(new Dictionary<int, KeyValuePair<string, string>>(EditCharacterDatabase.commonIdManager.idMap), EditCharacterDatabase.commonIdManager.nameMap, hashSet, CsvCommonIdManager.Type.IdOnly);
		}

		private const string csvTopCommonName = "npcedit";

		private const string typeNameForErrorLog = "NPCエディット";

		private static CsvCommonIdManager commonIdManager;

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

		public class Data
		{
			public Data(int id, CsvParser basicCsv)
			{
				for (int i = 1; i < basicCsv.max_cell_y; i++)
				{
					if (basicCsv.IsCellToExistData(0, i) && basicCsv.GetCellAsInteger(0, i) == id)
					{
						int num = 1;
						this.id = id;
						string cellAsString = basicCsv.GetCellAsString(num++, i);
						if (CharacterMgr.npcDatas.ContainsKey(cellAsString))
						{
							if (!CharacterMgr.npcDatas.ContainsKey(cellAsString))
							{
								Debug.LogError("NPCエディットエラー ユニーク名[" + cellAsString + "]のキャラプリセットが存在しません");
							}
							this.npcData = CharacterMgr.npcDatas[cellAsString];
						}
						this.isCBLEnabled = (basicCsv.GetCellAsString(num++, i) == "〇");
						this.checkPluginName = basicCsv.GetCellAsString(num++, i);
						string cellAsString2 = basicCsv.GetCellAsString(num++, i);
						if (!string.IsNullOrEmpty(cellAsString2))
						{
							this.checkLoadFolders = cellAsString2.Split(new char[]
							{
								'|'
							});
						}
						else
						{
							this.checkLoadFolders = new string[0];
						}
						string cellAsString3 = basicCsv.GetCellAsString(num++, i);
						if (!string.IsNullOrEmpty(cellAsString3))
						{
							List<CharacterMgr.NpcData> list = new List<CharacterMgr.NpcData>();
							foreach (string text in cellAsString3.Split(new char[]
							{
								'|'
							}))
							{
								if (!CharacterMgr.npcDatas.ContainsKey(text))
								{
									Debug.LogError("NPCエディットエラー ユニーク名[" + text + "]のキャラプリセットが存在しません");
								}
								list.Add(CharacterMgr.npcDatas[text]);
							}
							this.overRideTargetNpc = list.ToArray();
						}
						else
						{
							this.overRideTargetNpc = new CharacterMgr.NpcData[]
							{
								this.npcData
							};
						}
						this.additionalInformationText = basicCsv.GetCellAsString(num++, i);
						this.additionalInformationTextForCBL = basicCsv.GetCellAsString(num++, i);
					}
				}
			}

			public Texture2D GetThumbnail()
			{
				if (this.npcData.isResourcesLoad)
				{
					return GameMain.Instance.CharacterMgr.PresetLoadFromResources(this.npcData.presetFileName).texThum;
				}
				using (AFileBase afileBase = GameUty.FileOpen(this.npcData.presetFileName + ".preset", null))
				{
					if (afileBase != null && afileBase.IsValid())
					{
						byte[] buffer = afileBase.ReadAll();
						using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer), Encoding.UTF8))
						{
							return GameMain.Instance.CharacterMgr.PresetLoad(binaryReader, this.npcData.presetFileName).texThum;
						}
					}
				}
				return null;
			}

			public readonly int id;

			public readonly CharacterMgr.NpcData npcData;

			public readonly bool isCBLEnabled;

			public readonly string checkPluginName;

			public readonly string[] checkLoadFolders;

			public readonly string additionalInformationText;

			public readonly string additionalInformationTextForCBL;

			public readonly CharacterMgr.NpcData[] overRideTargetNpc;
		}
	}
}
