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

public class YotogiCommandFactory : 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 (YotogiCommandFactory.<>f__mg$cache0 == null)
		{
			YotogiCommandFactory.<>f__mg$cache0 = new Comparison<Transform>(YotogiCommandFactory.SortOriginal);
		}
		uigrid.onCustomSort = YotogiCommandFactory.<>f__mg$cache0;
		this.hidden_tree_transform_ = this.HiddenTree.transform;
	}

	public void UpdateCommand()
	{
		if (this.enabled_callback_func_ != null)
		{
			foreach (KeyValuePair<string, YotogiCommandFactory.Command> keyValuePair in this.command_dic_)
			{
				YotogiCommandFactory.Command value = keyValuePair.Value;
				for (int i = 0; i < value.children.Length; i++)
				{
					if (value.children[i].visible)
					{
						value.children[i].button.isEnabled = this.enabled_callback_func_(value.children[i].data);
						value.children[i].nameLabel.color = ((!value.children[i].button.isEnabled) ? Color.black : value.children[i].data.basic.color);
					}
				}
			}
		}
		if (this.change_color_callbacl_func_ != null)
		{
			foreach (KeyValuePair<string, YotogiCommandFactory.Command> keyValuePair2 in this.command_dic_)
			{
				YotogiCommandFactory.Command value2 = keyValuePair2.Value;
				for (int j = 0; j < value2.children.Length; j++)
				{
					if (value2.children[j].visible && value2.children[j].button.isEnabled)
					{
						Color color = value2.children[j].nameLabel.color;
						this.change_color_callbacl_func_(value2.children[j].data, ref color);
						if (color != value2.children[j].nameLabel.color)
						{
							value2.children[j].nameLabel.color = color;
						}
					}
				}
			}
		}
	}

	public void SetCommandCreateCallBack(YotogiCommandFactory.CommandCreateCallBack func)
	{
		this.command_create_callback_func_ = func;
	}

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

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

	public void SetChangeCommandTextColorCallBack(YotogiCommandFactory.ChangeCommandTextColorCallBack func)
	{
		this.change_color_callbacl_func_ = func;
	}

	public void SetCommandExecConditionTextsCallback(YotogiCommandFactory.CommandExecConditionTextsCallback func)
	{
		this.exec_condition_call_back_ = func;
	}

	public void ReserveSkillCommand(Skill.Data.Command skill_command)
	{
		this.ClearSkillCommand();
		List<KeyValuePair<string, List<Skill.Data.Command.Data>>> list = new List<KeyValuePair<string, List<Skill.Data.Command.Data>>>();
		for (int i = 0; i < skill_command.data.Length; i++)
		{
			string groupName = this.GetGroupName(skill_command.data[i].basic);
			bool flag = true;
			for (int j = 0; j < list.Count; j++)
			{
				if (list[j].Key == groupName)
				{
					list[j].Value.Add(skill_command.data[i]);
					flag = false;
					break;
				}
			}
			if (flag)
			{
				list.Add(new KeyValuePair<string, List<Skill.Data.Command.Data>>(groupName, new List<Skill.Data.Command.Data>()));
				list[list.Count - 1].Value.Add(skill_command.data[i]);
			}
		}
		for (int k = 0; k < list.Count; k++)
		{
			YotogiCommandFactory.Command command = new YotogiCommandFactory.Command();
			command.id = k * 100;
			command.visible = false;
			command.this_object = this.CreateTitle(list[k].Key, command.id);
			command.transform = command.this_object.transform;
			List<YotogiCommandFactory.Command.Children> list2 = new List<YotogiCommandFactory.Command.Children>();
			int num = command.id + 5;
			for (int l = 0; l < list[k].Value.Count; l++)
			{
				if (this.command_create_callback_func_ == null || this.command_create_callback_func_(list[k].Value[l]))
				{
					YotogiCommandFactory.Command.Children children = new YotogiCommandFactory.Command.Children();
					children.data = list[k].Value[l];
					children.id = num;
					children.visible = false;
					children.this_object = this.CreateCommand(this.GetCommandName(children.data.basic), children.id, false);
					children.button = children.this_object.GetComponent<UIButton>();
					children.nameLabel = children.this_object.GetComponentInChildren<UILabel>();
					children.eventCollider = children.this_object.GetComponentInChildren<UIEventTrigger>().GetComponent<BoxCollider>();
					children.transform = children.this_object.transform;
					UIEventTrigger component = children.eventCollider.GetComponent<UIEventTrigger>();
					YotogiCommandFactory.Command.Children data = children;
					EventDelegate.Add(component.onHoverOver, delegate()
					{
						this.OnHoverOver(data);
					});
					EventDelegate.Add(component.onHoverOut, delegate()
					{
						this.OnHoverOut(data);
					});
					EventDelegate.Add(component.onDragStart, delegate()
					{
						this.OnHoverOut(data);
					});
					list2.Add(children);
					num += 5;
				}
			}
			command.children = list2.ToArray();
			this.command_dic_.Add(list[k].Key, command);
		}
	}

	private void OnHoverOver(YotogiCommandFactory.Command.Children data)
	{
	}

	private void OnHoverOut(YotogiCommandFactory.Command.Children data)
	{
	}

	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.Data.Command.Data command_data)
	{
		YotogiCommandFactory.Command command = this.command_dic_[this.GetGroupName(command_data.basic)];
		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].button.isEnabled = true;
				}
				else
				{
					command.children[i].button.isEnabled = this.enabled_callback_func_(command.children[i].data);
				}
				Color color = (!command.children[i].button.isEnabled) ? Color.black : command.children[i].data.basic.color;
				if (this.change_color_callbacl_func_ != null)
				{
					this.change_color_callbacl_func_(command.children[i].data, ref color);
				}
				command.children[i].nameLabel.color = color;
				command.children[i].eventCollider.enabled = !command.children[i].button.isEnabled;
				if (!command.visible)
				{
					command.transform.SetParent(base.transform, false);
					command.visible = true;
				}
				this.grid_update_ = true;
				return;
			}
		}
	}

	public void RemoveCommand(Skill.Data.Command.Data command_data)
	{
		YotogiCommandFactory.Command command = this.command_dic_[this.GetGroupName(command_data.basic)];
		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.Data.Command.Data command_data)
	{
		YotogiCommandFactory.Command command = this.command_dic_[this.GetGroupName(command_data.basic)];
		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, YotogiCommandFactory.Command> keyValuePair in this.command_dic_)
		{
			YotogiCommandFactory.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<Localize>().SetTerm(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<Localize>().SetTerm(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, YotogiCommandFactory.Command> keyValuePair in this.command_dic_)
		{
			YotogiCommandFactory.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;
				}
			}
		}
	}

	private string GetGroupName(Skill.Data.Command.Data.Basic commandDataBasic)
	{
		return commandDataBasic.termGroupName;
	}

	private string GetCommandName(Skill.Data.Command.Data.Basic commandDataBasic)
	{
		return commandDataBasic.termName;
	}

	public GameObject HiddenTree;

	private UIGrid grid_;

	private Transform hidden_tree_transform_;

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

	private YotogiCommandFactory.CommandEnabledCallBack enabled_callback_func_;

	private YotogiCommandFactory.ChangeCommandTextColorCallBack change_color_callbacl_func_;

	private YotogiCommandFactory.CommandCreateCallBack command_create_callback_func_;

	private YotogiCommandFactory.CommandCallback callback_func_;

	private YotogiCommandFactory.CommandExecConditionTextsCallback exec_condition_call_back_;

	private bool grid_update_;

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

	public delegate bool CommandCreateCallBack(Skill.Data.Command.Data data);

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

	public delegate void CommandExecConditionTextsCallback(GameObject commandObj, Skill.Data.Command.Data data, bool visible);

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

	public delegate void ChangeCommandTextColorCallBack(Skill.Data.Command.Data data, ref Color color);

	private class CommandBase
	{
		public Transform transform;

		public GameObject this_object;

		public int id;

		public bool visible;
	}

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

		public class Children : YotogiCommandFactory.CommandBase
		{
			public Skill.Data.Command.Data data;

			public UIButton button;

			public UILabel nameLabel;

			public BoxCollider eventCollider;
		}
	}
}
