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

public class FreeSkillSelectOld : MonoBehaviour
{
	public void Awake()
	{
		if (this.update_obj_dic_ != null)
		{
			return;
		}
		this.update_obj_dic_ = new Dictionary<string, FreeSkillSelectOld.UpdateObject>();
		Func<GameObject, FreeSkillSelectOld.UpdateObject> func = delegate(GameObject top_obj)
		{
			GameObject childObject = UTY.GetChildObject(top_obj, "Content", false);
			FreeSkillSelectOld.UpdateObject updateObject = new FreeSkillSelectOld.UpdateObject();
			updateObject.storage_obj = UTY.GetChildObject(childObject, "StorageRoom", true);
			updateObject.arrow_obj = UTY.GetChildObject(top_obj, "Arrow", false);
			updateObject.ui_panel = childObject.GetComponent<UIPanel>();
			updateObject.scroll_view = childObject.GetComponent<UIScrollView>();
			updateObject.button_parent = UTY.GetChildObject(childObject, "GridParent", false);
			updateObject.ui_grid = updateObject.button_parent.GetComponent<UIGrid>();
			updateObject.tab_panel = updateObject.button_parent.GetComponent<UIWFTabPanel>();
			return updateObject;
		};
		string[] array = new string[]
		{
			"TypeCategoryViewer",
			"CategoryViewer",
			"SkillSelectViewer",
			"LevelViewer",
			"StageSelectViewer"
		};
		string[] array2 = new string[]
		{
			"タイプ",
			"カテゴリー",
			"スキル",
			"レベル",
			"ステージ"
		};
		for (int i = 0; i < array.Length; i++)
		{
			this.update_obj_dic_.Add(array2[i], func(UTY.GetChildObject(base.gameObject, array[i], false)));
		}
	}

	public void Start()
	{
	}

	public void SetMaid(Maid maid)
	{
		this.maid_ = maid;
	}

	public void SetYotogiManager(YotogiOldManager yotogi_mgr)
	{
		this.yotogi_mgr_ = yotogi_mgr;
	}

	public void CreateInstanceButton()
	{
		if (this.button_data_list_ != null)
		{
			return;
		}
		this.button_data_list_ = FreeSkillSelectOld.CreateButtonData(this.maid_);
		FreeSkillSelectOld.UpdateObject updateObject = this.update_obj_dic_["ステージ"];
		FreeSkillSelectOld.UpdateObject updateObject2 = this.update_obj_dic_["タイプ"];
		FreeSkillSelectOld.UpdateObject updateObject3 = this.update_obj_dic_["レベル"];
		foreach (KeyValuePair<string, FreeSkillSelectOld.UpdateObject> keyValuePair in this.update_obj_dic_)
		{
			FreeSkillSelectOld.UpdateObject value = keyValuePair.Value;
			value.Clear();
			value.Update();
		}
		int grade_id = 99;
		List<KeyValuePair<YotogiOld.StageData, bool>> playableStageList = YotogiOld.GetPlayableStageList(grade_id, null);
		for (int i = 0; i < playableStageList.Count; i++)
		{
			if (playableStageList[i].Value)
			{
				GameObject gameObject = Utility.CreatePrefab(updateObject.button_parent, "SceneYotogi/StageSelect/Prefab/Old/StageUnit", true);
				gameObject.name = playableStageList[i].Key.stage.ToString();
				YotogiOldStageUnit component = gameObject.GetComponent<YotogiOldStageUnit>();
				component.SetStageData(playableStageList[i].Key, playableStageList[i].Value);
				component.SetOnSelectEvent(new YotogiOldStageUnit.OnSelectEvent(this.OnClickStageEvent));
				component.is_change_bg = false;
			}
		}
		updateObject.Update();
		GameObject button_parent = updateObject3.button_parent;
		for (int j = 0; j < 3; j++)
		{
			EventDelegate onclick_event = new EventDelegate(this, "OnClickLevelBtnEvent");
			UIWFTabButton uiwftabButton = this.CreateTypeAndCategoryButton(updateObject3, "Lv." + (j + 1).ToString(), true, onclick_event);
			uiwftabButton.name = (j + 1).ToString();
		}
		updateObject3.Update();
		List<Transform> childList = updateObject3.ui_grid.GetChildList();
		for (int k = 0; k < childList.Count; k++)
		{
			UIWFTabButton componentInChildren = childList[k].gameObject.GetComponentInChildren<UIWFTabButton>();
			if (!(componentInChildren == null) && componentInChildren.isEnabled)
			{
				updateObject3.tab_panel.Select(componentInChildren);
				break;
			}
		}
		GameObject button_parent2 = updateObject2.button_parent;
		for (int l = 0; l < this.button_data_list_.Count; l++)
		{
			FreeSkillSelectOld.ButtonData buttonData = this.button_data_list_[l];
			EventDelegate eventDelegate = new EventDelegate(this, "OnClickTypeBtnEvent");
			eventDelegate.parameters[0].value = buttonData;
			buttonData.tab_button_obj = this.CreateTypeAndCategoryButton(updateObject2, buttonData.name, 0 < this.button_data_list_[l].children_list.Count, eventDelegate);
		}
		updateObject2.Update();
		childList = updateObject2.ui_grid.GetChildList();
		for (int m = 0; m < childList.Count; m++)
		{
			UIWFTabButton componentInChildren2 = childList[m].gameObject.GetComponentInChildren<UIWFTabButton>();
			if (!(componentInChildren2 == null) && componentInChildren2.isEnabled)
			{
				updateObject2.tab_panel.Select(componentInChildren2);
				break;
			}
		}
	}

	public void SelectSkill(Skill.Old.Data skill, YotogiOld.Stage stage = YotogiOld.Stage.サロン)
	{
		if (skill == null)
		{
			return;
		}
		FreeSkillSelectOld.ButtonData buttonData = null;
		FreeSkillSelectOld.ButtonData buttonData2 = null;
		FreeSkillSelectOld.ButtonData buttonData3 = null;
		bool flag = true;
		int num = 0;
		while (num < this.button_data_list_.Count && flag)
		{
			buttonData = this.button_data_list_[num];
			int num2 = 0;
			while (num2 < buttonData.children_list.Count && flag)
			{
				buttonData2 = buttonData.children_list[num2];
				int num3 = 0;
				while (num3 < buttonData2.children_list.Count && flag)
				{
					buttonData3 = buttonData2.children_list[num3];
					if (buttonData3.skill_data == skill)
					{
						flag = false;
					}
					num3++;
				}
				num2++;
			}
			num++;
		}
		if (flag)
		{
			return;
		}
		if (!buttonData.tab_button_obj.isSelected)
		{
			this.update_obj_dic_["タイプ"].tab_panel.Select(buttonData.tab_button_obj);
		}
		if (!buttonData2.tab_button_obj.isSelected)
		{
			this.update_obj_dic_["カテゴリー"].tab_panel.Select(buttonData2.tab_button_obj);
		}
		this.user_request_stage = stage;
		if (!buttonData3.tab_button_obj.isSelected)
		{
			this.update_obj_dic_["スキル"].tab_panel.Select(buttonData3.tab_button_obj);
		}
	}

	public void FixSkill()
	{
		if (this.yotogi_mgr_ == null || this.yotogi_mgr_.maid == null)
		{
			return;
		}
		YotogiOldStageSelectManager.SelectStage(YotogiOld.stage_data_list[this.select_stage]);
		Skill.Old.Data[] playSkillArray = new Skill.Old.Data[]
		{
			this.select_skill
		};
		this.yotogi_mgr_.SetPlaySkillArray(playSkillArray);
	}

	private void CreateCategory(FreeSkillSelectOld.ButtonData type_button_data)
	{
		FreeSkillSelectOld.UpdateObject updateObject = this.update_obj_dic_["カテゴリー"];
		updateObject.MoveStorageRoom(true);
		updateObject.Clear();
		GameObject button_parent = updateObject.button_parent;
		bool lockNTRPlay = GameMain.Instance.CharacterMgr.status.lockNTRPlay;
		int i = 0;
		while (i < type_button_data.children_list.Count)
		{
			FreeSkillSelectOld.ButtonData buttonData = type_button_data.children_list[i];
			if (!lockNTRPlay)
			{
				goto IL_87;
			}
			YotogiOld.Category category = (YotogiOld.Category)Enum.Parse(typeof(YotogiOld.Category), buttonData.name);
			if (category != YotogiOld.Category.交換 && category != YotogiOld.Category.乱交)
			{
				goto IL_87;
			}
			IL_E7:
			i++;
			continue;
			IL_87:
			bool flag = buttonData.children_list != null && 0 < buttonData.children_list.Count;
			EventDelegate eventDelegate = null;
			if (flag)
			{
				eventDelegate = new EventDelegate(this, "OnClickCategoryBtnEvent");
				eventDelegate.parameters[0].value = buttonData;
			}
			buttonData.tab_button_obj = this.CreateTypeAndCategoryButton(updateObject, buttonData.name, flag, eventDelegate);
			goto IL_E7;
		}
		updateObject.Update();
		List<Transform> childList = updateObject.ui_grid.GetChildList();
		for (int j = 0; j < childList.Count; j++)
		{
			UIWFTabButton componentInChildren = childList[j].gameObject.GetComponentInChildren<UIWFTabButton>();
			if (!(componentInChildren == null) && componentInChildren.isEnabled)
			{
				updateObject.tab_panel.Select(componentInChildren);
				break;
			}
		}
	}

	private void CreateSkill(FreeSkillSelectOld.ButtonData skill_button_data)
	{
		FreeSkillSelectOld.UpdateObject updateObject = this.update_obj_dic_["スキル"];
		updateObject.MoveStorageRoom(true);
		updateObject.Clear();
		GameObject button_parent = updateObject.button_parent;
		for (int i = 0; i < skill_button_data.children_list.Count; i++)
		{
			FreeSkillSelectOld.ButtonData buttonData = skill_button_data.children_list[i];
			if (buttonData.skill_data != null)
			{
				EventDelegate eventDelegate = new EventDelegate(this, "OnClickSkillBtnEvent");
				eventDelegate.parameters[0].value = buttonData;
				buttonData.tab_button_obj = this.CreateSkillButton(buttonData.skill_data, eventDelegate);
			}
		}
		updateObject.Update();
		List<Transform> childList = updateObject.ui_grid.GetChildList();
		for (int j = 0; j < childList.Count; j++)
		{
			UIWFTabButton componentInChildren = childList[j].gameObject.GetComponentInChildren<UIWFTabButton>();
			if (!(componentInChildren == null) && componentInChildren.isEnabled)
			{
				updateObject.tab_panel.Select(componentInChildren);
				break;
			}
		}
	}

	private void CreateStage(Skill.Old.Data skill_data)
	{
		FreeSkillSelectOld.UpdateObject updateObject = this.update_obj_dic_["ステージ"];
		YotogiOld.Stage stage = this.user_request_stage;
		UIWFTabButton uiwftabButton = null;
		updateObject.tab_panel.ResetSelect();
		List<Transform> childList = updateObject.ui_grid.GetChildList();
		for (int i = 0; i < childList.Count; i++)
		{
			YotogiOldStageUnit component = childList[i].gameObject.GetComponent<YotogiOldStageUnit>();
			UIWFTabButton componentInChildren = childList[i].gameObject.GetComponentInChildren<UIWFTabButton>();
			if (componentInChildren.isSelected)
			{
				componentInChildren.SetSelect(false);
			}
			if (componentInChildren.isEnabled != skill_data.IsExecStage(component.stage_data.stage))
			{
				componentInChildren.isEnabled = skill_data.IsExecStage(component.stage_data.stage);
				if (componentInChildren.isEnabled)
				{
					component.thumbnail_sprite.color = new Color(1f, 1f, 1f, 1f);
				}
				else
				{
					component.thumbnail_sprite.color = new Color(0.6f, 0.6f, 0.6f, 1f);
				}
			}
			if (stage == component.stage_data.stage && componentInChildren.isEnabled)
			{
				uiwftabButton = componentInChildren;
			}
		}
		if (uiwftabButton != null)
		{
			updateObject.tab_panel.Select(uiwftabButton);
		}
		else
		{
			for (int j = 0; j < childList.Count; j++)
			{
				UIWFTabButton componentInChildren2 = childList[j].gameObject.GetComponentInChildren<UIWFTabButton>();
				if (!(componentInChildren2 == null) && componentInChildren2.isEnabled)
				{
					updateObject.tab_panel.Select(componentInChildren2);
					break;
				}
			}
			this.user_request_stage = stage;
		}
	}

	private UIWFTabButton CreateSkillButton(Skill.Old.Data skill_data, EventDelegate onclick_event)
	{
		if (skill_data == null)
		{
			return null;
		}
		FreeSkillSelectOld.UpdateObject updateObject = this.update_obj_dic_["スキル"];
		GameObject gameObject = updateObject.MoveButtonParent();
		if (gameObject == null)
		{
			gameObject = Utility.CreatePrefab(updateObject.button_parent, "SceneYotogi/FreeMode/FreeSkillButton", true);
		}
		UTY.GetChildObject(gameObject, "title", false).GetComponent<UILabel>().text = skill_data.name;
		gameObject.name = skill_data.name;
		UIWFTabButton component = UTY.GetChildObject(gameObject, "BG", false).GetComponent<UIWFTabButton>();
		if (onclick_event != null)
		{
			component.onClick.Add(onclick_event);
		}
		return component;
	}

	private UIWFTabButton CreateTypeAndCategoryButton(FreeSkillSelectOld.UpdateObject update_obj, string name, bool enabled, EventDelegate onclick_event)
	{
		GameObject gameObject = update_obj.MoveButtonParent();
		if (gameObject == null)
		{
			gameObject = Utility.CreatePrefab(update_obj.button_parent, "SceneYotogi/SkillSelect/Prefab/CategoryBtn", true);
		}
		UTY.GetChildObject(gameObject, "Label", false).GetComponent<UILabel>().text = name;
		UIWFTabButton component = UTY.GetChildObject(gameObject, "Button", false).GetComponent<UIWFTabButton>();
		gameObject.name = name;
		component.isEnabled = enabled;
		if (enabled && onclick_event != null)
		{
			component.onClick.Add(onclick_event);
		}
		return component;
	}

	private void OnClickTypeBtnEvent(FreeSkillSelectOld.ButtonData type_btn_data)
	{
		this.update_obj_dic_["タイプ"].UpdateArrow(type_btn_data.tab_button_obj.gameObject);
		this.CreateCategory(type_btn_data);
	}

	private void OnClickCategoryBtnEvent(FreeSkillSelectOld.ButtonData categorybtn_data)
	{
		this.update_obj_dic_["カテゴリー"].UpdateArrow(categorybtn_data.tab_button_obj.gameObject);
		this.CreateSkill(categorybtn_data);
	}

	private void OnClickSkillBtnEvent(FreeSkillSelectOld.ButtonData skill_data)
	{
		this.update_obj_dic_["スキル"].UpdateArrow(skill_data.tab_button_obj.gameObject);
		this.select_skill_ = skill_data.skill_data;
		this.CreateStage(skill_data.skill_data);
	}

	private void OnClickLevelBtnEvent()
	{
		this.update_obj_dic_["レベル"].UpdateArrow(UIButton.current.gameObject);
		this.yotogi_mgr_.fix_skill_level = int.Parse(UIButton.current.gameObject.name);
	}

	private void OnClickStageEvent(YotogiOldStageUnit stage_unit)
	{
		this.select_stage_ = (this.user_request_stage = stage_unit.stage_data.stage);
		this.FixSkill();
	}

	private static List<FreeSkillSelectOld.ButtonData> CreateButtonData(Maid maid)
	{
		List<FreeSkillSelectOld.ButtonData> list = new List<FreeSkillSelectOld.ButtonData>();
		Dictionary<string, KeyValuePair<FreeSkillSelectOld.ButtonData, Dictionary<string, FreeSkillSelectOld.ButtonData>>> type_data_dic = new Dictionary<string, KeyValuePair<FreeSkillSelectOld.ButtonData, Dictionary<string, FreeSkillSelectOld.ButtonData>>>();
		string[] array = new string[]
		{
			"通常",
			"お仕置き",
			"酔い"
		};
		for (int i = 0; i < array.Length; i++)
		{
			FreeSkillSelectOld.ButtonData buttonData = new FreeSkillSelectOld.ButtonData();
			buttonData.children_list = new List<FreeSkillSelectOld.ButtonData>();
			buttonData.name = array[i];
			list.Add(buttonData);
			type_data_dic.Add(array[i], new KeyValuePair<FreeSkillSelectOld.ButtonData, Dictionary<string, FreeSkillSelectOld.ButtonData>>(buttonData, new Dictionary<string, FreeSkillSelectOld.ButtonData>()));
		}
		for (int j = 0; j < list.Count; j++)
		{
			for (int k = 0; k < 9; k++)
			{
				FreeSkillSelectOld.ButtonData buttonData2 = list[j];
				FreeSkillSelectOld.ButtonData buttonData3 = new FreeSkillSelectOld.ButtonData();
				buttonData3.children_list = new List<FreeSkillSelectOld.ButtonData>();
				FreeSkillSelectOld.ButtonData buttonData4 = buttonData3;
				YotogiOld.Category category = (YotogiOld.Category)k;
				buttonData4.name = category.ToString();
				buttonData3.parent = buttonData2;
				buttonData2.children_list.Add(buttonData3);
				type_data_dic[list[j].name].Value.Add(buttonData3.name, buttonData3);
			}
		}
		CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
		int[] keyArray = maid.status.yotogiSkill.oldDatas.GetKeyArray();
		int l = 0;
		while (l < keyArray.Length)
		{
			Skill.Old.Data skillData = YotogiOld.GetSkillData(keyArray[l]);
			if (1 >= skillData.player_num)
			{
				goto IL_1D0;
			}
			int num = 0;
			for (int m = 0; m < characterMgr.GetMaidCount(); m++)
			{
				if (!(characterMgr.GetMaid(m) != null))
				{
					break;
				}
				num++;
			}
			if (num >= skillData.player_num)
			{
				goto IL_1D0;
			}
			IL_283:
			l++;
			continue;
			IL_1D0:
			if (!skillData.IsExecMaidForFreeMode(maid.status))
			{
				goto IL_283;
			}
			string key = "通常";
			if (skillData.drunk_skill)
			{
				key = "酔い";
			}
			else if (skillData.osioki_skill)
			{
				key = "お仕置き";
			}
			FreeSkillSelectOld.ButtonData buttonData5 = type_data_dic[key].Value[skillData.category.ToString()];
			if (buttonData5 != null)
			{
				FreeSkillSelectOld.ButtonData buttonData6 = new FreeSkillSelectOld.ButtonData();
				buttonData6.parent = buttonData5;
				buttonData6.name = skillData.name;
				buttonData6.skill_data = skillData;
				buttonData5.children_list.Add(buttonData6);
				goto IL_283;
			}
			goto IL_283;
		}
		Action<string> action = delegate(string name)
		{
			Dictionary<string, FreeSkillSelectOld.ButtonData> value = type_data_dic[name].Value;
			HashSet<string> hashSet = new HashSet<string>();
			List<FreeSkillSelectOld.ButtonData> list2 = new List<FreeSkillSelectOld.ButtonData>();
			foreach (KeyValuePair<string, FreeSkillSelectOld.ButtonData> keyValuePair in value)
			{
				if (keyValuePair.Value.children_list != null && 1 <= keyValuePair.Value.children_list.Count && !hashSet.Contains(keyValuePair.Key))
				{
					hashSet.Add(keyValuePair.Key);
					list2.Add(keyValuePair.Value);
				}
			}
			type_data_dic[name].Key.children_list = list2;
		};
		action("酔い");
		action("お仕置き");
		return list;
	}

	private static FreeSkillSelectOld.ButtonData GetButtonData(List<FreeSkillSelectOld.ButtonData> check_list, string name)
	{
		FreeSkillSelectOld.ButtonData result = null;
		if (check_list == null || string.IsNullOrEmpty(name))
		{
			return result;
		}
		for (int i = 0; i < check_list.Count; i++)
		{
			if (check_list[i] != null)
			{
				if (!string.IsNullOrEmpty(check_list[i].name) && check_list[i].name == name)
				{
					result = check_list[i];
					break;
				}
			}
		}
		return result;
	}

	public YotogiOld.Stage select_stage
	{
		get
		{
			return this.select_stage_;
		}
	}

	public Skill.Old.Data select_skill
	{
		get
		{
			return this.select_skill_;
		}
	}

	private Maid maid_;

	private List<FreeSkillSelectOld.ButtonData> button_data_list_;

	private Dictionary<string, FreeSkillSelectOld.UpdateObject> update_obj_dic_;

	private YotogiOldManager yotogi_mgr_;

	private Skill.Old.Data select_skill_;

	private YotogiOld.Stage select_stage_;

	private YotogiOld.Stage user_request_stage;

	private class ButtonData
	{
		public UIWFTabButton tab_button_obj;

		public FreeSkillSelectOld.ButtonData parent;

		public List<FreeSkillSelectOld.ButtonData> children_list;

		public string name;

		public Skill.Old.Data skill_data;
	}

	private class UpdateObject
	{
		public void Update()
		{
			this.ui_grid.Reposition();
			this.tab_panel.UpdateChildren();
			this.scroll_view.ResetPosition();
			this.ui_panel.UpdateAnchors();
		}

		public void MoveStorageRoom(bool click_event_clear)
		{
			if (this.storage_obj == null)
			{
				return;
			}
			Transform transform = this.button_parent.transform;
			Transform transform2 = this.storage_obj.transform;
			while (0 < transform.childCount)
			{
				GameObject gameObject = transform.GetChild(0).gameObject;
				UIWFTabButton componentInChildren = gameObject.GetComponentInChildren<UIWFTabButton>();
				if (componentInChildren != null)
				{
					componentInChildren.SetSelect(false);
					if (click_event_clear)
					{
						componentInChildren.onClick.Clear();
					}
				}
				gameObject.transform.SetParent(transform2, false);
				gameObject.transform.localPosition = Vector3.zero;
			}
			transform.DetachChildren();
		}

		public GameObject MoveButtonParent()
		{
			if (this.storage_obj == null)
			{
				return null;
			}
			GameObject gameObject = null;
			Transform transform = this.button_parent.transform;
			Transform transform2 = this.storage_obj.transform;
			if (0 < transform2.childCount)
			{
				gameObject = transform2.GetChild(0).gameObject;
				gameObject.transform.SetParent(transform, false);
			}
			return gameObject;
		}

		public GameObject MoveButtonParent(string object_name)
		{
			if (this.storage_obj == null || string.IsNullOrEmpty(object_name))
			{
				return null;
			}
			GameObject gameObject = null;
			Transform transform = this.button_parent.transform;
			Transform transform2 = this.storage_obj.transform;
			for (int i = 0; i < transform2.childCount; i++)
			{
				if (transform2.GetChild(i).name == object_name)
				{
					gameObject = transform2.GetChild(i).gameObject;
					gameObject.transform.SetParent(transform, false);
					break;
				}
			}
			return gameObject;
		}

		public void UpdateArrow(GameObject target_obj)
		{
			if (this.arrow_obj == null || target_obj == null)
			{
				return;
			}
			Vector3 position = target_obj.transform.TransformPoint(new Vector3(0f, 0f, 0f));
			Vector3 local_pos = this.arrow_obj.transform.parent.InverseTransformPoint(position);
			local_pos.x = this.arrow_obj.transform.localPosition.x;
			local_pos.y += 18f;
			Hashtable args = TweenHash.EaseOutQuint(TweenHash.Type.Position, local_pos, 0.3f);
			iTween.MoveTo(this.arrow_obj, args);
		}

		public void Clear()
		{
			Transform transform = this.button_parent.transform;
			for (int i = 0; i < transform.childCount; i++)
			{
				UnityEngine.Object.Destroy(transform.GetChild(i).gameObject);
			}
			transform.DetachChildren();
		}

		public GameObject button_parent;

		public UIGrid ui_grid;

		public UIWFTabPanel tab_panel;

		public UIScrollView scroll_view;

		public UIPanel ui_panel;

		public GameObject arrow_obj;

		public GameObject storage_obj;
	}
}
