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

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

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

        protected override void Lerp(ValueType fromValue, ValueType toValue, float normalizedTime)
        {
            mTexts.ForEach(text =>
            {
                text.color = new Color
                (
                    text.color.r,
                    text.color.g,
                    text.color.b,
                    Mathf.Lerp((float)fromValue, (float)toValue, normalizedTime)
                );
                text.color2 = new Color
                (
                    text.color2.r,
                    text.color2.g,
                    text.color2.b,
                    Mathf.Lerp((float)fromValue, (float)toValue, normalizedTime)
                );
                text.Commit();
            });
        }

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