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


public class PlayerController : MonoBehaviour {

	public float speed;
	public Text countText;
	public Text winText;

	private Rigidbody rb;
	private int count;

	public int numberOfObject = 12;

	public float radius = 5f;

	public float Pseconds = 10f;

	private bool changePickups = false;

	List<GameObject> pickups_list= new List<GameObject>();
	List<GameObject> pickups1_list= new List<GameObject>();
	List<GameObject> pickups2_list= new List<GameObject>();

	List<Vector3> reserved_pos= new List<Vector3>();


	public GameObject pickup;
	public GameObject pickup1;
	public GameObject pickup2;


	void Start ()
	{

		for (int i = 0; i < numberOfObject; i++) {
			float angle = i * Mathf.PI * 2 / numberOfObject;
			Vector3 pos = new Vector3(Mathf.Cos(angle) * radius, 0.5f, Mathf.Sin(angle)* radius);
			reserved_pos.Add(pos);

			if (i % 2 == 0)
				pickups_list.Add(Instantiate (pickup, pos, Quaternion.identity));
			else
			{
				pickups1_list.Add(Instantiate(pickup1, pos, Quaternion.identity));

				GameObject g = Instantiate (pickup2, pos, Quaternion.identity);
				g.SetActive (false);
				pickups2_list.Add(g);
			}


		}


		//GameObject g = Instantiate(pickup1, new Vector3(1, 0.5f, 0), Quaternion.identity);
		//GameObject g2 = Instantiate(pickup1, new Vector3(1, 0.5f, 0), Quaternion.identity);

			
		//g.SetActive (false);
		//g2.SetActive (false);

		//if(new Vector3(1, 0.5f, 0.5f) == g.transform.position)
		//	g.SetActive (true);
		

		rb = GetComponent<Rigidbody>();
		count = 0;
		SetCountText ();
		winText.text = "";
	}

	void FixedUpdate ()
	{
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

		rb.AddForce (movement * speed);
	}


	void Update ()
	{
		if (changePickups)
		{
			Pseconds -= Time.deltaTime;
			if (Pseconds <= 0) 
			{
				for (int i = 0; i < pickups1_list.Count; i++) 
				{
					pickups1_list [i].SetActive (true);
					pickups2_list [i].SetActive (false);
				}

				changePickups = false;
				Pseconds = 10;
			} 
		}
			
	}

	void OnCollisionEnter (Collision other)
	{
		if(other.gameObject.CompareTag("Pick Up1"))
		{
			Vector3 pos = getRandomEmptyPos();
			reserved_pos.Add(pos);
			pickups1_list.Add(Instantiate(pickup1, pos, Quaternion.identity));

			GameObject g = Instantiate (pickup2, pos, Quaternion.identity);
			g.SetActive (false);
			pickups2_list.Add(g);

			numberOfObject++;
		}
	}

	void OnTriggerEnter(Collider other) 
	{

		if (other.gameObject.CompareTag ("Pick Up"))
		{
			for (int i = 0; i < pickups1_list.Count; i++) 
			{
				pickups1_list [i].SetActive (false);
				pickups2_list [i].SetActive (true);

			}

			changePickups = true;
			other.gameObject.SetActive (false);
			count = count + 1;
			SetCountText ();
		}

		if (other.gameObject.CompareTag ("Pick Up2"))
		{
			other.gameObject.SetActive (false);

			for (int i = 0; i < pickups1_list.Count; i++)
			{
				if (other.gameObject.transform.position == pickups1_list [i].transform.position) 
				{
					pickups1_list.RemoveAt (i);
					pickups2_list.RemoveAt (i);
				}
					
			}

			for (int i = 0; i < reserved_pos.Count; i++)
			{
				if (other.gameObject.transform.position == reserved_pos [i]) 
				{
					reserved_pos.RemoveAt (i);
				}

			}

			count = count + 1;
			SetCountText ();
		}


	}
	void SetCountText ()
	{
		countText.text = "Count: " + count.ToString ();
		if (count >= numberOfObject) 
		{
			winText.text = "You Win!";
		}
			
	}

	Vector3 getRandomEmptyPos()
	{
		Vector3 pos; 
		while (true) 
		{
			pos = new Vector3(Random.Range(-8, 8), 0.5f, Random.Range(-8, 8));

			if (!reserved_pos.Contains (pos))
				break;
		}

		return pos;
	}
}

