using UnityEngine;
using System.Collections;

namespace mgaia.Utils.LayerSystem
{
    [RequireComponent(typeof(ITouchable))]
    public class ResizingEffect : ITouchableEffects
    {
        private const float SCALE = 0.95f;
        private Vector3 INITIAL_SIZE, FINAL_SIZE;
        private Vector3[] INITIAL_COLLIDER_SIZE, FINAL_COLLIDER_SIZE;
        private Collider[] mCollidersList;

        void Awake()
        {
            mCollidersList = GetComponents<Collider>();

            INITIAL_SIZE = transform.localScale;
            FINAL_SIZE = INITIAL_SIZE * SCALE;
            INITIAL_COLLIDER_SIZE = new Vector3[mCollidersList.Length];
            FINAL_COLLIDER_SIZE = new Vector3[mCollidersList.Length];
            for (int i = 0; i < mCollidersList.Length; i++)
            {
                if (mCollidersList[i] is BoxCollider)
                {
                    INITIAL_COLLIDER_SIZE[i] = (mCollidersList[i] as BoxCollider).size;
                }
                else if (mCollidersList[i] is SphereCollider)
                {
                    INITIAL_COLLIDER_SIZE[i].x = (mCollidersList[i] as SphereCollider).radius;
                }
                else if (mCollidersList[i] is CapsuleCollider)
                {
                    INITIAL_COLLIDER_SIZE[i].x = (mCollidersList[i] as CapsuleCollider).radius;
                    INITIAL_COLLIDER_SIZE[i].y = (mCollidersList[i] as CapsuleCollider).height;
                }
                else
                {
                    Assert.AssertTrue(false, "Unimplemented collider type: " + mCollidersList[i].GetType());
                }
                FINAL_COLLIDER_SIZE[i] = INITIAL_COLLIDER_SIZE[i] * 1.0f / SCALE;
            }
        }

        public override void OnTouchBegan()
        {
            for (int i = 0; i < mCollidersList.Length; i++)
            {
                if (mCollidersList[i] is BoxCollider)
                {
                    (mCollidersList[i] as BoxCollider).size = FINAL_COLLIDER_SIZE[i];
                }
                else if (mCollidersList[i] is SphereCollider)
                {
                    (mCollidersList[i] as SphereCollider).radius = FINAL_COLLIDER_SIZE[i].x;
                }
                else if (mCollidersList[i] is CapsuleCollider)
                {
                    (mCollidersList[i] as CapsuleCollider).radius = FINAL_COLLIDER_SIZE[i].x;
                    (mCollidersList[i] as CapsuleCollider).height = FINAL_COLLIDER_SIZE[i].y;
                }
            }
            transform.localScale = FINAL_SIZE;
        }

        public override void OnTouchEnded()
        {
            transform.localScale = INITIAL_SIZE;
            for (int i = 0; i < mCollidersList.Length; i++)
            {
                if (mCollidersList[i] is BoxCollider)
                {
                    (mCollidersList[i] as BoxCollider).size = INITIAL_COLLIDER_SIZE[i];
                }
                else if (mCollidersList[i] is SphereCollider)
                {
                    (mCollidersList[i] as SphereCollider).radius = INITIAL_COLLIDER_SIZE[i].x;
                }
                else if (mCollidersList[i] is CapsuleCollider)
                {
                    (mCollidersList[i] as CapsuleCollider).radius = INITIAL_COLLIDER_SIZE[i].x;
                    (mCollidersList[i] as CapsuleCollider).height = INITIAL_COLLIDER_SIZE[i].y;
                }
            }
        }
    }
}