using UnityEngine;
using System.Collections;

public enum States
{
	IDLE = 0, FLY = 1, KO = 2, LIFE = 3
}



public class CharacterBehaviour : MonoBehaviour {

	static Vector3 FANCULONIA = new Vector3(-1000f, -1000f, -1000f);
	//						  				 ATTACK				UP			  	 LEFT 				DOWN
	private static KeyCode[] keysPlayerOne = {KeyCode.Space, 	KeyCode.W, 		 KeyCode.A,			KeyCode.D};
	private static KeyCode[] keysPlayerTwo = {KeyCode.Return, 	KeyCode.UpArrow, KeyCode.LeftArrow, KeyCode.RightArrow};

	private Animator animator;

	private States state;

	private float cooldown = 1.0f;
	private float nextAttack = 0.0f;
	private float durationAttack = 0.1f;
	private float timeAttack = 0.0f;
	private float hspeed = 0;
	private float vspeed = 0;
	private float SPEED = 5.0f;
	private float JUMP_SPEED = 150.0f;

	public States State {
		get { return this.state; }
	}
	public string Name {
		get { return this.name; }
	}
	
	// Use this for initialization
	void Start () {
		this.state = States.FLY;
		//this.spawn ();
		animator = this.GetComponent<Animator>();
	}

	/*
	 *	Player con comandi WASD+SPACE
	 */
	void keyPressedPlayer(int n) {

		if (this.state == States.KO) {
			return;
		}

		KeyCode[] keys;
		if (n == 0) {
			keys = keysPlayerOne;
		} else {
			keys = keysPlayerTwo;
		}

		//ATTACK
		if (Input.GetKeyDown(keys[0])) {
			this.attack();
		}

		//JUMP
		if (Input.GetKeyDown(keys[1])) {
			if (this.state == States.IDLE) {
				this.state = States.FLY;
				this.vspeed = this.JUMP_SPEED;
				//animator.SetInteger("Direction", 2); //Cambia l'animazione della macchina a stati dell'animator (parte l'animazione del salto)
			}

		} else {
			this.vspeed = 0;
		}

		//LEFT
		if (Input.GetKey (keys[2])) {
			this.hspeed = -this.SPEED;
			if (this.state == States.FLY)
				animator.SetInteger("Direction", 2);
			else
				animator.SetInteger("Direction", 0); //Cambia l'animazione della macchina a stati dell'animator (parte l'animazione del pg verso sinistra)

		//RIGHT
		} else if (Input.GetKey (keys[3])) {
			this.hspeed = this.SPEED;
			if (this.state == States.FLY)
				animator.SetInteger("Direction", 3);
			else
				animator.SetInteger("Direction", 1); //Cambia l'animazione della macchina a stati dell'animator (parte l'animazione del pg verso destra)

		//NO MOVEMENTS LEFT OR RIGHT
		} else {
			this.hspeed = 0;
		}
	}

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

		if (this.name == "Player1" ) {
			this.keyPressedPlayer(0);
		} else {
			this.keyPressedPlayer(1);
		}

		float delta = Time.deltaTime * SPEED;
		Vector3 vett = new Vector3 (this.hspeed, 0, 0) * delta;
		vett = Vector3.ClampMagnitude(vett, delta);
		this.transform.Translate(vett);

		this.rigidbody2D.AddForce(new Vector2(0, vspeed));
	}

	public void spawn() { //Visualizza e attiva le collisioni
		this.state = States.IDLE;
		this.collider2D.enabled = true;
		this.renderer.enabled = true;
		Debug.Log ("Sono spawnato!");
	}

	public void die() { //Disattiva la visualizzazione e le collisioni
		this.state = States.KO;
		this.collider2D.enabled = false;
		this.rigidbody2D.isKinematic = true;
		this.renderer.enabled = false;


		this.transform.position = FANCULONIA;
		Debug.Log ("Sono morto!");
	}


	public void attack() {
		if (Time.time > this.nextAttack) {
			this.nextAttack = Time.time + this.cooldown;
			this.timeAttack = Time.time + this.durationAttack;
		}
	}

	void collision(Collision2D coll) {

		if (this.state != States.KO) { 
			if (coll.gameObject.tag == "Enemy") {
				Debug.Log("MORTO PER COLPA DI : "+ coll.gameObject.name);
				this.die();
				return;
			}

			if (coll.gameObject.tag == "Ground") {
				this.state = States.IDLE;
			}
		
			if (coll.gameObject.tag == "Player") {
				//this.state = States.IDLE; //A COSA SERVE?
				if (Time.time < this.timeAttack) {

					Debug.Log("ATTACCO");
					/*
					coll.gameObject.rigidbody2D.position = new Vector3 (coll.gameObject.rigidbody2D.position.x + 1, coll.gameObject.rigidbody2D.position.y + 1, 0);
					//coll.gameObject.rigidbody2D.velocity = new Vector3 (coll.gameObject.rigidbody2D.velocity.x * 2, 2 * coll.gameObject.rigidbody2D.velocity.y, 0);
					if (this.gameObject.rigidbody2D.position.x < coll.gameObject.rigidbody2D.position.x) {
						coll.gameObject.rigidbody2D.velocity = new Vector3((coll.gameObject.rigidbody2D.position.x - this.rigidbody2D.position.x) * 5, (coll.gameObject.rigidbody2D.position.y - this.rigidbody2D.position.y) * 1.5f, 0);
						Debug.Log ("Attacco 2-1");
					}
					else {
						coll.gameObject.rigidbody2D.velocity = new Vector3((this.rigidbody2D.position.x - coll.gameObject.rigidbody2D.position.x) * -5, (this.rigidbody2D.position.y - coll.gameObject.rigidbody2D.position.y) * 1.5f, 0);
						Debug.Log ("Attacco 2-2");
					}*/
				}
			}
		}
	}

	void OnCollisionEnter2D(Collision2D coll) {
		collision(coll);
	}
	void OnCollisionStay2D(Collision2D coll) {
		collision(coll);
	}
	/*void OnCollisionExit2D(Collision2D coll) {
		this.state = States.FLY;
	}*/

}
