using System;
using System.Collections.Generic;
using MaidStatus;

namespace SceneEditWindow
{
	public class VoiceIconData
	{
		public static Dictionary<int, VoiceIconData.ItemData> itemList { get; private set; }

		public static Dictionary<int, VoiceIconData.ItemData> itemSubList { get; private set; }

		public static VoiceIconData.ItemData GetItemData(Personal.Data data)
		{
			return VoiceIconData.itemList[data.id];
		}

		public static VoiceIconData.ItemData GetItemData(SubMaid.Data data)
		{
			return VoiceIconData.itemSubList[data.id];
		}

		public static VoiceIconData.ItemData.EmotionData GetEmotionData(Personal.Data data, VoiceIconData.ItemData.EmotionType type)
		{
			return VoiceIconData.GetItemData(data).emotionDatas[type];
		}

		public static VoiceIconData.ItemData.EmotionData GetEmotionData(SubMaid.Data data, VoiceIconData.ItemData.EmotionType type)
		{
			return VoiceIconData.GetItemData(data).emotionDatas[type];
		}

		public static VoiceIconData.ItemData.EmotionData GetEmotionData(Maid maid, VoiceIconData.ItemData.EmotionType type)
		{
			return (maid.status.subCharaData == null) ? VoiceIconData.GetEmotionData(maid.status.personal, type) : VoiceIconData.GetEmotionData(maid.status.subCharaData, type);
		}

		public static void Create()
		{
			if (VoiceIconData.itemList != null)
			{
				return;
			}
			VoiceIconData.itemList = new Dictionary<int, VoiceIconData.ItemData>();
			VoiceIconData.itemSubList = new Dictionary<int, VoiceIconData.ItemData>();
			using (AFileBase afileBase = GameUty.FileSystem.FileOpen("edit_voice.nei"))
			{
				using (CsvParser csvParser = new CsvParser())
				{
					bool condition = csvParser.Open(afileBase);
					NDebug.Assert(condition, "edit_voice.nei\nopen failed.");
					for (int i = 1; i < csvParser.max_cell_y; i++)
					{
						if (csvParser.IsCellToExistData(0, i))
						{
							int num = 0;
							VoiceIconData.ItemData.EmotionData emotionData = new VoiceIconData.ItemData.EmotionData();
							string cellAsString = csvParser.GetCellAsString(num++, i);
							if (!string.IsNullOrEmpty(cellAsString))
							{
								try
								{
									emotionData.type = (VoiceIconData.ItemData.EmotionType)Enum.Parse(typeof(VoiceIconData.ItemData.EmotionType), cellAsString);
								}
								catch (Exception e)
								{
									NDebug.AssertParseError("VoiceIconData.ItemData.EmotionsType", e);
								}
							}
							string cellAsString2 = csvParser.GetCellAsString(num++, i);
							if (Personal.Contains(cellAsString2))
							{
								emotionData.personalData = Personal.GetData(cellAsString2);
							}
							else
							{
								emotionData.subMaidData = SubMaid.GetData(cellAsString2);
							}
							emotionData.voiceFileName = csvParser.GetCellAsString(num++, i);
							emotionData.facialExpressions = csvParser.GetCellAsString(num++, i);
							emotionData.addFacialExpressions = csvParser.GetCellAsString(num++, i);
							VoiceIconData.ItemData itemData = new VoiceIconData.ItemData();
							itemData.emotionDatas = new Dictionary<VoiceIconData.ItemData.EmotionType, VoiceIconData.ItemData.EmotionData>();
							if (emotionData.personalData != null)
							{
								if (VoiceIconData.itemList.ContainsKey(emotionData.personalData.id))
								{
									itemData = VoiceIconData.itemList[emotionData.personalData.id];
								}
								else
								{
									VoiceIconData.itemList.Add(emotionData.personalData.id, itemData);
								}
								if (itemData.emotionDatas.ContainsKey(emotionData.type))
								{
									NDebug.Assert("データが重複しています", false);
								}
								itemData.emotionDatas.Add(emotionData.type, emotionData);
							}
							else
							{
								if (VoiceIconData.itemSubList.ContainsKey(emotionData.subMaidData.id))
								{
									itemData = VoiceIconData.itemSubList[emotionData.subMaidData.id];
								}
								else
								{
									VoiceIconData.itemSubList.Add(emotionData.subMaidData.id, itemData);
								}
								if (itemData.emotionDatas.ContainsKey(emotionData.type))
								{
									NDebug.Assert("データが重複しています", false);
								}
								itemData.emotionDatas.Add(emotionData.type, emotionData);
							}
						}
					}
				}
			}
		}

		public class ItemData
		{
			public static readonly string[] EmotionTexFileNames = new string[]
			{
				"cm3d2_voiceicon_joy.tex",
				"cm3d2_voiceicon_anger.tex",
				"cm3d2_voiceicon_comfort.tex",
				"cm3d2_voiceicon_pity.tex"
			};

			public Dictionary<VoiceIconData.ItemData.EmotionType, VoiceIconData.ItemData.EmotionData> emotionDatas;

			public enum EmotionType
			{
				Joy,
				Anger,
				Sorrow,
				Fun
			}

			public class EmotionData
			{
				public void Apply(Maid maid)
				{
					if (maid == null)
					{
						return;
					}
					GameMain.Instance.SoundMgr.VoiceStopAll();
					maid.AudioMan.LoadPlay(this.voiceFileName, 0f, false, false);
					maid.FaceAnime(this.facialExpressions, 1f, 0);
					if (string.IsNullOrEmpty(this.addFacialExpressions))
					{
						maid.FaceBlend("無し");
					}
					else
					{
						maid.FaceBlend(this.addFacialExpressions);
					}
				}

				public Personal.Data personalData;

				public SubMaid.Data subMaidData;

				public string voiceFileName;

				public string facialExpressions;

				public string addFacialExpressions;

				public VoiceIconData.ItemData.EmotionType type;
			}
		}
	}
}
