// by chai
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace TweenAnimation
{
    public class TweenController : MonoBehaviour
    {
        public bool autoPlayOnEnable;
        public string defaultAnimation;
        public List<TweenAnimation> animations;

        private string m_Current;
        private TweenAnimation m_CurrentAnimation;

        private float m_StartTime;

        public void OnEnable()
        {
            if(autoPlayOnEnable)
            {
                PlayAnimation(defaultAnimation);
            }
        }

        public void PlayAnimation(string name)
        {
            m_Current = name;
            m_CurrentAnimation = null;
            for (int i = 0; i < animations.Count; ++i)
            {
                if(animations[i].name == name)
                {
                    m_CurrentAnimation = animations[i];
                    break;
                }
            }
            if(m_CurrentAnimation == null)
            {
                Debug.LogError("No such animation'" + name + "'");
            }
            else
            {
                m_CurrentAnimation.BeforePlay();
            }
            m_StartTime = Time.time;
        }

        private void Update()
        {
            if (m_CurrentAnimation == null)
                return;
            float t = Time.time;
            m_CurrentAnimation.Process(t - m_StartTime);
        }

    }

}