using UnityEngine;
using System.Collections;

public class Pentagram : MonoBehaviour {
	AudioSource spell;
	public Sprite pentagramOff;
	public Sprite pentagramOn;
	public Sprite pentagramToggle;
    public static bool canDie;
    float timer;
	int counter;
    public static bool killingBottles;
	bool time_to_doom_some_bottles;

	public GameObject brokenPepsi;
	public GameObject brokenCola;

	// Use this for initialization
	void Start () {
		time_to_doom_some_bottles = false;
		spell = GetComponent<AudioSource>();
		timer = 0f;
		counter = 0;
		canDie = false;
	}
	
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;
		if (counter > 3)
		{
			time_to_doom_some_bottles = true;

			this.gameObject.GetComponent<SpriteRenderer> ().sprite = pentagramOn;
			canDie = true;
			// print ("canDie " + canDie);
			if (timer >= 6.66f)
			{         				
                GameObject.FindGameObjectWithTag("Player").GetComponent<PentagramUpgrade> ().canPlacePentagram = true;
				Destroy (this.gameObject);
				spell.Stop ();
			}
		}
		else
		{
			this.gameObject.GetComponent<SpriteRenderer> ().sprite = pentagramOff;
			if (timer > 0.2f)
			{
				this.gameObject.GetComponent<SpriteRenderer> ().sprite = pentagramToggle;

				if (timer > 0.2666f)
				{
					spell.Play ();
					counter++;
					timer = 0;
				}
			}
		}
	
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (time_to_doom_some_bottles)
		{
			if (other.gameObject.tag == "enemy")
			{
				if (other.gameObject.name == "Pepsi(Clone)")
				{
					GameObject newPepsi = (GameObject)Instantiate (brokenPepsi, other.transform.position, Quaternion.Euler (0, 0, 0));
				}
				else
				{
					GameObject newCola= (GameObject)Instantiate (brokenCola, other.transform.position, Quaternion.Euler (0, 0, 0));
				}
				Destroy(other.gameObject);
			}
		}
	}
}
