using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	
	[SerializeField]
	BallManager ballManager;

	void Update()
	{
		foreach(Touch t in Input.touches)
		{
			if(t.phase == TouchPhase.Began)
			{
				OnClick(t.position);
			}
		}

		if((Application.platform != RuntimePlatform.IPhonePlayer) && (Application.platform != RuntimePlatform.Android) && Input.GetMouseButtonDown(0))
		{
			OnClick(Input.mousePosition);
		}
	}

	void OnClick(Vector2 mouseOrClickPosition)
	{
		Vector2 worldPoint = Camera.main.ScreenToWorldPoint( mouseOrClickPosition );
		RaycastHit2D[] hits = Physics2D.RaycastAll( worldPoint, Vector2.zero );

		Debug.Log("Hit");

		for(int i = 0; i < hits.Length; i++)
		{
			if(hits[i].collider.tag == "Ball")
			{
				ballManager.MoveActiveBall(worldPoint);
			}
			else if(hits[i].collider.tag == "Decoy")
			{
				 hits[i].transform.GetComponent<Decoy>().OnTap();
			}

			Debug.Log(hits[i].collider.name);
		}
	}
}
