using UnityEngine;
using System.Collections;

public class LayerSliceEffect_Fade : LayerSliceEffect
{
    private const float TIME = 0.75f;
    private float mElapsedTime;
    private tk2dBaseSprite[] mSprites;
    private tk2dTextMesh[] mTexts;
    private Color[] mInitialSpriteColors, mFinalSpriteColors, mInitialTextColors, mFinalTextColors;
    private Color[] mOriginalInitialSpriteColors, mOriginalFinalSpriteColors, mOriginalInitialTextColors, mOriginalFinalTextColors;

    protected override void Awake()
    {
        mSprites = GetComponentsInChildren<tk2dBaseSprite>();
        mInitialSpriteColors = new Color[mSprites.Length];
        mFinalSpriteColors = new Color[mSprites.Length];
        mOriginalInitialSpriteColors = new Color[mSprites.Length];
        mOriginalFinalSpriteColors = new Color[mSprites.Length];

        mTexts = GetComponentsInChildren<tk2dTextMesh>();
        mInitialTextColors = new Color[mTexts.Length];
        mFinalTextColors = new Color[mTexts.Length];
        mOriginalInitialTextColors = new Color[mTexts.Length];
        mOriginalFinalTextColors = new Color[mTexts.Length];

        for (int i = 0; i < mSprites.Length; i++)
        {
            mOriginalInitialSpriteColors[i] = new Color(mSprites[i].color.r, mSprites[i].color.g, mSprites[i].color.b, mSprites[i].color.a);
            mOriginalFinalSpriteColors[i] = new Color(mSprites[i].color.r, mSprites[i].color.g, mSprites[i].color.b, mSprites[i].color.a);
        }
        for (int i = 0; i < mTexts.Length; i++)
        {
            mOriginalInitialTextColors[i] = new Color(mTexts[i].color.r, mTexts[i].color.g, mTexts[i].color.b, mTexts[i].color.a);
            mOriginalFinalTextColors[i] = new Color(mTexts[i].color.r, mTexts[i].color.g, mTexts[i].color.b, mTexts[i].color.a);
        }
    }

    public override void Init(bool removing) 
    {
        mElapsedTime = 0.0f;
        float initialAlpha = removing ? 1.0f : 0.0f;
        for (int i = 0; i < mSprites.Length; i++)
        {
            mInitialSpriteColors[i] = new Color
            (
                mOriginalInitialSpriteColors[i].r, 
                mOriginalInitialSpriteColors[i].g, 
                mOriginalInitialSpriteColors[i].b, 
                mOriginalInitialSpriteColors[i].a * initialAlpha
            );
            mFinalSpriteColors[i] = new Color
            (
                mOriginalFinalSpriteColors[i].r,
                mOriginalFinalSpriteColors[i].g,
                mOriginalFinalSpriteColors[i].b,
                mOriginalFinalSpriteColors[i].a * (1.0f - initialAlpha)
            );
        }
        for (int i = 0; i < mTexts.Length; i++)
        {
            mInitialTextColors[i] = new Color
            (
                mOriginalInitialTextColors[i].r,
                mOriginalInitialTextColors[i].g,
                mOriginalInitialTextColors[i].b,
                mOriginalInitialTextColors[i].a * initialAlpha
            );
            mFinalTextColors[i] = new Color
            (
                mOriginalFinalTextColors[i].r,
                mOriginalFinalTextColors[i].g,
                mOriginalFinalTextColors[i].b,
                mOriginalFinalTextColors[i].a * (1.0f - initialAlpha)
            );
        }
        SetColors(0.0f);
    }

    public override bool CustomUpdate(float deltaTime)
    {
        mElapsedTime += deltaTime;
        if (mElapsedTime >= TIME)
        {
            SetColors(1.0f);
            return true;
        }
        else
        {
            float normalizedElapsedTime = mElapsedTime / TIME;
            SetColors(normalizedElapsedTime);
            return false;
        }
    }

    private void SetColors(float normalizedElapsedTime)
    {
        for (int i = 0; i < mSprites.Length; i++)
        {
            mSprites[i].color = Color.Lerp(mInitialSpriteColors[i], mFinalSpriteColors[i], normalizedElapsedTime);
        }
        for (int i = 0; i < mTexts.Length; i++)
        {
            mTexts[i].color = Color.Lerp(mInitialTextColors[i], mFinalTextColors[i], normalizedElapsedTime);
            mTexts[i].Commit();
        }
    }
}