// Use this for initialization
	void Start () {
		numPlanets = 1000;
		expansionIncrement = 80f / (numPlanets - 1);
		if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor) {
			dataPath = @"../data/1000blocks.db";
		} else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) {
			dataPath = @"..\data\1000blocks.db";
		}
		input = new SQLiteDAO(dataPath);
		//Debug.Log(System.Environment.CurrentDirectory);
		planets = new ParticleSystem.Particle[numPlanets];
		InitInArray();
		float increment = 1f / (numPlanets - 1);
        var addresses = input.Addresses.ToList();
		for (int i =0; i<numPlanets; i++) {
			float x = i * increment;
			//planets[i].position = PolarToCartesian(inArray[i]);
			planets[i].color = new Color(Random.Range (0f, 1f), Random.Range (0f, 1f),Random.Range (0f, 1f));
			//planets[i].size = (float)addresses[i].FirstTransaction.Outputs[0].Value/50;
		}
	}
	// Use this for initialization
	void Start () {
		var slash = Path.DirectorySeparatorChar;
		var dataPath = Application.dataPath + slash + "data" + slash + StartVisualization.dataset;
		dao = new SQLiteDAO(dataPath);

		int i = 0;
		float tau = Mathf.PI * 2;


		if (StartVisualization.exp) {
			play = true;
		}
		var numBlocks = StartVisualization.BlocksToShow;
		var addresses =
			from a in dao.Addresses
		/*	where a.FirstOccurrenceBlockHeight <= numBlocks && a.FirstOccurrenceBlockHeight > 0
			orderby a.FirstOccurrenceBlockHeight*/
			select a;
        if (play)
        {
            addresses = addresses.OrderBy(a => a.FirstOccurrenceBlockHeight);
        }
        CoinBase = GameObject.Find("Shockwave");
        CoinBase.AddComponent<OnClickCenterView>();
        planets.Add(CoinBase);
		// TODO: check if ToList makes it better or worse
		foreach (var address in addresses.ToList())
		{



            // init planet
            var balance = 0.0;
            float scaleFactor= 0f;
            if (play)
            {
                balance = address.BalanceAfterBlock(numBlocks);
                scaleFactor = (float) balance / 50;
            }
            else {
                balance = address.Profit;
                scaleFactor = (float)Mathf.Pow((float)address.Profit, 1 / 3f);
            }
			Vector3 scaleVector = new Vector3 (scaleFactor, scaleFactor, scaleFactor);
            Vector3 pizzaScaleVector = new Vector3(scaleFactor, scaleFactor, 1);


            // Pizza Planet
            if (address.ID.Equals("17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ")) {
				Debug.Log ("Found Laszlo's Pizza Address");
				GameObject planet = GameObject.Find ("PizzaBox");
				GameObject planet2 = GameObject.Find ("PizzaLayoutFront");
				GameObject planet3 = GameObject.Find ("PizzaLayoutBack");
				// init position
				positions.Add(PolarToCartesian(new Vector3(Random.Range(100f,500f), Random.Range(0f, tau), Random.Range(0f, tau))));
                planet.transform.position = (Vector3)positions[i];


                planet.transform.position = (Vector3) positions[i];

                planet.AddComponent<OnClickCenterView>();
				planet.GetComponent<OnClickCenterView>().setAddress (address.ID);
				planet.GetComponent<OnClickCenterView>().setBlock (address.FirstOccurrenceBlockHeight);
				planet.GetComponent<OnClickCenterView>().setBalance ((float) address.Profit);

                planet2.AddComponent<OnClickCenterView>();
                planet2.GetComponent<OnClickCenterView>().setAddress (address.ID);
				planet2.GetComponent<OnClickCenterView>().setBlock (address.FirstOccurrenceBlockHeight);
				planet2.GetComponent<OnClickCenterView>().setBalance ((float) address.Profit);

                planet3.AddComponent<OnClickCenterView>();
                planet3.GetComponent<OnClickCenterView>().setAddress (address.ID);
				planet3.GetComponent<OnClickCenterView>().setBlock (address.FirstOccurrenceBlockHeight);
				planet3.GetComponent<OnClickCenterView>().setBalance ((float) address.Profit);

                planet.transform.localScale = (pizzaScaleVector);
                planets.Add (planet);

                planet.GetComponent<Renderer>().enabled = true;
                planet2.GetComponent<Renderer>().enabled = true;
                planet3.GetComponent<Renderer>().enabled = true;

                pizzaBox =i;

				i++;

			}
            
			// Largest Transaction ever
			else if (address.ID.Equals("1M8s2S5bgAzSSzVTeL7zruvMPLvzSkEAuv")) {
				Debug.Log ("Largest Transaction ever");
				GameObject planet = GameObject.Find ("Dollar");
				GameObject planet2 = GameObject.Find("DollarBack");

           

                //planet.GetComponent<Renderer>().material.mainTexture = DollarTexture;
                // init position
                positions.Add(PolarToCartesian(new Vector3(Random.Range(100f, 500f), Random.Range(0f, tau), Random.Range(0f, tau))));
                planet.transform.position = (Vector3)positions[i];

                planet.AddComponent<OnClickCenterView>();
				planet.GetComponent<OnClickCenterView>().setAddress (address.ID);
				planet.GetComponent<OnClickCenterView>().setBlock (address.FirstOccurrenceBlockHeight);
				planet.GetComponent<OnClickCenterView>().setBalance ((float) address.Profit);

                planet2.AddComponent<OnClickCenterView>();
				planet2.GetComponent<OnClickCenterView>().setAddress (address.ID);
				planet2.GetComponent<OnClickCenterView>().setBlock (address.FirstOccurrenceBlockHeight);
				planet2.GetComponent<OnClickCenterView>().setBalance ((float) address.Profit);

               // scaleVector.x = Mathf.Pow(scaleVector.x, 10);
                //scaleVector.y = Mathf.Pow(scaleVector.y, 10);
                //scaleVector.z = Mathf.Pow(scaleVector.z, 10);


                planet.transform.localScale = scaleVector/4;

                planets.Add (planet);
                planets.Add(planet2);

                planet.GetComponent<Renderer>().enabled = true;
                planet2.GetComponent<Renderer>().enabled = true;


                dollarNote = i;

				i++;

			}
			else{
				GameObject planet = GameObject.CreatePrimitive (PrimitiveType.Sphere);

				if(StartVisualization.boringcolors) planet.GetComponent<Renderer>().material.color = Color.grey;
				else planet.GetComponent<Renderer>().material.color = HSVToRGB (Random.Range (0f, 1f), 1f, 1f);

                // init position
                if (StartVisualization.dataset=="first-1000.sqlite")
                {
                    var height = address.FirstOccurrenceBlockHeight;
                    positions.Add(PolarToCartesian(new Vector3((height == 0) ? height/2 : height/2 +10, Random.Range(0f, tau), Random.Range(0f, tau))));
                    if (height == 0)
                    {
                        planet.GetComponent<Renderer>().material.color = Color.yellow;
                    }

                    planet.transform.position = (Vector3)positions[i];

                    planet.AddComponent<OnClickCenterView>();
                    planet.GetComponent<OnClickCenterView>().setAddress(address.ID);
                    planet.GetComponent<OnClickCenterView>().setBlock(address.FirstOccurrenceBlockHeight);
                    planet.GetComponent<OnClickCenterView>().setBalance((float)balance);
                }
                else
                {
                    positions.Add(PolarToCartesian(new Vector3(Random.Range(100f, 500f), Random.Range(0f, tau), Random.Range(0f, tau))));
                    planet.transform.position = (Vector3)positions[i];

                    planet.AddComponent<OnClickCenterView>();
                    planet.GetComponent<OnClickCenterView>().setAddress(address.ID);
                    planet.GetComponent<OnClickCenterView>().setBlock(address.FirstOccurrenceBlockHeight);
                    planet.GetComponent<OnClickCenterView>().setBalance((float)address.Profit);
                }
				planet.transform.localScale = scaleVector;
				planets.Add (planet);
                
				i++;
			}
		}
		numPlanets = i+1;

	}