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

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

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

	public void addKey(int _frame, Vector3 _position, int _interp, int _easeType)
	{
		foreach (AMKey amkey in this.keys)
		{
			AMTranslationKey amtranslationKey = (AMTranslationKey)amkey;
			if (amtranslationKey.frame == _frame)
			{
				amtranslationKey.position = _position;
				amtranslationKey.interp = _interp;
				amtranslationKey.easeType = _easeType;
				this.updateCache();
				return;
			}
		}
		AMTranslationKey amtranslationKey2 = ScriptableObject.CreateInstance<AMTranslationKey>();
		amtranslationKey2.frame = _frame;
		amtranslationKey2.position = _position;
		amtranslationKey2.interp = _interp;
		amtranslationKey2.easeType = _easeType;
		this.keys.Add(amtranslationKey2);
		this.updateCache();
	}

	public void addKey(int _frame, Vector3 _position)
	{
		foreach (AMKey amkey in this.keys)
		{
			AMTranslationKey amtranslationKey = (AMTranslationKey)amkey;
			if (amtranslationKey.frame == _frame)
			{
				amtranslationKey.position = _position;
				this.updateCache();
				return;
			}
		}
		AMTranslationKey amtranslationKey2 = ScriptableObject.CreateInstance<AMTranslationKey>();
		amtranslationKey2.frame = _frame;
		amtranslationKey2.position = _position;
		this.keys.Add(amtranslationKey2);
		this.updateCache();
	}

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

	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 (frame <= (float)this.cache[0].startFrame)
		{
			this.obj.position = (this.cache[0] as AMTranslationAction).path[0];
			return;
		}
		if (frame >= (float)(this.cache[this.cache.Count - 1] as AMTranslationAction).endFrame)
		{
			this.obj.position = (this.cache[this.cache.Count - 1] as AMTranslationAction).path[(this.cache[this.cache.Count - 1] as AMTranslationAction).path.Length - 1];
			return;
		}
		foreach (AMAction amaction in this.cache)
		{
			AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
			if (frame >= (float)amtranslationAction.startFrame && frame <= (float)amtranslationAction.endFrame)
			{
				if (amtranslationAction.path.Length == 1)
				{
					this.obj.position = amtranslationAction.path[0];
					break;
				}
				float num = frame - (float)amtranslationAction.startFrame;
				if (num < 0f)
				{
					num = 0f;
				}
				AnimationCurve curve = null;
				AMTween.EasingFunction easingFunction;
				if (amtranslationAction.hasCustomEase())
				{
					if (AMTranslationTrack.<>f__mg$cache0 == null)
					{
						AMTranslationTrack.<>f__mg$cache0 = new AMTween.EasingFunction(AMTween.customEase);
					}
					easingFunction = AMTranslationTrack.<>f__mg$cache0;
					curve = amtranslationAction.easeCurve;
				}
				else
				{
					easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amtranslationAction.easeType);
				}
				float value = easingFunction(0f, 1f, num / (float)amtranslationAction.getNumberOfFrames(), curve);
				AMTween.PutOnPath(this.obj, amtranslationAction.path, Mathf.Clamp(value, 0f, 1f));
				break;
			}
		}
	}

	public bool autoKey(Transform _obj, int frame)
	{
		if (!this.obj)
		{
			return false;
		}
		if (_obj != this.obj)
		{
			return false;
		}
		if (this.cache.Count <= 0)
		{
			if (_obj.position != this.cachedInitialPosition)
			{
				this.addKey(frame, _obj.position);
				return true;
			}
			return false;
		}
		else
		{
			Vector3 positionAtFrame = this.getPositionAtFrame((float)frame);
			if (_obj.position != positionAtFrame)
			{
				this.addKey(frame, _obj.position);
				return true;
			}
			return false;
		}
	}

	public Vector3 getPositionAtFrame(float frame)
	{
		if (this.cache.Count <= 0)
		{
			return this.obj.position;
		}
		if (frame <= (float)this.cache[0].startFrame)
		{
			return (this.cache[0] as AMTranslationAction).path[0];
		}
		if (frame >= (float)(this.cache[this.cache.Count - 1] as AMTranslationAction).endFrame)
		{
			return (this.cache[this.cache.Count - 1] as AMTranslationAction).path[(this.cache[this.cache.Count - 1] as AMTranslationAction).path.Length - 1];
		}
		foreach (AMAction amaction in this.cache)
		{
			AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
			if ((int)frame >= amtranslationAction.startFrame && (int)frame <= amtranslationAction.endFrame)
			{
				if (amtranslationAction.path.Length == 1)
				{
					return amtranslationAction.path[0];
				}
				AnimationCurve curve = null;
				AMTween.EasingFunction easingFunction;
				if (amtranslationAction.hasCustomEase())
				{
					if (AMTranslationTrack.<>f__mg$cache1 == null)
					{
						AMTranslationTrack.<>f__mg$cache1 = new AMTween.EasingFunction(AMTween.customEase);
					}
					easingFunction = AMTranslationTrack.<>f__mg$cache1;
					curve = amtranslationAction.easeCurve;
				}
				else
				{
					easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amtranslationAction.easeType);
				}
				float num = frame - (float)amtranslationAction.startFrame;
				if (num < 0f)
				{
					num = 0f;
				}
				return AMTween.PointOnPath(amtranslationAction.path, Mathf.Clamp(easingFunction(0f, 1f, num / (float)amtranslationAction.getNumberOfFrames(), curve), 0f, 1f));
			}
		}
		Debug.LogError(string.Concat(new object[]
		{
			"Animator: Could not get ",
			this.obj.name,
			" position at frame '",
			frame,
			"'"
		}));
		return new Vector3(0f, 0f, 0f);
	}

	public override void drawGizmos(float gizmo_size)
	{
		foreach (AMAction amaction in this.cache)
		{
			AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
			if (amtranslationAction.path.Length > 1)
			{
				AMTween.DrawPath(amtranslationAction.path, new Color(255f, 255f, 255f, 0.5f));
				Gizmos.color = Color.green;
				Gizmos.DrawSphere(amtranslationAction.path[0], gizmo_size);
				Gizmos.DrawSphere(amtranslationAction.path[amtranslationAction.path.Length - 1], gizmo_size);
			}
		}
	}

	private AMPath getPathFromIndex(int startIndex)
	{
		List<Vector3> list = new List<Vector3>();
		int endIndex = startIndex;
		int frame = this.keys[startIndex].frame;
		int frame2 = this.keys[startIndex].frame;
		list.Add((this.keys[startIndex] as AMTranslationKey).position);
		for (int i = startIndex + 1; i < this.keys.Count; i++)
		{
			list.Add((this.keys[i] as AMTranslationKey).position);
			frame2 = this.keys[i].frame;
			endIndex = i;
			if ((this.keys[i] as AMTranslationKey).interp == 1)
			{
				break;
			}
		}
		return new AMPath(list.ToArray(), (this.keys[startIndex] as AMTranslationKey).interp, frame, frame2, startIndex, endIndex);
	}

	public override void updateCache()
	{
		if (this._obj != null)
		{
			this.objName = this._obj.name;
		}
		else
		{
			this.objName = string.Empty;
		}
		base.destroyCache();
		this.cache = new List<AMAction>();
		base.sortKeys();
		for (int i = 0; i < this.keys.Count; i++)
		{
			AMPath pathFromIndex = this.getPathFromIndex(i);
			AMTranslationAction amtranslationAction = ScriptableObject.CreateInstance<AMTranslationAction>();
			amtranslationAction.startFrame = pathFromIndex.startFrame;
			amtranslationAction.endFrame = pathFromIndex.endFrame;
			amtranslationAction.obj = this.obj;
			amtranslationAction.path = pathFromIndex.path;
			amtranslationAction.easeType = (this.keys[i] as AMTranslationKey).easeType;
			amtranslationAction.customEase = new List<float>(this.keys[i].customEase);
			this.cache.Add(amtranslationAction);
			if (i < this.keys.Count - 1)
			{
				i = pathFromIndex.endIndex - 1;
			}
		}
		foreach (AMTrack amtrack in this.parentTake.trackValues)
		{
			if (amtrack is AMOrientationTrack && ((amtrack as AMOrientationTrack).obj == this.obj || (amtrack as AMOrientationTrack).hasTarget(this.obj)))
			{
				amtrack.updateCache();
			}
		}
		base.updateCache();
	}

	public AMTranslationKey getActionStartKeyFor(int frame)
	{
		foreach (AMAction amaction in this.cache)
		{
			AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
			if (frame >= amtranslationAction.startFrame && frame < amtranslationAction.endFrame)
			{
				return (AMTranslationKey)base.getKeyOnFrame(amtranslationAction.startFrame);
			}
		}
		Debug.LogError("Animator: Action for frame " + frame + " does not exist in cache.");
		return new AMTranslationKey();
	}

	public Vector3 getInitialPosition()
	{
		return (this.keys[0] as AMTranslationKey).position;
	}

	public override AnimatorTimeline.JSONInit getJSONInit()
	{
		if (!this.obj || this.keys.Count <= 0)
		{
			return null;
		}
		AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
		jsoninit.type = "position";
		jsoninit.go = this.obj.gameObject.name;
		AnimatorTimeline.JSONVector3 jsonvector = new AnimatorTimeline.JSONVector3();
		jsonvector.setValue(this.getInitialPosition());
		jsoninit.position = jsonvector;
		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 Vector3 cachedInitialPosition;

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

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