﻿/* Copyright (C) Itseez3D, Inc. - All Rights Reserved
* You may not use this file except in compliance with an authorized license
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* UNLESS REQUIRED BY APPLICABLE LAW OR AGREED BY ITSEEZ3D, INC. IN WRITING, SOFTWARE DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED
* See the License for the specific language governing permissions and limitations under the License.
* Written by Itseez3D, Inc. <support@avatarsdk.com>, April 2017
*/

using System;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

namespace ItSeez3D.AvatarSdkSamples.Core
{
	/// <summary>
	/// Helper class to deal with head blendshape animations.
	/// </summary>
	public class AnimationManager : MonoBehaviour
	{
		#region UI

		public Text currentAnimationText;

		public RuntimeAnimatorController animatorController;

		public string[] animations;

		#endregion

		// animations-related data
		protected Animator animator = null;
		protected int currentAnimationIdx = 0;

		public void CreateAnimator (GameObject obj)
		{
			ChangeCurrentAnimation(0);

			animator = obj.AddComponent<Animator>();
			animator.applyRootMotion = true;
			animator.runtimeAnimatorController = animatorController;
		}

		public void OnPrevAnimation ()
		{
			ChangeCurrentAnimation (-1);
		}

		public void OnNextAnimation ()
		{
			ChangeCurrentAnimation (+1);
		}

		public void PlayCurrentAnimation ()
		{
			if (animator != null)
			{
				animator.Play(animations[currentAnimationIdx]);
			}
		}

		public void PlayAnimationByName(string animationName)
		{
			int animationIdx = animations.ToList().IndexOf(animationName);
			if (animationIdx < 0)
			{
				Debug.LogErrorFormat("There is no animation with name: {0}", animationName);
				return;
			}

			if (animationIdx != currentAnimationIdx)
			{
				currentAnimationIdx = animationIdx;
				PlayCurrentAnimation();
			}
		}

		private void ChangeCurrentAnimation(int delta)
		{
			var newIdx = currentAnimationIdx + delta;
			if (newIdx < 0)
				newIdx = animations.Length - 1;
			if (newIdx >= animations.Length)
				newIdx = 0;

			currentAnimationIdx = newIdx;
			if (currentAnimationText != null)
				currentAnimationText.text = animations[currentAnimationIdx].Replace('_', ' ');

			PlayCurrentAnimation();
		}
	}
}

