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

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

        public GameobjectAbsoluteScaleAnimation(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;
            for (int i = 0; i < mGameobjects.Length; i++)
            {
                float xTargetValue = Mathf.Lerp(convertedFromValue.x, convertedToValue.x, normalizedTime);
                float yTargetValue = Mathf.Lerp(convertedFromValue.y, convertedToValue.y, normalizedTime);
                float zTargetValue = Mathf.Lerp(convertedFromValue.z, convertedToValue.z, normalizedTime);
                if (!float.IsNaN(xTargetValue) && !float.IsNaN(yTargetValue) && !float.IsNaN(zTargetValue))
                {
                    mGameobjects[i].transform.localScale = new Vector3
                    (
                        xTargetValue,
                        yTargetValue,
                        zTargetValue
                    );
                }
            }
        }

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