using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

[Serializable]
public class AMEventKey : AMKey
{
	public MethodInfo methodInfo
	{
		get
		{
			if (this.component == null)
			{
				return null;
			}
			if (this.cachedMethodInfo != null)
			{
				return this.cachedMethodInfo;
			}
			if (this.methodName == null)
			{
				return null;
			}
			try
			{
				this.cachedMethodInfo = this.component.GetType().GetMethod(this.methodName);
			}
			catch
			{
				this.cachedMethodInfo = null;
				Debug.LogError("MonoBehaviorでなければいけません。");
			}
			return this.cachedMethodInfo;
		}
		set
		{
			if (value != null)
			{
				this.methodName = value.Name;
			}
			else
			{
				this.methodName = null;
			}
			this.cachedMethodInfo = value;
		}
	}

	public bool setMethodInfo(Component component, MethodInfo methodInfo, ParameterInfo[] cachedParameterInfos)
	{
		if (this.methodInfo != methodInfo || this.component != component)
		{
			this.component = component;
			this.methodInfo = methodInfo;
			this.destroyParameters();
			this.parameters = new List<AMEventParameter>();
			for (int i = 0; i < cachedParameterInfos.Length; i++)
			{
				AMEventParameter ameventParameter = ScriptableObject.CreateInstance<AMEventParameter>();
				ameventParameter.setValueType(cachedParameterInfos[i].ParameterType);
				this.parameters.Add(ameventParameter);
			}
			return true;
		}
		return false;
	}

	public bool setUseSendMessage(bool useSendMessage)
	{
		if (this.useSendMessage != useSendMessage)
		{
			this.useSendMessage = useSendMessage;
			return true;
		}
		return false;
	}

	public void destroyParameters()
	{
		if (this.parameters == null)
		{
			return;
		}
		foreach (AMEventParameter ameventParameter in this.parameters)
		{
			ameventParameter.destroy();
		}
	}

	public override void destroy()
	{
		this.destroyParameters();
		base.destroy();
	}

	public List<GameObject> getDependencies()
	{
		List<GameObject> list = new List<GameObject>();
		foreach (AMEventParameter ameventParameter in this.parameters)
		{
			list = list.Union(ameventParameter.getDependencies()).ToList<GameObject>();
		}
		return list;
	}

	public bool updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences, bool didUpdateObj, GameObject obj)
	{
		if (didUpdateObj && this.component)
		{
			string name = this.component.GetType().Name;
			this.component = obj.GetComponent(name);
			if (!this.component)
			{
				Debug.LogError(string.Concat(new string[]
				{
					"Animator: Component '",
					name,
					"' not found on new reference for GameObject '",
					obj.name,
					"'. Some event track data may be lost."
				}));
			}
			this.cachedMethodInfo = null;
		}
		bool flag = false;
		foreach (AMEventParameter ameventParameter in this.parameters)
		{
			if (ameventParameter.updateDependencies(newReferences, oldReferences) && !flag)
			{
				flag = true;
			}
		}
		return flag;
	}

	public override AMKey CreateClone()
	{
		AMEventKey ameventKey = ScriptableObject.CreateInstance<AMEventKey>();
		ameventKey.frame = this.frame;
		ameventKey.component = this.component;
		ameventKey.useSendMessage = this.useSendMessage;
		ameventKey.methodName = this.methodName;
		ameventKey.methodInfo = this.methodInfo;
		foreach (AMEventParameter ameventParameter in this.parameters)
		{
			ameventKey.parameters.Add(ameventParameter.CreateClone());
		}
		return ameventKey;
	}

	public override void CreateFromStringData(string data_text)
	{
		string[] array = data_text.Split(new char[]
		{
			':'
		});
		if (array.Length == 0 || array[0] != "Event")
		{
			return;
		}
		NDebug.Assert(this.obj != null, "AMEventKey::CreateFromStringData\nobjがnullです");
		int num = 1;
		this.frame = int.Parse(array[num++]);
		this.useSendMessage = bool.Parse(array[num++]);
		string str = array[num++];
		this.methodName = array[num++];
		this.component = this.obj.GetComponent<DanceMain>();
		NDebug.Assert(this.component != null, "AMEventKey::CreateFromStringData\n" + str + "が見つかりませんでした");
		this.methodInfo = this.component.GetType().GetMethod(this.methodName);
		this.parameters.Clear();
		int num2 = int.Parse(array[num++]);
		this.parameters = new List<AMEventParameter>();
		for (int i = 0; i < num2; i++)
		{
			this.parameters.Add(AMEventParameter.CreateFromStringData(array[num++]));
		}
	}

	public override string ToStringData()
	{
		List<string> list = new List<string>();
		list.Add("Event");
		list.Add(this.frame.ToString());
		list.Add(this.useSendMessage.ToString());
		list.Add(this.component.name);
		list.Add(this.methodName.ToString());
		list.Add(this.parameters.Count.ToString());
		for (int i = 0; i < this.parameters.Count; i++)
		{
			list.Add(this.parameters[i].ToStringData());
		}
		string text = list[0];
		for (int j = 1; j < list.Count; j++)
		{
			text = text + ":" + list[j];
		}
		return text;
	}

	public GameObject obj;

	public Component component;

	public bool useSendMessage = true;

	public List<AMEventParameter> parameters = new List<AMEventParameter>();

	public string methodName;

	private MethodInfo cachedMethodInfo;
}
