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

[Serializable]
public class AMRotationTrack : AMTrack
{
	public Transform obj
	{
		get
		{
			return this._obj;
		}
		set
		{
			if (value != null && this.cache.Count <= 0)
			{
				this.cachedInitialRotation = value.rotation;
			}
			this._obj = value;
			if (this._obj != null)
			{
				this.objName = this._obj.name;
			}
			else
			{
				this.objName = string.Empty;
			}
		}
	}

	public override string getTrackType()
	{
		return "Rotation";
	}

	public void addKey(int _frame, Quaternion _rotation)
	{
		foreach (AMKey amkey in this.keys)
		{
			AMRotationKey amrotationKey = (AMRotationKey)amkey;
			if (amrotationKey.frame == _frame)
			{
				amrotationKey.rotation = _rotation;
				this.updateCache();
				return;
			}
		}
		AMRotationKey amrotationKey2 = ScriptableObject.CreateInstance<AMRotationKey>();
		amrotationKey2.frame = _frame;
		amrotationKey2.rotation = _rotation;
		amrotationKey2.easeType = 21;
		this.keys.Add(amrotationKey2);
		this.updateCache();
	}

	public bool changeObject(Transform _obj)
	{
		this.obj = _obj;
		this.updateCache();
		return true;
	}

	public override void updateCache()
	{
		if (this._obj != null)
		{
			this.objName = this._obj.name;
		}
		else
		{
			this.objName = string.Empty;
		}
		base.sortKeys();
		base.destroyCache();
		this.cache = new List<AMAction>();
		for (int i = 0; i < this.keys.Count; i++)
		{
			AMRotationAction amrotationAction = ScriptableObject.CreateInstance<AMRotationAction>();
			amrotationAction.startFrame = this.keys[i].frame;
			if (this.keys.Count > i + 1)
			{
				amrotationAction.endFrame = this.keys[i + 1].frame;
			}
			else
			{
				amrotationAction.endFrame = -1;
			}
			amrotationAction.obj = this.obj;
			amrotationAction.startRotation = (this.keys[i] as AMRotationKey).rotation;
			if (amrotationAction.endFrame != -1)
			{
				amrotationAction.endRotation = (this.keys[i + 1] as AMRotationKey).rotation;
			}
			amrotationAction.easeType = (this.keys[i] as AMRotationKey).easeType;
			amrotationAction.customEase = new List<float>(this.keys[i].customEase);
			this.cache.Add(amrotationAction);
		}
		base.updateCache();
	}

	public override void previewFrame(float frame, AMTrack extraTrack = null)
	{
		if (!this.obj)
		{
			return;
		}
		if (string.IsNullOrEmpty(this.objName))
		{
			this.objName = this.obj.name;
		}
		if (this.cache.Count <= 0)
		{
			return;
		}
		if (this.cache[0] == null)
		{
			this.updateCache();
		}
		if (frame <= (float)this.cache[0].startFrame || (this.cache[0] as AMRotationAction).endFrame == -1)
		{
			this.obj.rotation = (this.cache[0] as AMRotationAction).getStartQuaternion();
			return;
		}
		if (frame >= (float)(this.cache[this.cache.Count - 2] as AMRotationAction).endFrame)
		{
			this.obj.rotation = (this.cache[this.cache.Count - 2] as AMRotationAction).getEndQuaternion();
			return;
		}
		foreach (AMAction amaction in this.cache)
		{
			AMRotationAction amrotationAction = (AMRotationAction)amaction;
			if (frame >= (float)amrotationAction.startFrame && frame <= (float)amrotationAction.endFrame)
			{
				if (frame == (float)amrotationAction.startFrame)
				{
					this.obj.rotation = amrotationAction.getStartQuaternion();
					break;
				}
				if (frame == (float)amrotationAction.endFrame)
				{
					this.obj.rotation = amrotationAction.getEndQuaternion();
					break;
				}
				AnimationCurve curve = null;
				AMTween.EasingFunction easingFunction;
				if (amrotationAction.hasCustomEase())
				{
					if (AMRotationTrack.<>f__mg$cache0 == null)
					{
						AMRotationTrack.<>f__mg$cache0 = new AMTween.EasingFunction(AMTween.customEase);
					}
					easingFunction = AMRotationTrack.<>f__mg$cache0;
					curve = amrotationAction.easeCurve;
				}
				else
				{
					easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amrotationAction.easeType);
				}
				float num = frame - (float)amrotationAction.startFrame;
				if (num < 0f)
				{
					num = 0f;
				}
				float value = num / (float)amrotationAction.getNumberOfFrames();
				Quaternion startQuaternion = amrotationAction.getStartQuaternion();
				Quaternion endQuaternion = amrotationAction.getEndQuaternion();
				Quaternion rotation = default(Quaternion);
				rotation.x = easingFunction(startQuaternion.x, endQuaternion.x, value, curve);
				rotation.y = easingFunction(startQuaternion.y, endQuaternion.y, value, curve);
				rotation.z = easingFunction(startQuaternion.z, endQuaternion.z, value, curve);
				rotation.w = easingFunction(startQuaternion.w, endQuaternion.w, value, curve);
				this.obj.rotation = rotation;
				break;
			}
		}
	}

	public bool autoKey(Transform _obj, int frame)
	{
		if (!this.obj)
		{
			return false;
		}
		if (this.obj != _obj)
		{
			return false;
		}
		if (this.cache.Count <= 0)
		{
			if (_obj.rotation != this.cachedInitialRotation)
			{
				this.addKey(frame, _obj.rotation);
				return true;
			}
			return false;
		}
		else
		{
			Quaternion rotationAtFrame = this.getRotationAtFrame((float)frame);
			if (_obj.rotation != rotationAtFrame)
			{
				this.addKey(frame, _obj.rotation);
				return true;
			}
			return false;
		}
	}

	public Quaternion getRotationAtFrame(float frame)
	{
		if (frame <= (float)this.cache[0].startFrame || (this.cache[0] as AMRotationAction).endFrame == -1)
		{
			return (this.cache[0] as AMRotationAction).getStartQuaternion();
		}
		if (frame >= (float)(this.cache[this.cache.Count - 2] as AMRotationAction).endFrame)
		{
			return (this.cache[this.cache.Count - 2] as AMRotationAction).getEndQuaternion();
		}
		foreach (AMAction amaction in this.cache)
		{
			AMRotationAction amrotationAction = (AMRotationAction)amaction;
			if (frame >= (float)amrotationAction.startFrame && frame <= (float)amrotationAction.endFrame)
			{
				if (frame == (float)amrotationAction.startFrame)
				{
					return amrotationAction.getStartQuaternion();
				}
				if (frame == (float)amrotationAction.endFrame)
				{
					return amrotationAction.getEndQuaternion();
				}
				AnimationCurve curve = null;
				AMTween.EasingFunction easingFunction;
				if (amrotationAction.hasCustomEase())
				{
					if (AMRotationTrack.<>f__mg$cache1 == null)
					{
						AMRotationTrack.<>f__mg$cache1 = new AMTween.EasingFunction(AMTween.customEase);
					}
					easingFunction = AMRotationTrack.<>f__mg$cache1;
					curve = amrotationAction.easeCurve;
				}
				else
				{
					easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amrotationAction.easeType);
				}
				float num = frame - (float)amrotationAction.startFrame;
				if (num < 0f)
				{
					num = 0f;
				}
				float value = num / (float)amrotationAction.getNumberOfFrames();
				Quaternion startQuaternion = amrotationAction.getStartQuaternion();
				Quaternion endQuaternion = amrotationAction.getEndQuaternion();
				return new Quaternion
				{
					x = easingFunction(startQuaternion.x, endQuaternion.x, value, curve),
					y = easingFunction(startQuaternion.y, endQuaternion.y, value, curve),
					z = easingFunction(startQuaternion.z, endQuaternion.z, value, curve),
					w = easingFunction(startQuaternion.w, endQuaternion.w, value, curve)
				};
			}
		}
		Debug.LogError(string.Concat(new object[]
		{
			"Animator: Could not get ",
			this.obj.name,
			" rotation at frame '",
			frame,
			"'"
		}));
		return new Quaternion(0f, 0f, 0f, 0f);
	}

	public Vector4 getInitialRotation()
	{
		return (this.keys[0] as AMRotationKey).getRotationQuaternion();
	}

	public override AnimatorTimeline.JSONInit getJSONInit()
	{
		if (!this.obj || this.keys.Count <= 0)
		{
			return null;
		}
		AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
		jsoninit.type = "rotation";
		jsoninit.go = this.obj.gameObject.name;
		AnimatorTimeline.JSONQuaternion jsonquaternion = new AnimatorTimeline.JSONQuaternion();
		jsonquaternion.setValue(this.getInitialRotation());
		jsoninit.rotation = jsonquaternion;
		return jsoninit;
	}

	public override List<GameObject> getDependencies()
	{
		List<GameObject> list = new List<GameObject>();
		if (this.obj)
		{
			list.Add(this.obj.gameObject);
		}
		return list;
	}

	public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
	{
		if (!this.obj)
		{
			return new List<GameObject>();
		}
		for (int i = 0; i < oldReferences.Count; i++)
		{
			if (oldReferences[i] == this.obj.gameObject)
			{
				this.obj = newReferences[i].transform;
				break;
			}
		}
		return new List<GameObject>();
	}

	[SerializeField]
	private Transform _obj;

	public string objName;

	public Quaternion cachedInitialRotation;

	[CompilerGenerated]
	private static AMTween.EasingFunction <>f__mg$cache0;

	[CompilerGenerated]
	private static AMTween.EasingFunction <>f__mg$cache1;
}
