using UnityEngine;
using System.Collections;

public class EnemyBehavorialManager : MonoBehaviour {
    private GameObject objSpawn;
    public GameObject cauldronP, cauldronC, pepsi, coke;
    float speed = 0.15f;
	public static float loseCounter;
    private int SpawnerID;
	AudioSource dying;
   // Vector2 movement;
    Rigidbody2D rb;

    // Use this for initialization
    void Start () {
		dying = GetComponent<AudioSource>();
        rb = GetComponent<Rigidbody2D>();
        objSpawn = GameObject.FindWithTag("Spawner");
		loseCounter = 0;
    }

    // Update is called once per frame
    void Update () {
        pepsi.transform.position = Vector2.MoveTowards(this.transform.position, cauldronP.transform.position, speed);
        coke.transform.position = Vector2.MoveTowards(this.transform.position, cauldronC.transform.position, speed);
    }

    void OnBecameInvisible()
    {
        removeMe();
    }

    void removeMe()
    {
        objSpawn.BroadcastMessage("killEnemy", SpawnerID);
        Destroy(gameObject);

    }

    // this gets called in the beginning when it is created by the spawner script
    void setName(int sName)
    {
        SpawnerID = sName;
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if(coll.gameObject.tag == "CauldronP" || coll.gameObject.tag == "CauldronC")
        {
            removeMe();
			loseCounter++;
			GameObject.Find ("CocaPepsa").GetComponent<CocaPepsaLife> ().LoseLife ();
			GameObject.FindGameObjectWithTag ("Player").GetComponent<LoseGame> ().counter++;
		
        }
		if(/*(coll.gameObject.tag == "Player" && _PlayerMovement.canDie == true && _PlayerMovement.attacking == true) ||*/ (coll.gameObject.tag == "Boomerang" && BoomerangUpgrade.canDie == true) || (coll.gameObject.tag == "Pentagram" && Pentagram.canDie == true) )
        {
			dying.Play ();
			print ("played");
            removeMe();
        }
    }

    void OnTriggerEnter2D(Collider2D coll)
    {

		if(coll.gameObject.tag == "Boomerang" && BoomerangUpgrade.canDie == true) 			
        {
			print ("death by boomerang");
            removeMe();

        }
    }

	void OnTriggerEnter2D(Collision2D collider)
	{
		if (collider.gameObject.tag == "Pentagram")// &&  Pentagram.canDie == true)
		{
			print ("Killing bottles by Pentagram");
			removeMe ();
		}
	}

}
