using UnityEngine;
using System;
using System.Collections.Generic;
using mgaia.Utils;

namespace mgaia.tk2d.Animations
{
    public class Tk2dTextScaleAnimation : AbstractCodeAnimation
    {
        private tk2dTextMesh[] mTexts;
        private Vector3[] mBaseScales;

        public Tk2dTextScaleAnimation(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 < mTexts.Length; i++)
            {
                mTexts[i].scale = new Vector3
                (
                    Mathf.Lerp(convertedFromValue.x * mBaseScales[i].x, convertedToValue.x * mBaseScales[i].x, normalizedTime),
                    Mathf.Lerp(convertedFromValue.y * mBaseScales[i].y, convertedToValue.y * mBaseScales[i].y, normalizedTime),
                    Mathf.Lerp(convertedFromValue.z * mBaseScales[i].z, convertedToValue.z * mBaseScales[i].z, normalizedTime)
                );
                mTexts[i].Commit();
            }
        }

        protected override void OnActivateAnimation(object[] objs)
        {
            mTexts = objs.Select(obj => obj as tk2dTextMesh).ToArray();
            mBaseScales = mTexts.Select(text => text.scale).ToArray();
        }
    }
}