using UnityEngine;
using System.Collections;

[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 Start()
    {
        mCollidersList = GetComponents<Collider>();
        /*Util.AssertTrue(mCollidersList.TrueForAll(colider =>
        {
            return colider.bounds.center.x.Aproximate(0.0f) &&
                    colider.bounds.center.y.Aproximate(0.0f) &&
                    colider.bounds.center.z.Aproximate(0.0f);
        }), " Resizable ITouchables must have their colliders centered at (0, 0, 0) ");*/
        
        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 
            {
                Util.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;
            }
        }
    }
}