using UnityEngine;
using System.Collections;

public class ShapeComponent : MonoBehaviour
{
    private PlayerController myPlayer;
    private bool bActive;
    private BoxCollider2D myCollision;
    private TM_CombatComponent myCC;

    public TM_Weapon airWeapon;
    public TM_Weapon groundWeapon;

    private Vector3 vCoreOffset;

    public Vector3 vPositionAdjustment;

    void Awake()
    {
        myPlayer = GameObject.Find("Player").GetComponent<PlayerController>();
        myCollision = GetComponent<BoxCollider2D>();
        myCC = GetComponent<TM_CombatComponent>();
    }

	// Use this for initialization
	void Start () 
    {
        if( myCC != null )  
    	    myCC.AddOnHitFunction(OnHitByProjectile);
	}
	
	// Update is called once per frame
	void Update ()
	{
	    transform.localPosition = vCoreOffset + vPositionAdjustment;
	    vPositionAdjustment = Vector3.zero;
	}

    public bool OnHitByProjectile(TM_Projectile p)
    {
        if (p.fDamage <= 0)
            return false;

        if (bActive)
        {
            myPlayer.OnBlockedEnemyShot();
        }
        return true;
    }

    public void EnableSquareShieldBlockingAction()
    {
        if (myCollision != null)
            myCollision.enabled = true;
        bActive = true;
    }

    public void DisableSquareShieldBlockingAction()
    {
        if( myCollision != null )
            myCollision.enabled = false;
        bActive = false;
    }

    public void MoveToNewTransformOffset(Vector3 vNewPosition)
    {
        Vector3 vStartPosition = transform.localPosition;
        StartCoroutine(MoveToNewPositionCoroutine(vStartPosition, vNewPosition, 0.5f));
    }

    IEnumerator MoveToNewPositionCoroutine( Vector3 vStartPosition, Vector3 vNewPosition, float fTransTime)
    {
        float fTime = 0;

        Vector3 vKickDir = Random.onUnitSphere;
        vKickDir.z = transform.localPosition.z;
        Vector3 vFakePosition = vStartPosition;


        while (fTime < fTransTime)
        {
            vFakePosition += vKickDir*3.0f*Time.deltaTime;
            Vector3 vNewPos = Vector3.Lerp(vFakePosition, vNewPosition, fTime / fTransTime);
            transform.localPosition = vNewPos;
            fTime += Time.deltaTime;
            yield return null;
        }

        transform.localPosition = vNewPosition;
        vCoreOffset = vNewPosition;
    }

    public void LaunchIntoSpace()
    {
        DisableSquareShieldBlockingAction();
        StartCoroutine(LaunchIntoSpace_Coroutine());
        Destroy(gameObject,1.0f);
    }

    IEnumerator LaunchIntoSpace_Coroutine()
    {
        bActive = false;
        Vector3 vKickDir = Random.onUnitSphere;
        vKickDir.z = 0;
        vKickDir.Normalize();

        while (true)
        {
            transform.position += vKickDir*8.0f*Time.deltaTime;
            Vector3 vRot = transform.rotation.eulerAngles;
            vRot.z += 720.0f*Time.deltaTime;
            transform.rotation = Quaternion.Euler(vRot);

            yield return null;
        }


    }
}
