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

public class PhotoMotionData
{
	private PhotoMotionData()
	{
	}

	public static void Create()
	{
		PhotoMotionData.motion_data_ = new List<PhotoMotionData>();
		HashSet<int> hashSet = new HashSet<int>();
		CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Normal, true, "phot_motion_enabled_list", ref hashSet);
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen("phot_motion_list.nei"))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, "phot_motion_list.nei\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i) && hashSet.Contains(csvParser.GetCellAsInteger(0, i)))
					{
						int num = 0;
						PhotoMotionData photoMotionData = new PhotoMotionData();
						photoMotionData.id = (long)csvParser.GetCellAsInteger(num++, i);
						photoMotionData.category = csvParser.GetCellAsString(num++, i);
						photoMotionData.name = csvParser.GetCellAsString(num++, i);
						photoMotionData.direct_file = csvParser.GetCellAsString(num++, i);
						photoMotionData.is_loop = (csvParser.GetCellAsString(num++, i) == "○");
						photoMotionData.call_script_fil = csvParser.GetCellAsString(num++, i);
						photoMotionData.call_script_label = csvParser.GetCellAsString(num++, i);
						photoMotionData.is_mod = false;
						string cellAsString = csvParser.GetCellAsString(num++, i);
						bool flag = csvParser.GetCellAsString(num++, i) == "○";
						photoMotionData.use_animekey_mune_l = (photoMotionData.use_animekey_mune_r = flag);
						photoMotionData.is_man_pose = (csvParser.GetCellAsString(num++, i) == "○");
						if (string.IsNullOrEmpty(cellAsString) || PluginData.IsEnabled(cellAsString))
						{
							PhotoMotionData.motion_data_.Add(photoMotionData);
							if (photoMotionData.call_script_fil == "h_edit_pose_001.ks" && photoMotionData.call_script_label == "*ポーズ001")
							{
								PhotoMotionData.init_data_ = photoMotionData;
							}
							else if (photoMotionData.is_man_pose && PhotoMotionData.init_data_for_man_ == null)
							{
								PhotoMotionData.init_data_for_man_ = photoMotionData;
							}
						}
					}
				}
			}
		}
		Action<string, List<string>> CheckModFile = null;
		CheckModFile = delegate(string path, List<string> result_list)
		{
			string[] files = Directory.GetFiles(path);
			for (int n = 0; n < files.Length; n++)
			{
				if (Path.GetExtension(files[n]) == ".anm")
				{
					result_list.Add(files[n]);
				}
			}
			string[] directories = Directory.GetDirectories(path);
			for (int num4 = 0; num4 < directories.Length; num4++)
			{
				CheckModFile(directories[num4], result_list);
			}
		};
		List<string> list = new List<string>();
		CheckModFile(PhotoWindowManager.path_photo_mod_motion, list);
		CRC32 crc = new CRC32();
		for (int j = 0; j < list.Count; j++)
		{
			long num2 = 0L;
			try
			{
				using (FileStream fileStream = new FileStream(list[j], FileMode.Open, FileAccess.Read))
				{
					byte[] array = new byte[fileStream.Length];
					fileStream.Read(array, 0, array.Length);
					uint num3 = crc.ComputeChecksum(array);
					num2 = (long)((ulong)-1 + (ulong)num3);
				}
			}
			catch
			{
			}
			if ((ulong)-1 <= (ulong)num2)
			{
				string fileName = Path.GetFileName(list[j]);
				PhotoMotionData photoMotionData2 = new PhotoMotionData();
				photoMotionData2.id = num2;
				photoMotionData2.category = "Mod";
				photoMotionData2.name = Path.GetFileNameWithoutExtension(fileName);
				photoMotionData2.direct_file = list[j];
				photoMotionData2.is_loop = false;
				photoMotionData2.call_script_fil = string.Empty;
				photoMotionData2.call_script_label = string.Empty;
				photoMotionData2.is_mod = true;
				PhotoMotionData.motion_data_.Add(photoMotionData2);
			}
		}
		Action<string, List<string>> action = delegate(string path, List<string> result_list)
		{
			string[] files = Directory.GetFiles(path);
			for (int n = 0; n < files.Length; n++)
			{
				if (Path.GetExtension(files[n]) == ".anm")
				{
					result_list.Add(files[n]);
				}
			}
		};
		list.Clear();
		action(PhotoModePoseSave.folder_path, list);
		foreach (string fullpath in list)
		{
			PhotoMotionData.AddMyPose(fullpath);
		}
		List<string> list2 = new List<string>();
		PhotoMotionData.category_list_ = new Dictionary<string, List<PhotoMotionData>>();
		for (int k = 0; k < PhotoMotionData.data.Count; k++)
		{
			PhotoMotionData photoMotionData3 = PhotoMotionData.data[k];
			if (!PhotoMotionData.category_list_.ContainsKey(photoMotionData3.category))
			{
				PhotoMotionData.category_list_.Add(photoMotionData3.category, new List<PhotoMotionData>());
				list2.Add(photoMotionData3.category);
			}
			PhotoMotionData.category_list_[photoMotionData3.category].Add(photoMotionData3);
		}
		HashSet<string> hashSet2 = new HashSet<string>();
		PhotoMotionData.popup_menu_list_ = new List<KeyValuePair<string, UnityEngine.Object>>();
		for (int l = 0; l < list2.Count; l++)
		{
			string text = list2[l];
			if (!hashSet2.Contains(text))
			{
				bool flag2 = false;
				List<PhotoMotionData> list3 = PhotoMotionData.category_list_[text];
				foreach (PhotoMotionData photoMotionData4 in list3)
				{
					if (!photoMotionData4.is_man_pose)
					{
						flag2 = true;
						break;
					}
				}
				if (flag2)
				{
					hashSet2.Add(text);
					PhotoMotionData.popup_menu_list_.Add(new KeyValuePair<string, UnityEngine.Object>(text, null));
				}
			}
		}
		if (!PhotoMotionData.category_list_.ContainsKey("マイポーズ"))
		{
			PhotoMotionData.popup_menu_list_.Add(new KeyValuePair<string, UnityEngine.Object>("マイポーズ", null));
			PhotoMotionData.category_list_.Add("マイポーズ", new List<PhotoMotionData>());
		}
		hashSet2.Clear();
		PhotoMotionData.popup_menu_list_for_man_ = new List<KeyValuePair<string, UnityEngine.Object>>();
		for (int m = 0; m < list2.Count; m++)
		{
			string text2 = list2[m];
			if (!hashSet2.Contains(text2))
			{
				bool flag3 = false;
				List<PhotoMotionData> list4 = PhotoMotionData.category_list_[text2];
				foreach (PhotoMotionData photoMotionData5 in list4)
				{
					if (photoMotionData5.is_man_pose)
					{
						flag3 = true;
						break;
					}
				}
				if (flag3)
				{
					hashSet2.Add(text2);
					PhotoMotionData.popup_menu_list_for_man_.Add(new KeyValuePair<string, UnityEngine.Object>(text2, null));
				}
			}
		}
	}

	public static PhotoMotionData AddMyPose(string fullpath)
	{
		string fileName = Path.GetFileName(fullpath);
		PhotoMotionData photoMotionData = new PhotoMotionData();
		photoMotionData.id = (long)fileName.GetHashCode();
		photoMotionData.category = "マイポーズ";
		photoMotionData.name = Path.GetFileNameWithoutExtension(fileName);
		photoMotionData.direct_file = fullpath;
		photoMotionData.is_loop = false;
		photoMotionData.call_script_fil = string.Empty;
		photoMotionData.call_script_label = string.Empty;
		photoMotionData.is_mod = false;
		photoMotionData.is_mypose = true;
		byte[] array = new byte[2];
		try
		{
			using (FileStream fileStream = new FileStream(photoMotionData.direct_file, FileMode.Open, FileAccess.Read))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					string b = binaryReader.ReadString();
					if ("CM3D2_ANIM" != b)
					{
						return null;
					}
					int num = binaryReader.ReadInt32();
					if (1001 <= num)
					{
						fileStream.Seek(-2L, SeekOrigin.End);
						fileStream.Read(array, 0, array.Length);
						photoMotionData.use_animekey_mune_l = (array[0] != 0);
						photoMotionData.use_animekey_mune_r = (array[1] != 0);
					}
					else
					{
						photoMotionData.use_animekey_mune_l = (photoMotionData.use_animekey_mune_r = false);
					}
				}
			}
		}
		catch
		{
		}
		PhotoMotionData.motion_data_.Add(photoMotionData);
		return photoMotionData;
	}

	public void Apply(Maid maid)
	{
		GameMain.Instance.ScriptMgr.StopMotionScript();
		if (maid == null || maid.body0 == null || maid.body0.m_Bones == null || maid.IsBusy)
		{
			return;
		}
		if (!maid.boMAN)
		{
			maid.body0.MuneYureL((float)((!this.use_animekey_mune_l) ? 1 : 0));
			maid.body0.MuneYureR((float)((!this.use_animekey_mune_r) ? 1 : 0));
			maid.body0.jbMuneL.enabled = (maid.body0.GetMuneYureL() != 0f);
			maid.body0.jbMuneR.enabled = (maid.body0.GetMuneYureR() != 0f);
		}
		if (!string.IsNullOrEmpty(this.direct_file))
		{
			maid.IKTargetToBone("左手", null, "無し", Vector3.zero, IKCtrlData.IKAttachType.Point, false, 0f, false, false);
			maid.IKTargetToBone("右手", null, "無し", Vector3.zero, IKCtrlData.IKAttachType.Point, false, 0f, false, false);
			if (!this.is_mod && !this.is_mypose)
			{
				maid.CrossFade(this.direct_file, false, this.is_loop, false, 0f, 1f);
			}
			else
			{
				byte[] array = new byte[0];
				try
				{
					using (FileStream fileStream = new FileStream(this.direct_file, FileMode.Open, FileAccess.Read))
					{
						array = new byte[fileStream.Length];
						fileStream.Read(array, 0, array.Length);
					}
				}
				catch
				{
				}
				if (0 < array.Length)
				{
					maid.body0.CrossFade(this.id.ToString(), array, false, this.is_loop, false, 0f, 1f);
					Maid.AutoTwist[] array2 = new Maid.AutoTwist[]
					{
						Maid.AutoTwist.ShoulderL,
						Maid.AutoTwist.ShoulderR,
						Maid.AutoTwist.WristL,
						Maid.AutoTwist.WristR,
						Maid.AutoTwist.ThighL,
						Maid.AutoTwist.ThighR
					};
					for (int i = 0; i < array2.Length; i++)
					{
						maid.SetAutoTwist(array2[i], true);
					}
				}
			}
		}
		else if (!string.IsNullOrEmpty(this.call_script_fil) && !string.IsNullOrEmpty(this.call_script_label))
		{
			CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
			int sloat = 0;
			for (int j = 0; j < characterMgr.GetMaidCount(); j++)
			{
				if (maid == characterMgr.GetMaid(j))
				{
					sloat = j;
					break;
				}
			}
			GameMain.Instance.ScriptMgr.LoadMotionScript(sloat, false, this.call_script_fil, this.call_script_label, maid.status.guid, string.Empty, true, true);
		}
	}

	public static List<PhotoMotionData> data
	{
		get
		{
			return PhotoMotionData.motion_data_;
		}
	}

	public static PhotoMotionData init_data
	{
		get
		{
			return PhotoMotionData.init_data_;
		}
	}

	public static PhotoMotionData init_data_for_man
	{
		get
		{
			return PhotoMotionData.init_data_for_man_;
		}
	}

	public static Dictionary<string, List<PhotoMotionData>> category_list
	{
		get
		{
			return PhotoMotionData.category_list_;
		}
	}

	public static List<KeyValuePair<string, UnityEngine.Object>> popup_category_list
	{
		get
		{
			return PhotoMotionData.popup_menu_list_;
		}
	}

	public static List<KeyValuePair<string, UnityEngine.Object>> popup_category_list_for_man
	{
		get
		{
			return PhotoMotionData.popup_menu_list_for_man_;
		}
	}

	public static PhotoMotionData Get(long id)
	{
		for (int i = 0; i < PhotoMotionData.motion_data_.Count; i++)
		{
			if (PhotoMotionData.motion_data_[i].id == id)
			{
				return PhotoMotionData.motion_data_[i];
			}
		}
		return null;
	}

	public long id;

	public string category;

	public string name;

	public string direct_file;

	public bool is_loop;

	public string call_script_fil;

	public string call_script_label;

	public bool is_mod;

	public bool is_mypose;

	public bool use_animekey_mune_l;

	public bool use_animekey_mune_r;

	public bool is_man_pose;

	private static PhotoMotionData init_data_;

	private static PhotoMotionData init_data_for_man_;

	private static List<PhotoMotionData> motion_data_;

	private static Dictionary<string, List<PhotoMotionData>> category_list_;

	private static List<KeyValuePair<string, UnityEngine.Object>> popup_menu_list_;

	private static List<KeyValuePair<string, UnityEngine.Object>> popup_menu_list_for_man_;
}
