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

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

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

        protected override void Lerp(ValueType fromValue, ValueType toValue, float normalizedTime)
        {
            Vector3 convertedFromValue = (Vector3)fromValue;
            Vector3 convertedToValue = (Vector3)toValue;
            Vector3 targetValue = Vector3.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.position = targetValue;
                }
            }
        }

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