using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
	public GameObject redWave;
	public GameObject blueWave;
	public GameObject greenWave;
	public GameObject yellowWave;
	public Animator anim;

	public float waveSpeed = 5f;

	public bool beatReady;
	public static float count;
	public bool isHit;
	public float hitTime;

	public GameObject ultBeat;
	void Start () {
		count = 50;
		beatReady = false;

	}

	void Update () {
		count = Mathf.Clamp (count, 0f, 100f);
		 anim.SetBool ("isReady",beatReady);
		if (Input.GetKey (KeyCode.LeftArrow)) {
			transform.Rotate(Vector3.forward * 5);
		}
		if (Input.GetKey (KeyCode.RightArrow)) {
			transform.Rotate(Vector3.back * 5);
		}
		if (Input.GetKeyDown(KeyCode.D)) {
			GameObject wave = GameObject.Instantiate<GameObject> (redWave);
			Rigidbody2D waveRb = wave.GetComponent<Rigidbody2D> ();
			wave.transform.position = transform.position;
			wave.transform.rotation = transform.rotation;
			waveRb.velocity = transform.up * waveSpeed;
		}
		if (Input.GetKeyDown(KeyCode.W)) {
			GameObject wave = GameObject.Instantiate<GameObject> (blueWave);
			Rigidbody2D waveRb = wave.GetComponent<Rigidbody2D> ();
			wave.transform.position = transform.position;
			wave.transform.rotation = transform.rotation;
			waveRb.velocity = transform.up * waveSpeed;
		}
		if (Input.GetKeyDown(KeyCode.A)) {
			GameObject wave = GameObject.Instantiate<GameObject> (greenWave);
			Rigidbody2D waveRb = wave.GetComponent<Rigidbody2D> ();
			wave.transform.position = transform.position;
			wave.transform.rotation = transform.rotation;
			waveRb.velocity = transform.up * waveSpeed;
		}
		if (Input.GetKeyDown(KeyCode.S)) {
			GameObject wave = GameObject.Instantiate<GameObject> (yellowWave);
			Rigidbody2D waveRb = wave.GetComponent<Rigidbody2D> ();
			wave.transform.position = transform.position;
			wave.transform.rotation = transform.rotation;
			waveRb.velocity = transform.up * waveSpeed;
		}
		if (count >= 100) {
			beatReady = true;
		}
		if (Input.GetKeyDown(KeyCode.Space) && beatReady == true) {
			count = 50;
			beatReady = false;
			Instantiate (ultBeat);
		}
		if (isHit == true && Time.time >= hitTime + 1f) {
			hitTime = Time.time;
			count -= 5;
		}

	}
	void OnTriggerEnter2D (Collider2D col) {
		if (col.gameObject.tag == "Red Enemy" || col.gameObject.tag == "Green Enemy" || col.gameObject.tag == "Blue Enemy" || col.gameObject.tag == "Yellow Enemy") {
			count -= 5;
			hitTime = Time.time;
			col.gameObject.GetComponent<EnemyScript>().enabled = false;
			col.gameObject.GetComponent<Rigidbody2D> ().velocity = new Vector3 (0f, 0f, 0f);
		}
	}
	void OnTriggerStay2D (Collider2D col) {
		if (col.gameObject.tag == "Red Enemy" || col.gameObject.tag == "Green Enemy" || col.gameObject.tag == "Blue Enemy" || col.gameObject.tag == "Yellow Enemy") {
			isHit = true;
		}
	}
	void OnTriggerExit2D (Collider2D col) {
		if (col.gameObject.tag == "Red Enemy" || col.gameObject.tag == "Green Enemy" || col.gameObject.tag == "Blue Enemy" || col.gameObject.tag == "Yellow Enemy") {
			isHit = false;
		}
	}
}
