using UnityEngine;
using System.Collections;

public class PlanetarController : MonoBehaviour {

	public PlanetarMesh planetarMesh = null;

	SphereCollider planetCollider = null;

	public static PlanetarController Generate(Transform transform, StarMap.SpaceObject props)
	{
		GameObject planetarObject = new GameObject (props.name);
		planetarObject.transform.parent = transform;
		PlanetarController controller = planetarObject.AddComponent<PlanetarController>();
		controller.GeneratePlanet (props);
		controller.planetarMesh.transform.localPosition = new Vector3(props.distance, 0);
		if (props.type != SpaceObjectType.star)
		{
			controller.planetCollider = controller.planetarMesh.gameObject.AddComponent<SphereCollider> ();
			controller.planetCollider.radius = props.size * 1.5f;
		}
		Material orbitalLineMaterial = Resources.Load <Material> ("Meshes/Planets/Materials/planet_line");
		OrbitalLine.Create(controller.gameObject, props.distance, props.angle, orbitalLineMaterial);
		return controller;
	}

	void Start () {
	}

	PlanetarMesh GeneratePlanet(StarMap.SpaceObject planet)
	{
		GameObject planetarObject = new GameObject ();
		planetarObject.transform.parent = transform;
		planetarMesh = planetarObject.AddComponent<PlanetarMesh>();
		GradientColorKey[] colorKeys = new GradientColorKey[planet.colorKeys.Length];
		for (int i = 0; i < planet.colorKeys.Length; i++)
		{
			colorKeys[i] = new GradientColorKey(planet.colorKeys[i], (float)i / (float)planet.colorKeys.Length);
		}
		planetarMesh.groundColor.SetKeys (colorKeys, planetarMesh.groundColor.alphaKeys);
		planetarMesh.radius = planet.size;
		planetarMesh.textureSize = 64;
		planetarMesh.isGasGiant = (planet.type == SpaceObjectType.gasGiant);
		planetarMesh.details = 6;
		planetarMesh.isUnlit = planet.type == SpaceObjectType.star;
		return planetarMesh;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
