using UnityEngine;
using System.Collections;

namespace UnityStandardAssets._2D
{
	public class Enemigo : MonoBehaviour {
		[SerializeField] private float m_MaxSpeed = 10f;
		[SerializeField] private float distAproximarse = 10f;
		[SerializeField] private int vida = 3;
		[SerializeField] private int fuerzaEmpujon = 100;


		//[SerializeField] private float distAtaque = 1f;

		private Animator m_Anim;            // Reference to the player's animator component.
		private Rigidbody2D m_Rigidbody2D;
		private bool m_FacingRight = true;

		private EdgeCollider2D cabeza;
		private CircleCollider2D pies;

		private float direccionPersonaje = 0;

		public GameObject personaje;
		Vector3 posInicio;
		Vector3 posFinal;
		Vector3 posDestino;
		void Start () {
			m_Anim = GetComponent<Animator>();
			m_Rigidbody2D = GetComponent<Rigidbody2D>();
			cabeza = GetComponent<EdgeCollider2D>();
			pies = GetComponent<CircleCollider2D>();
			personaje = GameObject.FindGameObjectWithTag ("jugador");
			posInicio = new Vector3 (transform.position.x,transform.position.y,transform.position.z);
			posFinal = new Vector3 (transform.position.x+2.5f,transform.position.y-2.7f,transform.position.z);
			posDestino = posFinal;
		}

		private void FixedUpdate(){

			/*float xPersonaje = personaje.transform.position.x;
			float distancia = Vector3.Distance(transform.position,personaje.transform.position);
			//Debug.Log (distancia,distAtaque);
			if (distancia <= distAproximarse) {
				Vector3 v = personaje.transform.position - transform.position;
				v.Normalize ();
				direccionPersonaje = v.x>0f?1f:-1f;
				v.Scale (new Vector3 (distancia, distancia, distancia));
				Vector3 vp = transform.position + v;
				Debug.DrawLine (transform.position, vp);
				Move(v);
			}*/
			caminata ();
			Debug.DrawLine(posInicio,posFinal);
			if (vida <= 0) {
				Destroy (gameObject);
			}
		}

		void caminata(){
			float distancia = Vector3.Distance(posDestino,transform.position);
			Vector3 v = posDestino - transform.position;
			v.Normalize ();
			direccionPersonaje = v.x>0f?1f:-1f;
			v.Scale (new Vector3 (m_MaxSpeed, m_MaxSpeed, m_MaxSpeed));
			//Vector3 vp = transform.position + v;
			//Debug.DrawLine (transform.position, vp);
			Move(v);
			if (distancia <= 0.1) {
				if (Vector3.Distance (posDestino, posFinal)<0.1) {
					posDestino = posInicio;
				} else {
					posDestino = posFinal;
				}
			}
		}

		public void Move(Vector2 move)
		{
			// The Speed animator parameter is set to the absolute value of the horizontal input.
			float vel = Mathf.Abs (move.x)>Mathf.Abs (move.y)?Mathf.Abs (move.x):Mathf.Abs (move.y);
			m_Anim.SetFloat("Speed", vel);

			// Move the character
			m_Rigidbody2D.velocity = new Vector2(move.x*m_MaxSpeed, move.y*m_MaxSpeed);

			// If the input is moving the player right and the player is facing left...
			if (move.x > 0 && !m_FacingRight){
				Flip();
			}else if (move.x < 0 && m_FacingRight){
				Flip();
			}
		}

		private void Flip(){
			// Switch the way the player is labelled as facing.
			m_FacingRight = !m_FacingRight;
			// Multiply the player's x local scale by -1.
			Vector3 theScale = transform.localScale;
			theScale.x *= -1;
			transform.localScale = theScale;

		}

		public void OnTriggerEnter2D(Collider2D otro){
			Debug.Log ("funciona");
			if (otro.CompareTag("jugadorGGJ")) {
				SelectorPersonajes p = otro.GetComponent<SelectorPersonajes> ();
				p.empujon (transform.position);
			} 
		}

		public void bajarVida(){
			vida--;
			empujon(direccionPersonaje*-1);
			m_Anim.SetTrigger ("atacado");
		}
		public void empujon(float direccion){
			m_Rigidbody2D.AddForce(new Vector2(direccion*fuerzaEmpujon,0f));
			//herido = true;
			//Debug.Log ("me pego");
		}
	}
}
