package space;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

import com.sun.opengl.util.texture.Texture;

import devUtil.TextureLoader;
import devUtil.Vector3;

import world.GameObject;

public class Planet extends GameObject {
	
	Texture texture;
	
	Vector3 position;
	double size;
	
	String level;
	
	double rotation;
	
	public Planet(String texturePath, Vector3 position, double size, String level) {
		
		texture = TextureLoader.getTexture(texturePath);
		
		this.position = position;
		this.size = size / 2;
		
		this.level = level;
		
		triggeredGameState = null;
	}
	
	public void render(GL gl) {
		
		texture.enable();
		texture.bind();
		
		gl.glTranslated(position.getX(), position.getY(), position.getZ());
		gl.glRotated(rotation, 0, 1, 0);
		
		GLUquadric skybox = glu.gluNewQuadric();
		
		glu.gluQuadricTexture(skybox, true);
		glu.gluQuadricDrawStyle(skybox, GLU.GLU_FILL);
		glu.gluQuadricNormals(skybox, GLU.GLU_FLAT);
		glu.gluQuadricOrientation(skybox, GLU.GLU_OUTSIDE);
		
		final int slices = 16;
		final int stacks = 16;
		
		glu.gluSphere(skybox, size, slices, stacks);
		glu.gluDeleteQuadric(skybox);

		gl.glRotated(rotation, 0, -1, 0);
		gl.glTranslated(-position.getX(), -position.getY(), -position.getZ());
		
		texture.disable();
	}
	
	public void update() {
		
		Vector3 space = camera.getPosition();
		space.subtract(position);
		
		if(space.getMagnitude() < size) {
			triggeredGameState = level;
			endScene();
		}
		
		rotation += .15f;
	}
	
	public void interact(GameObject object) {
		
		if(object instanceof Player) {
			
			if(triggeredGameState != null) {
				
				object.destroy();
				endScene();
			}
		}
	}
}