using System;
using UnityEngine;

namespace mgaia.Utils.Animations
{
    public class GameobjectLocalRotationAnimation : AbstractCodeAnimation
    {
        private GameObject[] mGameobjects;

        public GameobjectLocalRotationAnimation(float[] keyTimes, ValueType[] keyValues, ValueType initialValue, bool loop) : base(keyTimes, keyValues, initialValue, loop) { }

        protected override void Lerp(ValueType fromValue, ValueType toValue, float normalizedTime)
        {
            Quaternion convertedFromValue = (Quaternion)fromValue;
            Quaternion convertedToValue = (Quaternion)toValue;
            Quaternion targetValue = Quaternion.Lerp(convertedFromValue, convertedToValue, normalizedTime);

            if (!float.IsNaN(targetValue.x) && !float.IsNaN(targetValue.y) && !float.IsNaN(targetValue.z))
            {
                for (int i = 0; i < mGameobjects.Length; i++)
                {
                    mGameobjects[i].transform.localRotation = targetValue;
                }
            }
        }

        protected override void OnActivateAnimation(object[] objs)
        {
            mGameobjects = objs.Select(obj => obj as GameObject).ToArray();
        }
    }
}