using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class PacVan_Death : MonoBehaviour {

    public AudioClip death_sfx;

    public int pacVan_Lives = 3;
	private bool power;
	public Transform homePosition;

	void Start()
	{
		power = false;
		if (!AllScores.all_scores.scores.ContainsKey ("ghost")) {
			AllScores.all_scores.scores.Add ("ghost", 0);
		} else {
			AllScores.all_scores.scores["ghost"] = 0;
		}
		if (!AllScores.all_scores.scores.ContainsKey ("dracula")) {
			AllScores.all_scores.scores.Add ("dracula", 0);
		}  else {
			AllScores.all_scores.scores["dracula"] = 0;
		}
		if (!AllScores.all_scores.scores.ContainsKey ("werewolf")) {
			AllScores.all_scores.scores.Add ("werewolf", 0);
		}  else {
			AllScores.all_scores.scores["werewolf"] = 0;
		}
		if (!AllScores.all_scores.scores.ContainsKey ("zombie")) {
			AllScores.all_scores.scores.Add ("zombie", 0);
		}  else {
			AllScores.all_scores.scores["zombie"] = 0;
		}
	}

	void PowerStart() {
		power = true;
	}

	void PowerDone(){
		power = false;
	}

    void OnCollisionEnter2D(Collision2D coll)
    {
        Debug.Log("You collided with: " + coll.gameObject.tag);

		if (!power && (coll.gameObject.tag == "Monster1" || coll.gameObject.tag == "Monster2" || coll.gameObject.tag == "Monster3" || coll.gameObject.tag == "Monster4")) {
			//reward the monster somehow, +1 score or something (Maybe final kill is worth 3 points)
			//coll.gameObject.SendMessage("PlusScore", 1);
			if (AllScores.all_scores != null) {
				if (coll.gameObject.tag == "Monster1") {
					Debug.Log (AllScores.all_scores.scores["ghost"]);
					AllScores.all_scores.scores["ghost"] += 1;
				} else if (coll.gameObject.tag == "Monster2") {
					AllScores.all_scores.scores["dracula"] += 1;
				} else if (coll.gameObject.tag == "Monster3") {
					AllScores.all_scores.scores["werewolf"] += 1;
				} else if (coll.gameObject.tag == "Monster4") {
					AllScores.all_scores.scores["zombie"] += 1;
				}
			}

			pacVan_Lives--;
            if (death_sfx != null){
                AudioSource.PlayClipAtPoint(death_sfx, transform.position);
            }

			Debug.Log ("YOU DIED (Ran into a monster)");

			//respawn pacVan? or is it game over
			if (pacVan_Lives > 0) {

                //move PacVan home for his new life, and reset his movement
                transform.position = new Vector3(homePosition.position.x, homePosition.position.y, transform.position.z);
				gameObject.GetComponent<pacmanMove> ().resetDest ();

                //Make PacVan invulnerable and increase his speed for a short duration, to prevent camping his home (pacmanMove)
                gameObject.SendMessage("respawnAmp");

			} else { 
				//GAME OVER
				Debug.Log ("GAME OVER (Out of Lives)");
				AllScores.all_scores.results = "Monsters Win";
				SceneManager.LoadScene("Results");
			}
				
		} else if (power && (coll.gameObject.tag == "Monster1" || coll.gameObject.tag == "Monster2" || coll.gameObject.tag == "Monster3" || coll.gameObject.tag == "Monster4")) {
			coll.gameObject.SendMessage ("Eaten");
		}

    }
}
