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

public class YotogiOldCommandFactory : MonoBehaviour
{
	public static int SortOriginal(Transform a, Transform b)
	{
		return a.localPosition.z.CompareTo(b.localPosition.z);
	}

	private void Awake()
	{
		this.grid_ = base.gameObject.GetComponent<UIGrid>();
		UIGrid uigrid = this.grid_;
		if (YotogiOldCommandFactory.<>f__mg$cache0 == null)
		{
			YotogiOldCommandFactory.<>f__mg$cache0 = new Comparison<Transform>(YotogiOldCommandFactory.SortOriginal);
		}
		uigrid.onCustomSort = YotogiOldCommandFactory.<>f__mg$cache0;
		this.hidden_tree_transform_ = this.HiddenTree.transform;
	}

	public void UpdateCommandEnabled()
	{
		if (this.enabled_callback_func_ == null)
		{
			return;
		}
		foreach (KeyValuePair<string, YotogiOldCommandFactory.Command> keyValuePair in this.command_dic_)
		{
			YotogiOldCommandFactory.Command value = keyValuePair.Value;
			for (int i = 0; i < value.children.Length; i++)
			{
				if (value.children[i].visible)
				{
					value.children[i].this_object.GetComponent<UIButton>().isEnabled = this.enabled_callback_func_(value.children[i].data);
				}
			}
		}
	}

	public void SetCommandCallback(YotogiOldCommandFactory.CommandCallback func)
	{
		this.callback_func_ = func;
	}

	public void SetCommandEnabledCallBack(YotogiOldCommandFactory.CommandEnabledCallBack func)
	{
		this.enabled_callback_func_ = func;
	}

	public void ReserveSkillCommand(Skill.Old.Data.Command skill_command)
	{
		this.ClearSkillCommand();
		List<KeyValuePair<string, List<Skill.Old.Data.Command.Data>>> list = new List<KeyValuePair<string, List<Skill.Old.Data.Command.Data>>>();
		for (int i = 0; i < skill_command.data.Length; i++)
		{
			string group_name = skill_command.data[i].basic.group_name;
			bool flag = true;
			for (int j = 0; j < list.Count; j++)
			{
				if (list[j].Key == group_name)
				{
					list[j].Value.Add(skill_command.data[i]);
					flag = false;
					break;
				}
			}
			if (flag)
			{
				list.Add(new KeyValuePair<string, List<Skill.Old.Data.Command.Data>>(group_name, new List<Skill.Old.Data.Command.Data>()));
				list[list.Count - 1].Value.Add(skill_command.data[i]);
			}
		}
		for (int k = 0; k < list.Count; k++)
		{
			YotogiOldCommandFactory.Command command = new YotogiOldCommandFactory.Command();
			command.id = k * 100;
			command.visible = false;
			command.this_object = this.CreateTitle(list[k].Key, command.id);
			command.transform = command.this_object.transform;
			command.children = new YotogiOldCommandFactory.Command.Children[list[k].Value.Count];
			int num = command.id + 5;
			for (int l = 0; l < list[k].Value.Count; l++)
			{
				YotogiOldCommandFactory.Command.Children children = new YotogiOldCommandFactory.Command.Children();
				children.data = list[k].Value[l];
				children.id = num;
				children.visible = false;
				children.this_object = this.CreateCommand(children.data.basic.name, children.id, children.data.basic.active_command);
				children.transform = children.this_object.transform;
				command.children[l] = children;
				num += 5;
			}
			this.command_dic_.Add(list[k].Key, command);
		}
	}

	public void ClearSkillCommand()
	{
		this.ClearCommand();
		for (int i = 0; i < this.hidden_tree_transform_.childCount; i++)
		{
			UnityEngine.Object.Destroy(this.hidden_tree_transform_.GetChild(i).gameObject);
		}
		this.hidden_tree_transform_.DetachChildren();
		this.command_dic_.Clear();
	}

	public void AddCommand(Skill.Old.Data.Command.Data command_data)
	{
		YotogiOldCommandFactory.Command command = this.command_dic_[command_data.basic.group_name];
		for (int i = 0; i < command.children.Length; i++)
		{
			if (command.children[i].data == command_data)
			{
				command.children[i].transform.SetParent(base.transform, false);
				command.children[i].visible = true;
				if (this.enabled_callback_func_ == null)
				{
					command.children[i].this_object.GetComponent<UIButton>().isEnabled = true;
				}
				else
				{
					command.children[i].this_object.GetComponent<UIButton>().isEnabled = this.enabled_callback_func_(command.children[i].data);
				}
				if (!command.visible)
				{
					command.transform.SetParent(base.transform, false);
					command.visible = true;
				}
				this.grid_update_ = true;
				return;
			}
		}
	}

	public void RemoveCommand(Skill.Old.Data.Command.Data command_data)
	{
		YotogiOldCommandFactory.Command command = this.command_dic_[command_data.basic.group_name];
		for (int i = 0; i < command.children.Length; i++)
		{
			if (command.children[i].data == command_data)
			{
				command.children[i].transform.SetParent(this.hidden_tree_transform_, false);
				command.children[i].visible = false;
				bool flag = true;
				for (int j = 0; j < command.children.Length; j++)
				{
					if (command.children[j].visible)
					{
						flag = false;
						break;
					}
				}
				if (flag && command.visible)
				{
					command.transform.SetParent(this.hidden_tree_transform_, false);
					command.visible = false;
				}
				this.grid_update_ = true;
				return;
			}
		}
	}

	public GameObject GetCommandGameObject(Skill.Old.Data.Command.Data command_data)
	{
		YotogiOldCommandFactory.Command command = this.command_dic_[command_data.basic.group_name];
		for (int i = 0; i < command.children.Length; i++)
		{
			if (command.children[i].data == command_data)
			{
				return command.children[i].this_object;
			}
		}
		return null;
	}

	public void ClearCommand()
	{
		foreach (KeyValuePair<string, YotogiOldCommandFactory.Command> keyValuePair in this.command_dic_)
		{
			YotogiOldCommandFactory.Command value = keyValuePair.Value;
			if (value.visible)
			{
				value.transform.SetParent(this.hidden_tree_transform_, false);
				value.visible = false;
			}
			for (int i = 0; i < value.children.Length; i++)
			{
				if (value.children[i].visible)
				{
					value.children[i].transform.SetParent(this.hidden_tree_transform_, false);
					value.children[i].visible = false;
				}
			}
		}
		this.grid_update_ = true;
	}

	public void LateUpdate()
	{
		if (this.grid_update_)
		{
			this.grid_.Reposition();
			this.grid_update_ = false;
		}
	}

	private GameObject CreateTitle(string name, int id)
	{
		if (name == null)
		{
			name = string.Empty;
		}
		GameObject gameObject = Utility.CreatePrefab(this.HiddenTree, "SceneYotogi/Yotogi/Prefab/CommandCategoryTitle", true);
		gameObject.GetComponent<UILabel>().text = name;
		gameObject.name = "-" + name;
		Vector3 localPosition = gameObject.transform.localPosition;
		localPosition.z = (float)id;
		gameObject.transform.localPosition = localPosition;
		return gameObject;
	}

	private GameObject CreateCommand(string name, int id, bool is_active)
	{
		if (name == null)
		{
			name = string.Empty;
		}
		GameObject gameObject = Utility.CreatePrefab(this.HiddenTree, "SceneYotogi/Yotogi/Prefab/Command", true);
		UTY.GetChildObject(gameObject, "Name", false).GetComponent<UILabel>().text = name;
		gameObject.name = "cm:" + name;
		Vector3 localPosition = gameObject.transform.localPosition;
		localPosition.z = (float)id;
		gameObject.transform.localPosition = localPosition;
		EventDelegate eventDelegate = new EventDelegate(this, "OnClick");
		EventDelegate.Parameter[] parameters = eventDelegate.parameters;
		parameters[0].obj = gameObject;
		EventDelegate.Add(gameObject.GetComponent<UIButton>().onClick, eventDelegate);
		if (is_active)
		{
			UTY.GetChildObject(gameObject, "Name", false).GetComponent<UILabel>().color = Color.red;
		}
		return gameObject;
	}

	private void OnClick(GameObject obj)
	{
		if (this.callback_func_ == null)
		{
			return;
		}
		int num = (int)obj.transform.localPosition.z;
		foreach (KeyValuePair<string, YotogiOldCommandFactory.Command> keyValuePair in this.command_dic_)
		{
			YotogiOldCommandFactory.Command value = keyValuePair.Value;
			for (int i = 0; i < value.children.Length; i++)
			{
				if (value.children[i].id == num)
				{
					this.callback_func_(value.children[i].data);
					return;
				}
			}
		}
	}

	public GameObject HiddenTree;

	private UIGrid grid_;

	private Transform hidden_tree_transform_;

	private Dictionary<string, YotogiOldCommandFactory.Command> command_dic_ = new Dictionary<string, YotogiOldCommandFactory.Command>();

	private YotogiOldCommandFactory.CommandEnabledCallBack enabled_callback_func_;

	private YotogiOldCommandFactory.CommandCallback callback_func_;

	private bool grid_update_;

	[CompilerGenerated]
	private static Comparison<Transform> <>f__mg$cache0;

	public delegate void CommandCallback(Skill.Old.Data.Command.Data data);

	public delegate bool CommandEnabledCallBack(Skill.Old.Data.Command.Data data);

	private class CommandBase
	{
		public Transform transform;

		public GameObject this_object;

		public int id;

		public bool visible;
	}

	private class Command : YotogiOldCommandFactory.CommandBase
	{
		public YotogiOldCommandFactory.Command.Children[] children;

		public class Children : YotogiOldCommandFactory.CommandBase
		{
			public Skill.Old.Data.Command.Data data;
		}
	}
}
