using UnityEngine;
using System.Collections;

namespace mgaia.Utils.LayerSystem
{
	/// <summary>
	/// Monobehaviour que representa um grupo de elementos de uma layer (IInputReceivable) que entrara e saira da tela usando um mesmo efeito visual.
	/// Deve ser adicionado a um Gameobject filho do Gameobject raiz layer (o que possuir a camera e o behaviour IInputReceivable).
	/// </summary>
    public class LayerSlice : MonoBehaviour
    {
        public int InsertionOrder = 1;
        private LayerSliceEffect mEffect = null, mFadeEffect = null, mMovementEffect = null, mEffectiveEffect = null;

        void Awake()
        {
            Assert.AssertTrue(InsertionOrder >= 1, "InsertionOrder must be equal to or higher than 1");
            Assert.AssertTrue(transform.parent.GetComponent<IInputReceivable>() != null, "LayerSlice objects must be sons of IInputReceivables");

            if (GetComponents<LayerSliceEffect>().Length == 2)
            {
                mMovementEffect = GetComponent<LayerSliceEffectMovement>();
                mFadeEffect = GetComponent<BaseLayerSliceEffectFade>();
            }
            else
            {
                mEffect = GetComponent<LayerSliceEffect>();
            }
        }

        public void Init(bool removing, bool unstacking)
        {
            if (mEffect != null)
            {
                mEffectiveEffect = mEffect;
                mEffect.Init(removing);
            }
            else if (mMovementEffect != null && mFadeEffect != null)
            {
                if (removing)
                {
                    if (unstacking)
                    {
                        mEffectiveEffect = mMovementEffect;
                        mMovementEffect.Init(removing);
                    }
                    else
                    {
                        mEffectiveEffect = mFadeEffect;
                        mFadeEffect.Init(removing);
                    }
                }
                else
                {
                    if (unstacking)
                    {
                        mEffectiveEffect = mFadeEffect;
                        mFadeEffect.Init(removing);
                    }
                    else
                    {
                        mEffectiveEffect = mMovementEffect;
                        mMovementEffect.Init(removing);
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>True if the effect ended, false otherwise</returns>
        public bool UpdateSlice(float deltaTime)
        {
            if (mEffectiveEffect == null)
            {
                return true;
            }
            return mEffectiveEffect.UpdateEffect(deltaTime);
        }

        public void OnLayerPositionSet()
        {
            if (mEffect != null)
            {
                mEffect.OnLayerPositionSet();
            }
            else if (mMovementEffect != null && mFadeEffect != null)
            {
                mMovementEffect.OnLayerPositionSet();
                mFadeEffect.OnLayerPositionSet();
            }
        }
    }
}