using UnityEngine;
using System.Collections;

public class Frostdora : MonoBehaviour 
{

    PlayerMovement playerScript;
    Rigidbody2D myBody;
    bool bInitialPush = false;

    float fFlightTime = 0.0f;

    float fDontGetCaughtByPlayerTimer;

    public GameObject prefab_frozenDebuff;

    public AudioSource myAudio;
    public AudioClip freezeEnemy;

    // Use this for initialization
	void Start ()
	{
        myAudio = gameObject.AddComponent<AudioSource>();
        myBody = GetComponent<Rigidbody2D>();
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (fDontGetCaughtByPlayerTimer > 0.0f)
            fDontGetCaughtByPlayerTimer -= Time.smoothDeltaTime;

        //zoom towards player
        if (playerScript == null && !GetPlayerScript())
            return;

        Vector2 vDelta = playerScript.transform.position - transform.position;
        float fDist = vDelta.magnitude;
        vDelta.Normalize();
        myBody.AddForce(vDelta * 50 * fDist * Time.smoothDeltaTime);

        if( fFlightTime > 5.0f && Mathf.Abs(fDist) > 12.0f)
        {
            Vector3 hatPos = transform.position;
            hatPos.y = playerScript.transform.position.y;
            transform.position = hatPos;
            myBody.velocity = new Vector2(myBody.velocity.x, 0);
        }

        fFlightTime += Time.smoothDeltaTime;
	}

    protected virtual bool GetPlayerScript()
    {
        GameObject go = GameObject.Find("PlayerMans");
        if (go != null)
        {
            playerScript = go.GetComponent<PlayerMovement>();
        }

        return playerScript != null;
    }

    public void LaunchFrostdora( Vector2 vDirection )
    {
        myBody = GetComponent<Rigidbody2D>();
        myBody.AddForce(vDirection * 1200.0f);
        fDontGetCaughtByPlayerTimer = 0.5f;
        fFlightTime = 0.0f;

        Debug.Log("Frostdora fired! " + vDirection * 1200.0f);
    }

    public void OnTriggerEnter2D(Collider2D coll)
    {
        
        //Did we collide with an enemy?
        BaseEnemy enemyMans = coll.gameObject.GetComponent<BaseEnemy>();
        if (enemyMans != null && enemyMans.currentAiEnemyBehaviorState != AIEnemyBehaviorState.dying_lol && enemyMans.currentAiEnemyBehaviorState != AIEnemyBehaviorState.frozen)
        {
            GameObject newFreezyThing = GameObject.Instantiate(prefab_frozenDebuff);
            newFreezyThing.transform.position = enemyMans.transform.position;
            newFreezyThing.transform.parent = enemyMans.transform;
            enemyMans.currentAiEnemyBehaviorState = AIEnemyBehaviorState.frozen;

            FrozenDebuff fd = newFreezyThing.GetComponent<FrozenDebuff>();
            fd.AssignVictim( enemyMans );

            myAudio.PlayOneShot(freezeEnemy);

            return;
        }

        //zoom towards player
        if (playerScript == null && !GetPlayerScript())
            return;

        //player?
        if (coll.gameObject == playerScript.gameObject && fDontGetCaughtByPlayerTimer <= 0.0f)
        {
            Debug.Log("Oh no! Player caught me!");
            playerScript.RecoverFedora();
            gameObject.SetActive(false);
        }

        /*
        BaseEnemy enemyMans = coll.gameObject.GetComponent<BaseEnemy>();
        if (enemyMans != null)
        {
            Vector3 vEnemyTowardsUs = transform.position - enemyMans.transform.position;
            Vector3 vEnemyMovDirection = enemyMans.bFacingRight ? Vector3.right : Vector3.left;
            float fDot = Vector3.Dot(vEnemyMovDirection, vEnemyTowardsUs);
            if (fDot > 0)
                enemyMans.SuggestJump();

        }
         */
    }
}
