using UnityEngine;
using System.Collections;

public class PlayerBlack : Player 
{
	private GameObject[] whiteOnly;
	private GameObject[] blackOnly;

	private string nameHolding;

	void Start()
	{
		whiteOnly = GameObject.FindGameObjectsWithTag("OnlyWhite");
		blackOnly = GameObject.FindGameObjectsWithTag("OnlyBlack");
	}

	protected new void Update()
	{
		base.Update();
		if (base.blackActive)
		{
			base.Movement ("Horizontal", "Vertical");

			if (Input.GetKeyDown("f"))
			{
				if (base.lastPosition == 0)
				{
					base.animator.SetInteger("Horizontal", 0);
					base.animator.SetInteger("Vertical", 5);
				}
				else if (base.lastPosition == 1)
				{
					base.animator.SetInteger("Horizontal", 2);
					base.animator.SetInteger("Vertical", 0);
				}
				else if (base.lastPosition == 2)
				{
					base.animator.SetInteger("Horizontal", 2);
					base.animator.SetInteger("Vertical", 0);
				}
				else if (base.lastPosition == 3)
				{
					base.animator.SetInteger("Horizontal", 0);
					base.animator.SetInteger("Vertical", 4);
				}
			}

			if (transform.childCount > 0)
			{
				if (base.lastPosition == 0)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x, transform.position.y-0.4f, 0f);
				}
				else if (base.lastPosition == 1)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x-0.4f, transform.position.y, 0f);
				}
				else if (base.lastPosition == 2)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x+0.4f, transform.position.y, 0f);
				}
				else if (base.lastPosition == 3)
				{
					transform.FindChild(nameHolding).transform.position = new Vector3 (transform.position.x, transform.position.y+0.4f, 0f);
				}
			}

		}
		else
		{
			base.animator.SetInteger("Vertical", 0);
			base.animator.SetInteger("Horizontal", 0);
		}

		if (Input.GetKeyUp("q")) 
		{
			transform.DetachChildren();
		}
	}
	
	void OnCollisionStay2D(Collision2D other)
	{
//		if (other.transform.tag == "OnlyBlack")
//		{
//			if (Input.GetKeyDown("q")) 
//			{
//				transform.DetachChildren();
//				other.transform.parent = transform;
//				nameHolding = other.transform.name;
//			}
//		}
		if (blackActive)
		{
			if (Input.GetButton("Fire1")) //Fire1 = q
			{
				if (other.transform.tag == "Black") 
				{
					foreach (GameObject white in whiteOnly)
					{
						white.gameObject.SetActive(false);
					}
				}
				else if (other.transform.tag == "Fim")
				{

					if(jogadorPerto)
					{
						Application.LoadLevel(5);
					}
					else
					{
						if (blackActive)
						{
							blackGuy.gameObject.GetComponent<PlayerBlack>().blackActive = false;
							whiteGuy.gameObject.GetComponent<PlayerWhite>().blackActive = false;
						}
						else
						{
							blackGuy.gameObject.GetComponent<PlayerBlack>().blackActive = true;
							whiteGuy.gameObject.GetComponent<PlayerWhite>().blackActive = true;
						}
					}
				}
			}
		}
	}
}
