using UnityEngine;
using System.Collections;
using System.Xml;
public class LoadPlanets : MonoBehaviour {
	public GameObject planetTemplate;


	// Use this for initialization
	void Start () {
		LoadAllPlanets();

	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void LoadAllPlanets()
	{
		TextAsset textAsset = (TextAsset) Resources.Load("planets");  
		XmlDocument doc = new XmlDocument ();
		doc.LoadXml ( textAsset.text );
		foreach (XmlNode planet in doc.SelectNodes("planets/planet")) 
		{

			string name, diameter, distancetoSun, rotationPeriod, orbitalVelocity;
			name = planet.Attributes.GetNamedItem ("name").Value;
			diameter = planet.Attributes.GetNamedItem ("diameter").Value;
			distancetoSun = planet.Attributes.GetNamedItem ("distancetoSun").Value;
			rotationPeriod = planet.Attributes.GetNamedItem ("rotationPeriod").Value;
			orbitalVelocity = planet.Attributes.GetNamedItem ("orbitalVelocity").Value;
			Debug.Log ("Planet name: " + name );

			float diameter2, distancetoSun2, rotationPeriod2, orbitalVelocity2;
			diameter2 = float.Parse(diameter);
			distancetoSun2 = float.Parse(distancetoSun);
			rotationPeriod2 = float.Parse(rotationPeriod);
			orbitalVelocity2 = float.Parse(orbitalVelocity);
			print ("Planet" + name + ": Diameter" + diameter2+ ";Distance" + distancetoSun2);

			GameObject g = Instantiate (planetTemplate);
			g.GetComponent<Planet>().SetDistanceToSun(distancetoSun2);
			g.GetComponent<Planet> ().SetOrbitSpeed (orbitalVelocity2);
			g.GetComponent<Planet> ().SetRotationalSpeed (1 / rotationPeriod2);
			g.GetComponent<Planet> ().SetName (name);
			g.GetComponent<Planet> ().SetRadius(diameter2);



		}
	}


}
