using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Drug : MonoBehaviour {

	public bool dontSpawn;

	void Start ()
	{
		if (!dontSpawn)
			spawn ();
	}

	public void spawn()
	{
        //GameObject points = GameObject.Find ("Points");
        //GameObject[] points = GameController.copPoints;
        List<CopPoint> availablePoints = new List<CopPoint>();
		List<float> distances = new List<float>();
		float totalDistance = 0f;
        GameObject[] copPoints = GameController.instance.getCopPoints();
		for (int i = 0; i < copPoints.Length; i++)
		{
			Transform newPoint = copPoints[i].transform;
			if (((Vector2)(newPoint.transform.position - PlayerMovement.instance.transform.position)).magnitude > 70f)
			{

				availablePoints.Add (newPoint.GetComponent<CopPoint>());
				distances.Add(((Vector2)(transform.position - PlayerMovement.instance.transform.position)).magnitude);
				totalDistance += distances[distances.Count - 1];
			}


			 //= availablePoints[Random.Range (0, availablePoints.Count)].transform.position;
			
		}
		
		float selectedValue = Random.Range (0f, totalDistance);
		
		Vector3 selectedPoint = Vector3.zero;
		for (int j = 0; selectedValue >= 0f; j++)
		{
			selectedValue -= distances[j];
			if (selectedValue < 0f)
				selectedPoint = availablePoints[j].transform.position + (Vector3)(MathHelper.getVectorFromAngle(Random.Range (0f, Mathf.PI * 2f),
				                                                                                                Random.Range(0f, availablePoints[j].GetComponent<CircleCollider2D>().radius)));
		}
		transform.position = new Vector3(selectedPoint.x, selectedPoint.y, transform.position.z);
	}
}
