package sg.env;

import sg.util.SGUtil;

import com.jme.image.Texture;
import com.jme.light.LightNode;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Skybox;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jmex.effects.LensFlare;
import com.jmex.effects.LensFlareFactory;

public class Environment extends Node {
	private static final long serialVersionUID = 1L;
	
	/** A Node that follows the player, contains only 
	 * things that the player can't interact with 
	 * */
	private Node localNode;

	public Environment(Node parentNode){
		buildLocalNode();
		buildEnvoriment();
	}
	
	private void buildEnvoriment(){
		Node station = SGUtil.loadModel("sg/data/models/stations/station.3ds");
		station.setLocalTranslation(new Vector3f( 500, 0, 0));
		this.attachChild(station);
		
		//Node station2 = SGUtil.loadModel("sg/data/models/stations/station2.obj");
		//station2.setLocalScale(0.5f);
		//station2.setLocalTranslation(new Vector3f( -500, 0, 0));
		//this.attachChild(station2);
	}
	
	private void buildLocalNode() {
		localNode = new Node("local_space");
		
		// Skybox
		Skybox space = new Skybox("space", 3000, 3000, 3000);
		Texture north = SGUtil.loadTexture("sg/data/skybox/advanced/front.png");
		Texture rest = SGUtil.loadTexture("sg/data/skybox/advanced/rest.png");

		space.setTexture(Skybox.Face.North, north);
		space.setTexture(Skybox.Face.West, rest);
		space.setTexture(Skybox.Face.South, rest);
		space.setTexture(Skybox.Face.East, rest);
		space.setTexture(Skybox.Face.Up, rest);
		space.setTexture(Skybox.Face.Down, rest);
		space.preloadTextures();
		space.updateRenderState();
		localNode.attachChild(space);
		
		// Lights        
        LightNode lightNode = new LightNode("space_light_node");
        
		// Setup the lens flare textures.
        TextureState[] tex = new TextureState[4];
        tex[0] = SGUtil.getTextureState("jmetest/data/texture/flare1.png");
        tex[1] = SGUtil.getTextureState("jmetest/data/texture/flare2.png");
        tex[2] = SGUtil.getTextureState("jmetest/data/texture/flare3.png");
        tex[3] = SGUtil.getTextureState("jmetest/data/texture/flare4.png");
        LensFlare flare = LensFlareFactory.createBasicLensFlare("sunflare", tex);
        flare.setRootNode(this);
        flare.setLocalTranslation( 0, 0, 5000);
        lightNode.attachChild(flare);
        lightNode.updateRenderState();
        
        // attach the nodes
        localNode.attachChild(lightNode);        
        this.attachChild(localNode);
	}
	
	public void update(){
		localNode.setLocalTranslation(
				DisplaySystem.getDisplaySystem().getRenderer().getCamera().getLocation());
	}
}
