using UnityEngine;
using System.Collections;

public class particles : MonoBehaviour {
	public GameObject blackie;
	public float density;
	Vector3 thisvelocity;
	public int taggy = 1;
	public bool showTheD = false;
	public bool connected = false;
	float timer;
	/*public void AddDensity(float given){
		transform.parent.gameObject.SendMessage("AddDensity", given);
	}*/

	void OnGUI(){
		if(showTheD)
		GUI.Box(new Rect(Screen.width /2 + transform.position.x * 20,Screen.height /2 +transform.position.y * -20,100,30), "d: " + density);
	}

	// Use this for initialization
	void Start () {
		if(gameObject.name.StartsWith("Meteor"))
		blackie = GameObject.Find("core");

		if(gameObject.name.StartsWith("Meteor2"))
			blackie = GameObject.Find("core2");

		int expandedrange = blackie.GetComponent<core> ().stage;

		density = Random.Range(1f, 1f+expandedrange/4f) + expandedrange/4f + blackie.GetComponent<core> ().totalDensity/40f;
		transform.localScale = new Vector3(density/2f,density/2f,density/2f);
		rigidbody.mass = density;

		int randomtype = Random.Range(expandedrange,2+expandedrange);

		if (randomtype > 4)
						randomtype = 4;

		taggy = randomtype;

		if(randomtype == 1)
			renderer.material.color = Color.yellow;

		if(randomtype == 2)
			renderer.material.color = Color.red;

		if(randomtype == 3)
			renderer.material.color = new Color (139f / 255, 69f / 255, 19f / 255);

		if (randomtype > 3)
			renderer.material.color = Color.blue;

		//transform.localScale *= (1f + taggy / 4f);

		thisvelocity = new Vector3(blackie.transform.position.x-transform.position.x,blackie.transform.position.y-transform.position.y,0f).normalized;

		thisvelocity.x += Random.Range(-20f,20f)/100;
		thisvelocity.y += Random.Range(-20f,20f)/100;

		rigidbody.velocity = thisvelocity*3f/density;
	
	}
	
	// Update is called once per frame
	void Update () {
		if (connected == false) 
		{
			timer += Time.deltaTime;

			if(timer >= 20f)
			Destroy (this.gameObject);
		}
		if (connected == true) {
			//transform.LookAt (blackie.transform.position);
			//rigidbody.AddForce (transform.forward * 10.0f);		
		}
		if (connected == true && transform.parent.GetComponent<core> ().stage != taggy) 
			Destroy (this.gameObject);

	}


	void OnCollisionEnter(Collision col)
	{
	if (col.gameObject.name.StartsWith ("Meteor") && connected == false) 
		{
			if (col.gameObject.GetComponent<particles> ().connected == true && col.transform.parent.GetComponent<core> ().stage == taggy) 
			{
			/*
				Vector3 tempvec = col.transform.position - transform.position;
				float temprad = tempvec.magnitude*2;
				float radiuscheck = 7.5f*0.25f*Mathf.Pow(1.25f,col.transform.parent.GetComponent<core>().stage);
				Debug.Log ("radius of horizon: " + radiuscheck);
				Debug.Log ("length of magnitude: " + temprad);

							

				if(temprad > radiuscheck)
				{
					
					if(col.transform.parent.GetComponent<core>().stage >1)
					{
						col.transform.parent.GetComponent<core>().totalDensity = col.transform.parent.GetComponent<core>().coreMaxDensity - col.transform.parent.GetComponent<core>().stage*10f;
						
						col.transform.parent.GetComponent<core>().stage--;
						col.transform.localScale *= 0.75f;
						
					}
					
					
					Debug.Log ("stage num: " + col.transform.parent.GetComponent<core>().stage);
					
					col.transform.parent.GetComponent<core>().levelprogress = 0;
					//col.transform.parent.GetComponent<core>().totalDensity = ;
					transform.SetParent(null);
					
				} */

				transform.SetParent (col.transform.parent, true);
				
				rigidbody.velocity = Vector3.zero;
				rigidbody.isKinematic = true;
				
				connected = true;
				
				transform.parent.GetComponent<core> ().totalDensity += density;
				transform.parent.GetComponent<core> ().levelprogress += density;
				
				density = 0;

			}
		}
	}


	void OnTriggerStay(Collider other)
	{

		if(other.gameObject.name.StartsWith("eventhorizon"))
		{
			float totzdensity = other.transform.parent.GetComponent<core>().totalDensity;
			this.gameObject.rigidbody.AddForce(new Vector3(other.transform.position.x-transform.position.x,other.transform.position.y-transform.position.y,0f).normalized * (1f + totzdensity/20f));
		}
	}

}
