using System;
using System.Collections.Generic;
using MaidStatus;
using UnityEngine;
using wf;
using Yotogis;

public class YotogiSkillListManager : MonoBehaviour
{
	public static Dictionary<int, YotogiSkillListManager.Data> CreateDatas(Status status, bool specialConditionCheck, Skill.Data.SpecialConditionType type = Skill.Data.SpecialConditionType.Null)
	{
		Dictionary<int, YotogiSkillListManager.Data> dictionary = new Dictionary<int, YotogiSkillListManager.Data>();
		List<KeyValuePair<string, bool>> list = new List<KeyValuePair<string, bool>>();
		foreach (SortedDictionary<int, Skill.Data> sortedDictionary in Skill.skill_data_list)
		{
			foreach (KeyValuePair<int, Skill.Data> keyValuePair in sortedDictionary)
			{
				list.Clear();
				Skill.Data value = keyValuePair.Value;
				if (!specialConditionCheck || value.specialConditionType == type)
				{
					if (value.IsExecSeikeiken(status.seikeiken) && value.IsExecRelation(status.relation) && value.IsExecContract(status.contract) && value.IsExecPersonal(status.personal))
					{
						YotogiSkillData yotogiSkillData = status.yotogiSkill.Get(value.id);
						if (yotogiSkillData == null)
						{
							if (value.getcondition_data.yotogi_class != null && !value.getcondition_data.yotogi_class.learnConditions.isFutureLearnPossible(status))
							{
								continue;
							}
							list = value.getcondition_data.CreateConditionTextAndStaturResults(status);
						}
						if (list.Count == 0)
						{
							list.Add(new KeyValuePair<string, bool>("なし", true));
						}
						YotogiSkillListManager.Data data = new YotogiSkillListManager.Data();
						data.skillData = value;
						data.conditionDatas = list.ToArray();
						data.maidStatusSkillData = yotogiSkillData;
						dictionary.Add(data.skillData.id, data);
					}
				}
			}
		}
		return dictionary;
	}

	public static Dictionary<int, YotogiSkillListManager.Data> CreateDatasOld(Status status)
	{
		Dictionary<int, YotogiSkillListManager.Data> dictionary = new Dictionary<int, YotogiSkillListManager.Data>();
		List<KeyValuePair<string, bool>> list = new List<KeyValuePair<string, bool>>();
		foreach (SortedDictionary<int, Skill.Old.Data> sortedDictionary in Skill.Old.skill_data_list)
		{
			foreach (KeyValuePair<int, Skill.Old.Data> keyValuePair in sortedDictionary)
			{
				list.Clear();
				Skill.Old.Data value = keyValuePair.Value;
				if (value.IsExecSeikeiken(status.seikeiken) && value.IsExecRelation(status.relation))
				{
					YotogiSkillData yotogiSkillData = status.yotogiSkill.Get(value.id);
					if (yotogiSkillData == null)
					{
						list = value.getcondition_data.CreateConditionTextAndStaturResults(status);
					}
					if (list.Count == 0)
					{
						list.Add(new KeyValuePair<string, bool>("なし", true));
					}
					YotogiSkillListManager.Data data = new YotogiSkillListManager.Data();
					data.skillDataOld = value;
					data.conditionDatas = list.ToArray();
					data.maidStatusSkillData = yotogiSkillData;
					dictionary.Add(value.id, data);
				}
			}
		}
		return dictionary;
	}

	private void Awake()
	{
		if (this.conditionDatas != null)
		{
			return;
		}
		this.itemGrid = this.contentObject.GetComponent<UIGrid>();
		NDebug.AssertNull(this.itemGrid != null);
		this.conditionList = this.conditionsObject.GetComponentInChildren<UIWFConditionList>();
		NDebug.AssertNull(this.conditionList != null);
		this.conditionBG = UTY.GetChildObject(this.conditionsObject, "BG", false).GetComponent<UIWidget>();
		for (int i = 0; i < 50; i++)
		{
			this.InstantiateSkillItem(this.storageObject);
		}
		this.conditionDatas = new Dictionary<int, KeyValuePair<string, Color>[]>();
		this.instansToSkillDatas = new Dictionary<int, KeyValuePair<Skill.Data, Skill.Old.Data>>();
		this.conditionsObject.SetActive(false);
	}

	public void CreateData(Maid maid)
	{
		if (this.conditionDatas == null)
		{
			this.Awake();
		}
		this.DeleteItemAllObject();
		this.conditionDatas.Clear();
		this.instansToSkillDatas.Clear();
		Utility.ResetNGUI(this.itemGrid);
		Utility.ResetNGUI(this.scrollView);
		List<KeyValuePair<string, Color>> list = new List<KeyValuePair<string, Color>>();
		int num = 0;
		Dictionary<int, YotogiSkillListManager.Data> dictionary;
		if (MaidManagement.compatibilityMode)
		{
			dictionary = YotogiSkillListManager.CreateDatasOld(maid.status);
		}
		else
		{
			dictionary = YotogiSkillListManager.CreateDatas(maid.status, false, Skill.Data.SpecialConditionType.Null);
		}
		List<YotogiSkillListManager.Data> list2 = new List<YotogiSkillListManager.Data>();
		bool lockNTRPlay = GameMain.Instance.CharacterMgr.status.lockNTRPlay;
		foreach (KeyValuePair<int, YotogiSkillListManager.Data> keyValuePair in dictionary)
		{
			YotogiSkillListManager.Data value = keyValuePair.Value;
			if (value.skillData != null)
			{
				if (value.skillData.specialConditionType == Skill.Data.SpecialConditionType.Faint)
				{
					continue;
				}
				if (lockNTRPlay && (value.skillData.category == Yotogi.Category.交換 || value.skillData.category == Yotogi.Category.乱交))
				{
					continue;
				}
			}
			else if (value.skillDataOld != null && lockNTRPlay && (value.skillDataOld.category == YotogiOld.Category.交換 || value.skillDataOld.category == YotogiOld.Category.乱交))
			{
				continue;
			}
			if (value.skillData != null && value.skillData.getcondition_data.requestPersonals.Length != 0)
			{
				list2.Add(value);
			}
			else
			{
				string skillName = (value.skillData == null) ? value.skillDataOld.name : value.skillData.name;
				int skillId = (value.skillData == null) ? value.skillDataOld.id : value.skillData.id;
				GameObject gameObject = this.CreateItemObject(maid, ++num, skillName, skillId);
				if (0 < value.conditionDatas.Length)
				{
					List<KeyValuePair<string, Color>> list3 = new List<KeyValuePair<string, Color>>();
					foreach (KeyValuePair<string, bool> keyValuePair2 in value.conditionDatas)
					{
						list3.Add(new KeyValuePair<string, Color>(keyValuePair2.Key, (!keyValuePair2.Value) ? Color.gray : Color.white));
					}
					this.conditionDatas.Add(gameObject.GetInstanceID(), list3.ToArray());
					this.instansToSkillDatas.Add(gameObject.GetInstanceID(), new KeyValuePair<Skill.Data, Skill.Old.Data>(value.skillData, value.skillDataOld));
				}
			}
		}
		foreach (YotogiSkillListManager.Data data in list2)
		{
			string skillName2 = (data.skillData == null) ? data.skillDataOld.name : data.skillData.name;
			int skillId2 = (data.skillData == null) ? data.skillDataOld.id : data.skillData.id;
			GameObject gameObject2 = this.CreateItemObject(maid, -1, skillName2, skillId2);
			if (0 < data.conditionDatas.Length)
			{
				List<KeyValuePair<string, Color>> list4 = new List<KeyValuePair<string, Color>>();
				if (data.skillData != null)
				{
					foreach (Personal.Data data2 in data.skillData.getcondition_data.requestPersonals)
					{
						if (maid.status.personal.id == data2.id)
						{
							list4.Add(new KeyValuePair<string, Color>("ヒロインタイプ " + maid.status.personal.drawName, Color.white));
							break;
						}
					}
				}
				foreach (KeyValuePair<string, bool> keyValuePair3 in data.conditionDatas)
				{
					list4.Add(new KeyValuePair<string, Color>(keyValuePair3.Key, (!keyValuePair3.Value) ? Color.gray : Color.white));
				}
				this.conditionDatas.Add(gameObject2.GetInstanceID(), list4.ToArray());
				this.instansToSkillDatas.Add(gameObject2.GetInstanceID(), new KeyValuePair<Skill.Data, Skill.Old.Data>(data.skillData, data.skillDataOld));
			}
		}
		Utility.ResetNGUI(this.itemGrid);
		Utility.ResetNGUI(this.scrollView);
	}

	private void OnEnable()
	{
		Utility.ResetNGUI(this.itemGrid);
		Utility.ResetNGUI(this.scrollView);
		this.OnHoverOutItem();
	}

	private GameObject InstantiateSkillItem(GameObject parent)
	{
		GameObject gameObject = Utility.CreatePrefab(parent, "SceneEdit/Profile/Prefab/ProfileYotogiSkillItem", true);
		UIEventTrigger component = gameObject.GetComponent<UIEventTrigger>();
		EventDelegate eventDelegate = new EventDelegate(this, "OnHoverOverItem");
		eventDelegate.parameters[0].value = gameObject.GetInstanceID();
		EventDelegate.Add(component.onHoverOver, eventDelegate);
		EventDelegate.Add(component.onHoverOut, new EventDelegate.Callback(this.OnHoverOutItem));
		return gameObject;
	}

	private void OnHoverOverItem(int instanceId)
	{
		if (this.conditionDatas.ContainsKey(instanceId))
		{
			KeyValuePair<Skill.Data, Skill.Old.Data> keyValuePair = this.instansToSkillDatas[instanceId];
			this.conditionTitleLabel.text = ((keyValuePair.Key == null) ? keyValuePair.Value.name : keyValuePair.Key.name) + " 取得条件";
			this.conditionList.SetTexts(this.conditionDatas[instanceId], 500);
			this.conditionBG.height = 90 + this.conditionList.height;
			this.conditionsObject.SetActive(true);
		}
		else
		{
			this.OnHoverOutItem();
		}
	}

	private void OnHoverOutItem()
	{
		this.conditionsObject.SetActive(false);
	}

	private GameObject CreateItemObject(Maid maid, int no, string skillName, int skillId)
	{
		GameObject gameObject;
		if (this.storageObject.transform.childCount == 0)
		{
			gameObject = this.InstantiateSkillItem(this.storageObject);
		}
		else
		{
			gameObject = this.storageObject.transform.GetChild(0).gameObject;
		}
		gameObject.transform.SetParent(this.contentObject.transform, false);
		if (0 <= no)
		{
			gameObject.name = no.ToString("D4");
		}
		else
		{
			gameObject.name = "EX";
		}
		UILabel component = UTY.GetChildObject(gameObject, "Number/Value", false).GetComponent<UILabel>();
		if (0 <= no)
		{
			component.text = no.ToString();
		}
		else
		{
			component.text = "EX";
		}
		UILabel component2 = UTY.GetChildObject(gameObject, "SkillName/Value", false).GetComponent<UILabel>();
		component2.text = skillName;
		UISprite component3 = UTY.GetChildObject(gameObject, "LevelOfAchievement/Icon", false).GetComponent<UISprite>();
		int num = (!maid.status.yotogiSkill.Contains(skillId)) ? 0 : maid.status.yotogiSkill.Get(skillId).level;
		string spriteName = "profile_yotogiskill_sign_minus";
		if (num != 1)
		{
			if (num != 2)
			{
				if (num == 3)
				{
					spriteName = "cm3d2_edit_profile_yotogiskill_sign_nijumaru";
				}
			}
			else
			{
				spriteName = "cm3d2_edit_profile_yotogiskill_sign_maru";
			}
		}
		else
		{
			spriteName = "cm3d2_edit_profile_yotogiskill_sign_sankaku";
		}
		if (maid.status.yotogiSkill.Contains(skillId) && maid.status.yotogiSkill.Get(skillId).playCount == 0u)
		{
			spriteName = "cm3d2_edit_profile_yotogiskill_sign_batu";
		}
		component3.spriteName = spriteName;
		component2.color = ((num != 0) ? Color.white : Color.gray);
		gameObject.GetComponent<BoxCollider>().enabled = (num == 0);
		return gameObject;
	}

	private void DeleteItemAllObject()
	{
		List<GameObject> list = new List<GameObject>();
		for (int i = 0; i < this.contentObject.transform.childCount; i++)
		{
			list.Add(this.contentObject.transform.GetChild(i).gameObject);
		}
		foreach (GameObject obj in list)
		{
			this.DeleteItemObject(obj);
		}
	}

	private void DeleteItemObject(GameObject obj)
	{
		if (obj.transform.parent != this.storageObject.transform)
		{
			obj.transform.SetParent(this.storageObject.transform, false);
		}
	}

	[SerializeField]
	private GameObject storageObject;

	[SerializeField]
	private GameObject contentObject;

	[SerializeField]
	private UIScrollView scrollView;

	[SerializeField]
	private GameObject conditionsObject;

	[SerializeField]
	private UILabel conditionTitleLabel;

	private UIGrid itemGrid;

	private UIWFConditionList conditionList;

	private UIWidget conditionBG;

	private Dictionary<int, KeyValuePair<string, Color>[]> conditionDatas;

	private Dictionary<int, KeyValuePair<Skill.Data, Skill.Old.Data>> instansToSkillDatas;

	public class Data
	{
		public bool enabled
		{
			get
			{
				return this.maidStatusSkillData != null;
			}
		}

		public Skill.Data skillData;

		public Skill.Old.Data skillDataOld;

		public KeyValuePair<string, bool>[] conditionDatas;

		public YotogiSkillData maidStatusSkillData;
	}
}
