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

[RequireComponent(typeof(Animation))]
[RequireComponent(typeof(Animator))]
public class MaidIdleMotionAdditiveHook : MonoBehaviour
{
	public bool isAdditive
	{
		get
		{
			return this.isAdditive_;
		}
		set
		{
			this.isAdditive_ = value;
			if (this.isAdditive_)
			{
				if (this.StartAdditive(this.targetClip))
				{
					this.maidAnimation.enabled = false;
					this.animator.enabled = true;
				}
				else
				{
					this.isAdditive_ = false;
				}
			}
			else
			{
				this.animator.enabled = false;
				if (!this.maidAnimation.enabled)
				{
					this.maidAnimation.enabled = true;
					bool flag = false;
					IEnumerator enumerator = this.maidAnimation.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							object obj = enumerator.Current;
							AnimationState animationState = (AnimationState)obj;
							if (this.targetClip != null)
							{
								if (animationState != null && animationState.clip != this.targetClip)
								{
									animationState.time = 0f;
									flag = true;
									break;
								}
							}
							else if (animationState != null && animationState.clip != null && this.maidAnimation.IsPlaying(animationState.clip.name))
							{
								animationState.time = 0f;
								flag = true;
								break;
							}
						}
					}
					finally
					{
						IDisposable disposable;
						if ((disposable = (enumerator as IDisposable)) != null)
						{
							disposable.Dispose();
						}
					}
					if (flag)
					{
						this.maidAnimation.Sample();
					}
				}
			}
		}
	}

	public float additiveWeight
	{
		get
		{
			return this.animator.GetLayerWeight(1);
		}
		set
		{
			this.animator.SetLayerWeight(1, value);
		}
	}

	private void Awake()
	{
		this.maidAnimation = base.GetComponent<Animation>();
		this.animator = base.GetComponent<Animator>();
		if (this.animator == null)
		{
			this.animator = base.gameObject.AddComponent<Animator>();
		}
		this.animator.enabled = false;
		this.animator.runtimeAnimatorController = UnityEngine.Object.Instantiate<RuntimeAnimatorController>(Resources.Load<RuntimeAnimatorController>("ScenePhotoMode/Animation/MaidIdleAdditive"));
		this.overrideController = new AnimatorOverrideController(this.animator.runtimeAnimatorController);
		List<KeyValuePair<AnimationClip, AnimationClip>> list = new List<KeyValuePair<AnimationClip, AnimationClip>>(this.overrideController.overridesCount);
		this.overrideController.GetOverrides(list);
		list[0] = new KeyValuePair<AnimationClip, AnimationClip>(list[0].Key, null);
		this.overrideController.ApplyOverrides(list);
		this.animator.runtimeAnimatorController = this.overrideController;
	}

	private bool StartAdditive(AnimationClip clip = null)
	{
		if (clip == null)
		{
			IEnumerator enumerator = this.maidAnimation.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					AnimationState animationState = (AnimationState)obj;
					if (!(animationState == null) && !(animationState.clip == null))
					{
						if (this.maidAnimation.IsPlaying(animationState.clip.name))
						{
							clip = animationState.clip;
							break;
						}
					}
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
			if (clip == null)
			{
				return false;
			}
		}
		AnimationClip animationClip = this.instanceClip;
		AnimationClip animationClip2 = UnityEngine.Object.Instantiate<AnimationClip>(clip);
		animationClip2.legacy = false;
		this.instanceClip = animationClip2;
		List<KeyValuePair<AnimationClip, AnimationClip>> list = new List<KeyValuePair<AnimationClip, AnimationClip>>(this.overrideController.overridesCount);
		this.overrideController.GetOverrides(list);
		list[0] = new KeyValuePair<AnimationClip, AnimationClip>(list[0].Key, animationClip2);
		this.overrideController.ApplyOverrides(list);
		this.animator.CrossFade(this.animator.GetCurrentAnimatorStateInfo(0).fullPathHash, 0f);
		if (animationClip != null)
		{
			UnityEngine.Object.DestroyImmediate(animationClip);
		}
		return true;
	}

	private void OnDestroy()
	{
		UnityEngine.Object.Destroy(this);
		if (!this.notAnimatorDestroy && this.animator != null)
		{
			this.animator.enabled = false;
			UnityEngine.Object.Destroy(this.animator);
			this.animator = null;
		}
		if (this.instanceClip != null)
		{
			UnityEngine.Object.Destroy(this.instanceClip);
			this.instanceClip = null;
		}
	}

	private const string IdleAdditiveAnimatorControllerPath = "ScenePhotoMode/Animation/MaidIdleAdditive";

	public AnimationClip targetClip;

	public bool notAnimatorDestroy;

	private bool isAdditive_;

	private Animator animator;

	private Animation maidAnimation;

	private AnimatorOverrideController overrideController;

	private AnimationClip instanceClip;
}
