using UnityEngine;
using System.Collections;

public class ShapeInfo : MonoBehaviour {

    public int ShapeTypeID = 0;
    public Vector3 visualDestinationPosition = Vector3.zero;

    public GameObject ExplosionObject = null;

    public bool FlaggedForExplosion = false; // Set to true when this piece should self destruct
    public int MatchCount = 0; // How many in a row I am a match of

    private float verticalVelocityForGravity = 0.0f;
    private bool _fallingToGravity = false;
    public bool fallingToGravity
    {
        get
        {
            return _fallingToGravity;
        }
        set
        {
            _fallingToGravity = value;
        }
    }

    private bool _beingDragged;
    public bool beingDragged
    {
        get
        {
            return _beingDragged;
        }
        set
        {
            _beingDragged = value;
            if (value == false)
            {
                gameObject.transform.localScale = Vector3.one;
            }
            else
            {
            }
        }
    }

    public void BeginFallingToGravity(float initialVelocity)
    {
        fallingToGravity = true;
        verticalVelocityForGravity = initialVelocity;
    }

    // Use this for initialization
    void Start () {
        myRandomSeed = Random.value;
	}

    private float myRandomSeed = 0.0f;

	// Update is called once per frame
	void FixedUpdate () {

        Vector3 toTarget = visualDestinationPosition - gameObject.transform.position;

        if (_fallingToGravity)
        {
            // If we're falling to gravity then we use a different update method than if we're being dragged
            if (toTarget.y > verticalVelocityForGravity)
            {
                // We have arrived, stop falling
                gameObject.transform.position = visualDestinationPosition;
                _fallingToGravity = false;

                GameManager.Instance.ClickSounds.Play();
            }
            else
            {
                Vector3 gravityVector = new Vector3(0.0f, verticalVelocityForGravity, 0.0f);
                gameObject.transform.position += gravityVector;
                verticalVelocityForGravity += GameSettings.Gravity;

            }
        }
        else
        {
            if (toTarget.magnitude < 0.2f)
            {
                gameObject.transform.position = visualDestinationPosition;
            }
            else
            {
                gameObject.transform.position += toTarget * 0.3f;
            }
        }


        // If we are part of a match (in the event the player releases the mouse) then we should rotate gently
        if (MatchCount > 2)
        {
            // 90 BPM music with double the time period for going back and forth.
            float timePeriod = 0.5f;
            float NormalizedTime = Mathf.PingPong(myRandomSeed + Time.time, timePeriod) / timePeriod;
            float rotation = Mathf.Lerp(-45.0f, 45.0f, NormalizedTime);
            gameObject.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, rotation);
        }
        else
        {
            gameObject.transform.localRotation = Quaternion.identity;

        }


        // If we are part of the elite group that is being dragged then we pulse gently
        // TODO Pulse with the music
        float scale = 1.0f;
        if (beingDragged)
        {
            scale *= 1.1f;
        }
        else
        {
            scale *= Mathf.Lerp(0.95f, 1.05f, Mathf.PingPong(Time.time + myRandomSeed, 1.0f));
        }
        if (MatchCount > 2)
        {
            scale *= Random.Range(1.0f, 1.2f);
        }
        gameObject.transform.localScale = new Vector3(scale, scale, 1.0f);


        if (FlaggedForExplosion)
        {
            if (ExplosionObject!= null)
            {
                GameObject.Instantiate(ExplosionObject, transform.position, Quaternion.identity);
            }
            GameObject.Destroy(gameObject);
        }
	}
}
