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

public class YotogiClassListManager : MonoBehaviour
{
	public static List<YotogiClassListManager.Data> CreateDatas(Status status)
	{
		List<YotogiClass.Data> list = new List<YotogiClass.Data>();
		foreach (YotogiClass.Data data in YotogiClass.GetAllDatas(true))
		{
			if (data.learnConditions.isLearnPossiblePersonal(status.personal) && data.classType != AbstractClassData.ClassType.Old)
			{
				list.Add(data);
			}
		}
		return YotogiClassListManager.CreateDatas(list, status);
	}

	public static List<YotogiClassListManager.Data> CreateDatasOld(Status status)
	{
		List<YotogiClass.Data> list = new List<YotogiClass.Data>();
		foreach (YotogiClass.Data data in YotogiClass.GetAllDatas(true))
		{
			if (data.learnConditions.isLearnPossiblePersonal(status.personal) && data.classType != AbstractClassData.ClassType.New)
			{
				list.Add(data);
			}
		}
		return YotogiClassListManager.CreateDatas(list, status);
	}

	private static List<YotogiClassListManager.Data> CreateDatas(List<YotogiClass.Data> datas, Status status)
	{
		List<YotogiClassListManager.Data> list = new List<YotogiClassListManager.Data>();
		List<KeyValuePair<string, bool>> list2 = new List<KeyValuePair<string, bool>>();
		foreach (YotogiClass.Data data in datas)
		{
			list2 = data.learnConditions.CreateConditionTextAndStaturResults(status);
			YotogiClassListManager.Data data2 = new YotogiClassListManager.Data();
			data2.classData = data;
			data2.isLearn = status.yotogiClass.Contains(data2.classData);
			data2.conditionDatas = list2.ToArray();
			data2.specialSkillList = YotogiClass.GetLearnSkillList(data2.classData.id);
			data2.specialSkillOldList = YotogiClass.GetLearnSkillOldList(data2.classData.id);
			list.Add(data2);
		}
		list.Sort();
		return list;
	}

	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.InstantiateItem(this.storageObject);
		}
		this.conditionDatas = new Dictionary<int, KeyValuePair<string, Color>[]>();
		this.instansToDatas = new Dictionary<int, YotogiClassListManager.Data>();
		this.conditionsObject.SetActive(false);
		this.specialSkillLabelsParent.SetActive(false);
		this.specialSkillLabelList = new List<UILabel>(UTY.GetChildObject(this.specialSkillLabelsParent, "UnitParent", false).GetComponentsInChildren<UILabel>());
		ClassChangePanel.CreateClassText();
	}

	public void CreateData(Maid maid)
	{
		if (this.conditionDatas == null)
		{
			this.Awake();
		}
		this.DeleteItemAllObject();
		this.conditionDatas.Clear();
		this.instansToDatas.Clear();
		Utility.ResetNGUI(this.itemGrid);
		Utility.ResetNGUI(this.scrollView);
		bool lockNTRPlay = GameMain.Instance.CharacterMgr.status.lockNTRPlay;
		List<KeyValuePair<string, Color>> list = new List<KeyValuePair<string, Color>>();
		int num = 0;
		List<YotogiClassListManager.Data> list2;
		if (MaidManagement.compatibilityMode)
		{
			list2 = YotogiClassListManager.CreateDatasOld(maid.status);
		}
		else
		{
			list2 = YotogiClassListManager.CreateDatas(maid.status);
		}
		List<YotogiClassListManager.Data> list3 = new List<YotogiClassListManager.Data>();
		foreach (YotogiClassListManager.Data data in list2)
		{
			if (!lockNTRPlay || !YotogiClass.IsNTRClass(data.classData.id, MaidManagement.compatibilityMode))
			{
				if (data.classData.learnConditions.isSpecialConditions())
				{
					AbstractClassData.LearnConditions learnConditions = data.classData.learnConditions;
					if (data.isLearn && !learnConditions.CheckSpecialConditions(maid.status))
					{
						continue;
					}
					if (!data.isLearn && !learnConditions.isFutureLearnPossible(maid.status))
					{
						continue;
					}
				}
				if (data.classData != null && data.classData.learnConditions.requestPersonals.Count != 0 && data.classData.classType != AbstractClassData.ClassType.Old)
				{
					list3.Add(data);
				}
				else
				{
					GameObject gameObject = this.CreateItemObject(maid, ++num, data);
					if (0 < data.conditionDatas.Length)
					{
						List<KeyValuePair<string, Color>> list4 = new List<KeyValuePair<string, Color>>();
						foreach (KeyValuePair<string, bool> keyValuePair in data.conditionDatas)
						{
							list4.Add(new KeyValuePair<string, Color>(keyValuePair.Key, (!keyValuePair.Value) ? Color.gray : Color.white));
						}
						this.conditionDatas.Add(gameObject.GetInstanceID(), list4.ToArray());
						this.instansToDatas.Add(gameObject.GetInstanceID(), data);
					}
				}
			}
		}
		foreach (YotogiClassListManager.Data data2 in list3)
		{
			GameObject gameObject2 = this.CreateItemObject(maid, -1, data2);
			if (0 < data2.conditionDatas.Length)
			{
				List<KeyValuePair<string, Color>> list5 = new List<KeyValuePair<string, Color>>();
				if (data2.classData != null)
				{
					foreach (int num2 in data2.classData.learnConditions.requestPersonals)
					{
						if (maid.status.personal.id == num2)
						{
							list5.Add(new KeyValuePair<string, Color>("ヒロインタイプ " + maid.status.personal.drawName, Color.white));
							break;
						}
					}
				}
				foreach (KeyValuePair<string, bool> keyValuePair2 in data2.conditionDatas)
				{
					list5.Add(new KeyValuePair<string, Color>(keyValuePair2.Key, (!keyValuePair2.Value) ? Color.gray : Color.white));
				}
				this.conditionDatas.Add(gameObject2.GetInstanceID(), list5.ToArray());
				this.instansToDatas.Add(gameObject2.GetInstanceID(), data2);
			}
		}
		Utility.ResetNGUI(this.itemGrid);
		Utility.ResetNGUI(this.scrollView);
	}

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

	private GameObject InstantiateItem(GameObject parent)
	{
		GameObject gameObject = Utility.CreatePrefab(parent, "SceneEdit/Profile/Prefab/ProfileYotogiSkillItemSimple", 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))
		{
			YotogiClassListManager.Data data = this.instansToDatas[instanceId];
			this.conditionTitleLabel.text = data.classData.drawName + " 取得条件";
			this.conditionList.SetTexts(this.conditionDatas[instanceId], 500);
			this.conditionBG.height = 90 + this.conditionList.height;
			this.conditionsObject.SetActive(true);
			foreach (UILabel uilabel in this.specialSkillLabelList)
			{
				uilabel.text = string.Empty;
			}
			int num = 0;
			if (MaidManagement.compatibilityMode)
			{
				foreach (KeyValuePair<int, List<Skill.Old.Data>> keyValuePair in data.specialSkillOldList)
				{
					foreach (Skill.Old.Data data2 in keyValuePair.Value)
					{
						if (num < this.specialSkillLabelList.Count)
						{
							this.specialSkillLabelList[num++].text = data2.name;
							if (this.specialSkillLabelList.Count == num)
							{
								this.specialSkillLabelList[num - 1].text = "etc...";
							}
						}
						if (this.specialSkillLabelList.Count == num)
						{
							break;
						}
					}
					if (this.specialSkillLabelList.Count == num)
					{
						break;
					}
				}
			}
			else
			{
				foreach (KeyValuePair<int, List<Skill.Data>> keyValuePair2 in data.specialSkillList)
				{
					foreach (Skill.Data data3 in keyValuePair2.Value)
					{
						if (num < this.specialSkillLabelList.Count)
						{
							this.specialSkillLabelList[num++].text = data3.name;
							if (this.specialSkillLabelList.Count == num)
							{
								this.specialSkillLabelList[num - 1].text = "etc...";
							}
						}
						if (this.specialSkillLabelList.Count == num)
						{
							break;
						}
					}
					if (this.specialSkillLabelList.Count == num)
					{
						break;
					}
				}
			}
			this.specialSkillLabelsParent.SetActive(!string.IsNullOrEmpty(this.specialSkillLabelList[0].text));
			if (this.specialSkillLabelsParent.activeSelf)
			{
				UIWidget component = UTY.GetChildObject(this.specialSkillLabelsParent, "BG", false).GetComponent<UIWidget>();
				component.height = 100 + 36 * num;
			}
		}
		else
		{
			this.OnHoverOutItem();
		}
	}

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

	private GameObject CreateItemObject(Maid maid, int no, YotogiClassListManager.Data data)
	{
		GameObject gameObject;
		if (this.storageObject.transform.childCount == 0)
		{
			gameObject = this.InstantiateItem(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 = data.classData.drawName;
		component2.color = (data.isLearn ? Color.white : Color.gray);
		gameObject.GetComponent<BoxCollider>().enabled = !data.isLearn;
		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;

	[SerializeField]
	private GameObject specialSkillLabelsParent;

	private UIGrid itemGrid;

	private UIWFConditionList conditionList;

	private UIWidget conditionBG;

	private List<UILabel> specialSkillLabelList;

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

	private Dictionary<int, YotogiClassListManager.Data> instansToDatas;

	public class Data : IComparable, IComparable<YotogiClassListManager.Data>
	{
		public int CompareTo(YotogiClassListManager.Data other)
		{
			return this.classData.CompareTo(other.classData);
		}

		public int CompareTo(object other)
		{
			return this.classData.CompareTo(((YotogiClassListManager.Data)other).classData);
		}

		public YotogiClass.Data classData;

		public KeyValuePair<string, bool>[] conditionDatas;

		public bool isLearn;

		public SortedList<int, List<Skill.Data>> specialSkillList;

		public SortedList<int, List<Skill.Old.Data>> specialSkillOldList;
	}
}
