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

namespace SceneEditWindow
{
	public class PoseIconData
	{
		public static List<PoseIconData.ItemData> itemList { get; private set; }

		public static PoseIconData.ItemData GetItemData(int id)
		{
			PoseIconData.ItemData result = default(PoseIconData.ItemData);
			foreach (PoseIconData.ItemData itemData in PoseIconData.itemList)
			{
				if (itemData.id == id)
				{
					result = itemData;
					break;
				}
			}
			return result;
		}

		public static void Create()
		{
			if (PoseIconData.itemList != null)
			{
				return;
			}
			HashSet<int> hashSet = new HashSet<int>();
			CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Normal, true, "edit_pose_enabled_list", ref hashSet);
			HashSet<int> idHash = new HashSet<int>();
			HashSet<string> texNameHash = new HashSet<string>();
			PoseIconData.itemList = new List<PoseIconData.ItemData>();
			using (AFileBase afileBase = GameUty.FileSystem.FileOpen("edit_pose.nei"))
			{
				using (CsvParser csvParser = new CsvParser())
				{
					bool condition = csvParser.Open(afileBase);
					NDebug.Assert(condition, "edit_pose.nei\nopen failed.");
					for (int i = 1; i < csvParser.max_cell_y; i++)
					{
						if (csvParser.IsCellToExistData(0, i))
						{
							int num = 0;
							PoseIconData.ItemData item = default(PoseIconData.ItemData);
							item.id = csvParser.GetCellAsInteger(num++, i);
							if (hashSet.Contains(item.id))
							{
								if (!idHash.Contains(item.id))
								{
									idHash.Add(item.id);
									item.iconTexName = Path.ChangeExtension(csvParser.GetCellAsString(num++, i).ToString(), "tex");
									if (!texNameHash.Contains(item.iconTexName))
									{
										texNameHash.Add(item.iconTexName);
										item.targetBoneName = csvParser.GetCellAsString(num++, i);
										item.callScriptFileName = Path.ChangeExtension(csvParser.GetCellAsString(num++, i).ToString(), "ks");
										item.callLabelName = csvParser.GetCellAsString(num++, i);
										item.fileSystem = GameUty.FileSystem;
										PoseIconData.itemList.Add(item);
									}
								}
							}
						}
					}
				}
			}
			int poseCount = 0;
			Action<string> action = delegate(string fileName)
			{
				if (!GameUty.FileSystemOld.IsExistentFile(fileName))
				{
					return;
				}
				using (AFileBase afileBase2 = GameUty.FileSystemOld.FileOpen(fileName))
				{
					using (CsvParser csvParser2 = new CsvParser())
					{
						bool condition2 = csvParser2.Open(afileBase2);
						NDebug.Assert(condition2, fileName + "\nopen failed.");
						for (int j = 1; j < csvParser2.max_cell_y; j++)
						{
							if (csvParser2.IsCellToExistData(0, j))
							{
								int cell_x = 0;
								PoseIconData.ItemData item2 = default(PoseIconData.ItemData);
								item2.iconTexName = Path.ChangeExtension(csvParser2.GetCellAsString(cell_x++, j).ToString(), "tex");
								if (!texNameHash.Contains(item2.iconTexName))
								{
									texNameHash.Add(item2.iconTexName);
									item2.targetBoneName = csvParser2.GetCellAsString(cell_x++, j);
									item2.callScriptFileName = Path.ChangeExtension(csvParser2.GetCellAsString(cell_x++, j).ToString(), "ks");
									item2.callLabelName = csvParser2.GetCellAsString(cell_x++, j);
									if (csvParser2.IsCellToExistData(cell_x, j))
									{
										item2.id = csvParser2.GetCellAsInteger(cell_x, j) + 910000;
									}
									else
									{
										item2.id = 900000 + poseCount++;
									}
									if (!idHash.Contains(item2.id))
									{
										idHash.Add(item2.id);
										item2.fileSystem = GameUty.FileSystemOld;
										PoseIconData.itemList.Add(item2);
									}
								}
							}
						}
					}
				}
			};
			foreach (string str in GameUty.PathListOld)
			{
				action("edit_pose_" + str + ".nei");
			}
			PoseIconData.itemList.Sort((PoseIconData.ItemData a, PoseIconData.ItemData b) => a.id - b.id);
		}

		public struct ItemData
		{
			public bool ExecScript()
			{
				if (GameMain.Instance == null || GameMain.Instance.ScriptMgr == null || string.IsNullOrEmpty(this.callScriptFileName))
				{
					return false;
				}
				ScriptManager scriptMgr = GameMain.Instance.ScriptMgr;
				if (this.fileSystem != GameUty.FileSystem)
				{
					scriptMgr.compatibilityMode = true;
				}
				scriptMgr.is_motion_blend = false;
				scriptMgr.LoadMotionScript(0, false, this.callScriptFileName, this.callLabelName, string.Empty, string.Empty, false, true);
				scriptMgr.is_motion_blend = true;
				if (scriptMgr.compatibilityMode)
				{
					scriptMgr.compatibilityMode = false;
				}
				return true;
			}

			public int id;

			public string iconTexName;

			public string targetBoneName;

			public string callScriptFileName;

			public string callLabelName;

			public AFileSystemBase fileSystem;
		}
	}
}
