using UnityEngine;
using System.Collections;




public class _PlayerMovement : MonoBehaviour {

	// Use this for initialization
	AudioSource Slash;
	float speed = 0.5f;
	public float mouseX;
	Vector3 distanceToTarget;
	public static bool  attacking;
    	public static bool canDie;
	public bool facingLeft;
	private float attackTimer = 0;
	private float attackCooldown = 0.25f;

	public GameObject brokenPepsi;
	public GameObject brokenCola;

	private Animator anim;

	void Start () 
	{
		Slash = GetComponent<AudioSource>();
		// should be set to false when game is done, is true now for easyness
		Cursor.visible = true;
		attacking = false;
	
		facingLeft = true;
		anim = gameObject.GetComponent<Animator> ();
	}
		
	void Update () 
	{
		// connects the boolean in the animator with the boolean in the script
		anim.SetBool ("Attacking", attacking);
		anim.SetBool ("facingLeft", facingLeft);
	}

	void FixedUpdate()
	{
		// make the player walk
		if(Input.GetKey("d"))
		{
			facingLeft = false;
			transform.Translate ((Vector2.right) * speed);
		}
		if(Input.GetKey("w"))
		{
			transform.Translate ((Vector2.up) * speed);
		}
		if(Input.GetKey("a"))
		{
			facingLeft = true;
			transform.Translate ((Vector2.left) * speed);
		}
		if(Input.GetKey("s"))
		{
			transform.Translate ((Vector2.down) * speed);
		}
		// end of the player walk code


		// make the player attack
		if(Input.GetMouseButtonDown(0) && !attacking)
		{
			attacking = true;
			Slash.Play ();
			attackTimer = attackCooldown;
            		canDie = true;
		}

		// handles the time that the attack takes
		if(attacking)
		{
			if(attackTimer > 0)
			{
				attackTimer -= Time.deltaTime;
			}
			else
			{
				Slash.Stop ();
				attacking = false;
			}
		}
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if(attacking)
		{
			if(other.gameObject.tag == "enemy")
			{
				if (other.gameObject.name == "PepsiBoss(Clone)" || other.gameObject.name == "CocaColaBoss(Clone)")
				{
					other.gameObject.GetComponent<BossBehavorialManager> ().removeMe ();
				}

				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);
			}
		}
	}
}