using UnityEngine;
using System.Collections;

namespace mgaia.Utils.LayerSystem
{
    [RequireComponent(typeof(ITouchable))]
    public abstract class AbstractColorEffect : ITouchableEffects
    {
        private const float DARKENING = 100.0f / 255.0f;
        private Color[] INITIAL_COLOR_TEXTS, INITIAL_COLOR_SPRITES, FINAL_COLOR_TEXTS, FINAL_COLOR_SPRITES;
        private IColorContainer[] mSpriteContainers;
        private IColorContainer[] mTextContainers;

        protected abstract Color GetFinalTextColor(Color initialColor);
        protected abstract Color GetFinalSpriteColor(Color initialColor);
        protected abstract IColorContainer[] GetSpriteContainers();
        protected abstract IColorContainer[] GetTextContainers();

        private void Start()
        {
            mSpriteContainers = GetSpriteContainers();
            mTextContainers = GetTextContainers();
        }

        private void CreateColorVectors()
        {
            INITIAL_COLOR_TEXTS = new Color[mTextContainers.Length];
            FINAL_COLOR_TEXTS = new Color[mTextContainers.Length];
            for (int i = 0; i < mTextContainers.Length; i++)
            {
                FINAL_COLOR_TEXTS[i] = GetFinalTextColor(INITIAL_COLOR_TEXTS[i] = mTextContainers[i].Color);
            }

            INITIAL_COLOR_SPRITES = new Color[mSpriteContainers.Length];
            FINAL_COLOR_SPRITES = new Color[mSpriteContainers.Length];
            for (int i = 0; i < mSpriteContainers.Length; i++)
            {
                FINAL_COLOR_SPRITES[i] = GetFinalSpriteColor(INITIAL_COLOR_SPRITES[i] = mSpriteContainers[i].Color);
            }
        }

        public override void OnTouchBegan()
        {
            CreateColorVectors();
            for (int i = 0; i < mTextContainers.Length; i++)
            {
                mTextContainers[i].Color = FINAL_COLOR_TEXTS[i];
            }
            for (int i = 0; i < mSpriteContainers.Length; i++)
            {
                mSpriteContainers[i].Color = FINAL_COLOR_SPRITES[i];
            }
        }

        public override void OnTouchEnded()
        {
            for (int i = 0; i < mTextContainers.Length; i++)
            {
                mTextContainers[i].Color = INITIAL_COLOR_TEXTS[i];
            }
            for (int i = 0; i < mSpriteContainers.Length; i++)
            {
                mSpriteContainers[i].Color = INITIAL_COLOR_SPRITES[i];
            }
        }
    }
}