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

namespace mgaia.Utils.LayerSystem
{
    /// <summary>
    /// Efeito de mudança de escala dos elementos de primeiro nível da Layer
    /// </summary>
    /// <owner>Victor</owner>
    public class LayerSliceEffectScale : LayerSliceEffect
    {
        [SerializeField]
        private TimeAndScale[] _timeAndScales;

        [SerializeField]
        private bool _scaleAll = true;

        private GameobjectRelativeScaleAnimation mAnimation;

        protected override void CustomInit(bool removing)
        {
            float[] times = _timeAndScales.Select(timeAndScale => timeAndScale.Time).ToArray();

            IEnumerable<ValueType> scalesEnumerable = _timeAndScales.Select(timeAndScale => (ValueType)timeAndScale.Scale);
            ValueType[] scales;
            if (removing)
            {
                scales = scalesEnumerable.Reverse().ToArray();
            }
            else
            {
                scales = scalesEnumerable.ToArray();
            }

            mAnimation = new GameobjectRelativeScaleAnimation(times, scales, scales[0], false);

            object[] objectsToScale = default(object[]);

            if (_scaleAll)
            {
                objectsToScale = transform.GetChildren().Select(lTransform => lTransform.gameObject).ToArray();
            }
            else
            {
                objectsToScale = new[] { gameObject };
            }

            mAnimation.ActivateAnimation(objectsToScale);
        }

        protected override bool CustomUpdate(float deltaTime)
        {
            return mAnimation.Update(deltaTime);
        }
    }
}