using UnityEngine;
using System.Collections;
using UnityEngine.UI; //Incluir este using cuando programemos con objetos de la interfaz

[RequireComponent(typeof(Animator), typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour 
{
	public int speed;
	public int turnSpeed;
	float h;
	float v;
	Animator anim;
	public int life;
	public GameObject lifeUI;
	bool magnetPositive;
	bool magnetNegative;
	public float attractPower=7;
	public float repelPower = 7;
	public Transform maincamera;

	public GameObject particles;
	public Animator animator;

	void Start()
	{
		anim = GetComponent<Animator>();
	}

	void Test(float prueba)
	{
		Debug.Log (prueba);
	}

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

		if (magnetPositive) {
			Attract ();
			particles.SetActive(true);
			animator.SetBool("atraer", true);
			animator.SetBool("repeler", false);
		} else if (magnetNegative) {
			Repel ();
			particles.SetActive(true);
			animator.SetBool("atraer", false);
			animator.SetBool("repeler", true);
		}
		else
		{
			particles.SetActive(false);
			animator.SetBool("atraer", false);
			animator.SetBool("repeler", false);
		}

		if (Input.GetMouseButton (0)) {
			magnetPositive = true;
			magnetNegative = false;
		} else if (Input.GetMouseButton (1)) {
			magnetPositive = false;
			magnetNegative = true;

			foreach (Transform child in this.transform) {
				if (child.gameObject.layer == LayerMask.NameToLayer("object"))
				{
					child.SetParent (null);
				}
			}
		} else {
			magnetNegative = false;
			magnetPositive = false;

			foreach (Transform child in this.transform) {
				if (child.gameObject.layer == LayerMask.NameToLayer("object"))
				{
					child.SetParent (null);
				}
			}
		}

		this.GetComponent<Rigidbody>().velocity = Vector3.zero;
	}

	void Move()
	{
		h = Input.GetAxis ("Horizontal");
		v = Input.GetAxis ("Vertical");

		if(Mathf.Abs(h) < 0.1f && Mathf.Abs(v) < 0.1f)
		{
			return;
		}

		//transform.Translate (new Vector3 (0, 0, v) * speed * Time.deltaTime);
		//transform.Rotate (new Vector3 (0, h, 0) * turnSpeed * Time.deltaTime);
		//anim.SetFloat ("speed", v);

		var direction = (new Vector3 (h, 0, v)).normalized;
		var newDirection = maincamera.TransformDirection (direction);
		newDirection.y = 0;
		newDirection = newDirection.normalized;

		gameObject.GetComponent<Rigidbody>().MovePosition(transform.position + (newDirection * 0.1f));
		transform.rotation = Quaternion.LookRotation(newDirection);
	}

	void Attract (float attractRadius = 1.1f)
	{
		var targets = Physics.OverlapSphere (transform.position, attractRadius);

		foreach (var target in targets) 
		{
			Vector3 forceDirection = (transform.position-target.transform.position).normalized;
			Rigidbody rb = target.GetComponent<Rigidbody> ();

			//var powerModifier = 1f - Mathf.Clamp01(Map01((transform.position - target.transform.position).magnitude, 0f, 5f));
			
			if (rb != null)
			{
				rb.AddForce (forceDirection * attractPower /** powerModifier*/);

				/*if ((transform.position - target.transform.position).magnitude < 0.7f) {
					target.transform.SetParent (this.transform, true);
				}*/
			}
		}
	}

	void Repel (float repelRaduis = 1.8f)
	{
		var targets = Physics.OverlapSphere (transform.position, repelRaduis);

		foreach (var target in targets) 
		{
			Vector3 forceDirection = (transform.position - target.transform.position).normalized;
			Rigidbody rb = target.GetComponent<Rigidbody> ();
			if (rb != null) {
				rb.AddForce (-forceDirection*repelPower);
			}
		}
	}

	protected static float Map01(float value, float min, float max)
	{
		return (value - min) * 1f / (max - min);
	}
				
}
