using UnityEngine;
using System.Collections;
using System.Collections.Generic; //input for lists

public class playerController : MonoBehaviour {

	public string HorizontalBtn; //holds string for horizontal input
	public string VerticalBtn; //holds string for Vertical input
	public string StrongAttackBtn;
	
	public Animator animator; //Holds animator for Pinata
	public GameObject heart; //Health sprites
	public GameObject newConfetti; //confetti when hit
	public stickController stick; //controller for stick
	
	public float runSpeed; //run speed
	public float jumpHeight; //jump height
	public float health; //holds health
	public int confettiOnHit; //amount of confetti created on hit
	public Vector2 healthPos;
	public float invulTime;
	public float attackWait;
	public bool isHit;

	private float displayedHealth = 3; //currently displayed health
	private bool updateHealth = true; //if the health bar needs updating
	private float curDirection = -1; //direction player is pointing
	private bool canJump = true; // if player can jump
	private List<GameObject> hearts = new List<GameObject> (); //list which holds health sprites;
	private bool isSpinning = false; //checks if player is spin attacking
	private float lastHit;
	private float lastAttack;


	void Start(){
		if (transform.rotation.y == 180)
			curDirection = -1;
		else
			curDirection = 1;

	}

	
	// Update is called once per frame
	void Update () {

		displayHealth ();
		death ();
		applyPlayerDirection (Input.GetAxisRaw (HorizontalBtn));
		damage ();
	}

	void FixedUpdate() {

		/*
		 * Movement inputs
		 */
		Vector2 velocity = rigidbody2D.velocity;
		velocity.x = Input.GetAxis (HorizontalBtn) * runSpeed; //Horizontal input

		if ((Input.GetAxisRaw (VerticalBtn) == 1) && (canJump)) //Vertical input
		{
			velocity.y = jumpHeight;
			canJump = false;
		}//end if

		if (Input.GetAxisRaw (StrongAttackBtn)==1) {
			if (!isSpinning && (lastAttack+attackWait) < Time.time){
				lastAttack = Time.time;
				StartCoroutine (spinAttack (Vector3.down * 360, 0.2f));
			}
		}



		rigidbody2D.velocity = velocity; //apply inputs
		animator.SetFloat("Velocity",velocity.magnitude);//inputs for animator
		animator.SetBool ("Fall", !canJump);


	}//end fixed update

	private void displayHealth(){

		if (displayedHealth != health) //check if health needs updating
			updateHealth = true;

		if (updateHealth){
			foreach (GameObject desHeart in hearts) //Destroy all heart sprites
				GameObject.Destroy (desHeart);
			hearts.Clear(); //sets list to zero
		
			for (int i=1; i<= health; i++) { //creates new heart sprite for each health 
				GameObject heartCanister = Instantiate (heart) as GameObject; //creat heart sprite
				Vector3 heartPos = new Vector3 ();

				//set position
				if (healthPos.x == 1)
					heartPos.x = (i * 0.033f);
				else
					heartPos.x = 1-(i * 0.033f);

				if (healthPos.y == 1)
					heartPos.y = 0.95f;
				else
					heartPos.y = 0.05f;

				heartCanister.transform.position = heartPos;
				hearts.Add(heartCanister); //adds heart to list
			}//end for

		displayedHealth = health;
		updateHealth = false;
		}//end if

	}//end displayHealth

	//respawns player if they die
	private void death(){
		if (health <= 0) {
			Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0); //picks random position
			health = 3; //resets life
			transform.position = spawnPos; //changes position
			rigidbody2D.velocity = Vector2.zero;
		}//end if
	}//end death

	private void applyPlayerDirection(float moveHorizontal)
	{

		if  ((curDirection != moveHorizontal) && (moveHorizontal != 0) && !isSpinning) //if player movement direction vs displayed direction
		{
			transform.Rotate(0,180,0); //rotates player
			curDirection = moveHorizontal; //updates direction
		}
	}

	private void damage (){
		if (isHit) {
			gameObject.GetComponent<PolygonCollider2D> ().enabled = false;
			isHit = false;
			lastHit = Time.time;
		}
		if (lastHit+invulTime < Time.time)
			gameObject.GetComponent<PolygonCollider2D> ().enabled = true;


	}
	IEnumerator spinAttack(Vector3 byAngles, float inTime) {
		isSpinning = true;
		stick.isAttacking = true;
		Debug.Log ("" + gameObject.name + "spinning");
		Quaternion startAngle = transform.rotation;
		Quaternion endAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
		float direction = transform.eulerAngles.y;

		for(float i = 0; i < 1; i += Time.deltaTime/inTime) {
			transform.rotation = Quaternion.Euler(0,direction,Mathf.Lerp(0,360,i));
			yield return null;

		}
		transform.eulerAngles = new Vector3 (0,direction,0);
		Debug.Log ("" + gameObject.name + "End spin");
		isSpinning = false;
		stick.isAttacking = false;
	}



	

	void OnCollisionEnter2D(Collision2D col){
		if (col.collider.tag == "ground")
			canJump = true;
		else if (col.collider.tag == "Player") {
			canJump = true;
			}
	}






}
