using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LDPickupProjectile : TM_Projectile
{

    private bool bIsHeadingTowardActor;
    private LDGameActor collectionActor;
    public LDGameActor myGameActor;
    private GameManager theGame;
    public TM_SproggiwoodCoinMotionComponent sprog;
    public LDProjectileMovementComponent myMovementComponent;
    private Vector3 vOldGamePosition;
    private System.Action<LDPickupProjectile> actionOnCollect;
    public int EnergyAwarded;
    public float HealthAwarded;


	// Use this for initialization
	void Start () 
    {
		theGame = GameManager.GetGame();
	}
	
    public override void UpdateMovement()
    {
        //If I've been collected, continue my forced move towards the player
        if( bIsHeadingTowardActor )
        {
            sprog.EndPosition = collectionActor.vWorldPos;
            sprog.EndPosition.y += collectionActor.fDistanceFromGround;

            Vector2 vNewPos = sprog.GetCurrentPosition();
            myMovementComponent.SetForcedMovement(vOldGamePosition, vNewPos );
            vOldGamePosition = vNewPos;
            if (sprog.ArrivedAtGoal())
            {
                DoImpact(transform.position);
            }
        }
        else if( myMovementComponent != null ) //just drift off and have a time
        {
            myMovementComponent.CalculateMoveThisFrame();
        }
    }


    public override void OnTriggerEnter2D(Collider2D hitObject)
    {
        OnHitObject(hitObject.gameObject);
    }

    public override void OnTriggerStay2D(Collider2D hitObject)
    {
        OnHitObject(hitObject.gameObject);
    }

    protected override bool OnHitObject(GameObject hitObject)
    {
        if( bIsHeadingTowardActor )
        {
            //whatever
        }
        else if( hitObject == theGame.thePlayer.gameObject )
        {
            bIsHeadingTowardActor = true;
            vOldGamePosition = myGameActor.vWorldPos;
            collectionActor = hitObject.GetComponent<LDGameActor>();
            actionOnCollect = collectionActor.OnCollectPickup;
            //do a thing to fly toward the player
            sprog.Launch(vOldGamePosition, collectionActor.GetPickupCollectionPosition(), vDirection, fSpeed * 2.0f, 0.5f);
        }

        return true;
    }

    public void ForceBeginCollection(LDGameActor tower, System.Action<LDPickupProjectile> collectAction)
    {
        bIsHeadingTowardActor = true;
        vOldGamePosition = myGameActor.GetPickupCollectionPosition();
        collectionActor = tower;
        actionOnCollect = collectAction;

        //Fly quickly toward the tower
        sprog.Launch(vOldGamePosition, collectionActor.GetPickupCollectionPosition(), Random.insideUnitCircle, 6.0f, 1.0f);
    }

    protected override void DoImpact(Vector3 pos)
    {
        if (actionOnCollect != null)
        {
            actionOnCollect(this);
        }
        Destroy(gameObject);

        /*
        if (Prefab_Impact != null)
        {
            GameObject go = Instantiate(Prefab_Impact);
            go.transform.position = pos;
            TM_Utilities.PointSpriteInDirection(UnityEngine.Random.insideUnitCircle, go);
            Destroy(go, 3.0f);
        }
         * */
    }
}
