using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class AMAnimationTrack : AMTrack
{
	public override string getTrackType()
	{
		return "Animation";
	}

	public bool setObject(GameObject obj)
	{
		if (this.obj != obj)
		{
			this.obj = obj;
			return true;
		}
		return false;
	}

	public void addKey(int _frame, AnimationClip _clip, WrapMode _wrapMode)
	{
		foreach (AMKey amkey in this.keys)
		{
			AMAnimationKey amanimationKey = (AMAnimationKey)amkey;
			if (amanimationKey.frame == _frame)
			{
				amanimationKey.amClip = _clip;
				amanimationKey.wrapMode = _wrapMode;
				this.updateCache();
				return;
			}
		}
		AMAnimationKey amanimationKey2 = ScriptableObject.CreateInstance<AMAnimationKey>();
		amanimationKey2.frame = _frame;
		amanimationKey2.amClip = _clip;
		amanimationKey2.wrapMode = _wrapMode;
		this.keys.Add(amanimationKey2);
		this.updateCache();
	}

	public override void updateCache()
	{
		base.destroyCache();
		this.cache = new List<AMAction>();
		base.sortKeys();
		for (int i = 0; i < this.keys.Count; i++)
		{
			AMAnimationAction amanimationAction = ScriptableObject.CreateInstance<AMAnimationAction>();
			amanimationAction.startFrame = this.keys[i].frame;
			amanimationAction.obj = this.obj;
			amanimationAction.amClip = (this.keys[i] as AMAnimationKey).amClip;
			amanimationAction.wrapMode = (this.keys[i] as AMAnimationKey).wrapMode;
			amanimationAction.crossfade = (this.keys[i] as AMAnimationKey).crossfade;
			amanimationAction.crossfadeTime = (this.keys[i] as AMAnimationKey).crossfadeTime;
			this.cache.Add(amanimationAction);
		}
		base.updateCache();
	}

	public void previewFrame(float frame, float frameRate)
	{
		if (!this.obj)
		{
			return;
		}
		if (this.cache.Count <= 0)
		{
			return;
		}
		bool flag = false;
		for (int i = this.cache.Count - 1; i >= 0; i--)
		{
			if ((float)this.cache[i].startFrame <= frame)
			{
				AnimationClip amClip = (this.cache[i] as AMAnimationAction).amClip;
				if (amClip)
				{
					amClip.wrapMode = (this.cache[i] as AMAnimationAction).wrapMode;
					amClip.SampleAnimation(this.obj, this.getTime(frameRate, frame - (float)this.cache[i].startFrame));
				}
				flag = true;
				break;
			}
		}
		Animation component = this.obj.GetComponent<Animation>();
		if (!flag && component.clip)
		{
			component.clip.SampleAnimation(this.obj, 0f);
		}
	}

	public float getTime(float frameRate, float numberOfFrames)
	{
		return numberOfFrames / frameRate;
	}

	public override AnimatorTimeline.JSONInit getJSONInit()
	{
		return null;
	}

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

	public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
	{
		List<GameObject> list = new List<GameObject>();
		if (!this.obj)
		{
			return list;
		}
		int i = 0;
		while (i < oldReferences.Count)
		{
			if (oldReferences[i] == this.obj)
			{
				if (!newReferences[i].GetComponent(typeof(Animation)))
				{
					Debug.LogWarning("Animator: Animation Track component 'Animation' not found on new reference for GameObject '" + this.obj.name + "'. Duplicate not replaced.");
					list.Add(oldReferences[i]);
					return list;
				}
				this.obj = newReferences[i];
				break;
			}
			else
			{
				i++;
			}
		}
		return list;
	}

	public GameObject obj;
}
