package org.concord.energy3d.model;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.concord.energy3d.scene.Scene;
import org.concord.energy3d.util.SelectUtil;

import com.ardor3d.extension.model.collada.jdom.ColladaAnimUtils;
import com.ardor3d.extension.model.collada.jdom.ColladaImporter;
import com.ardor3d.extension.model.collada.jdom.ColladaMaterialUtils;
import com.ardor3d.extension.model.collada.jdom.data.ColladaStorage;
import com.ardor3d.math.Vector3;
import com.ardor3d.renderer.queue.RenderBucketType;
import com.ardor3d.renderer.state.BlendState;
import com.ardor3d.renderer.state.BlendState.TestFunction;
import com.ardor3d.scenegraph.Spatial;
import com.ardor3d.scenegraph.extension.BillboardNode;
import com.ardor3d.scenegraph.shape.Quad;
import com.ardor3d.util.resource.ResourceLocatorTool;
import com.ardor3d.util.resource.ResourceSource;

public class Tree extends HousePart {
	private static final long serialVersionUID = 1L;
	private static Spatial treeModel;
	private transient Spatial model;
	private transient BillboardNode billboard; 
	
	public static void loadModel() {
		new Thread() {
			@Override
			public void run() {
				System.out.print("Loading tree model...");
				Thread.yield();
				final ResourceSource source = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, "tree.dae");
				final ColladaImporter colladaImporter = new ColladaImporter();
				Logger.getLogger(ColladaAnimUtils.class.getName()).setLevel(Level.SEVERE);
				Logger.getLogger(ColladaMaterialUtils.class.getName()).setLevel(Level.SEVERE);
				ColladaStorage storage;
				try {
					storage = colladaImporter.load(source);
					treeModel = storage.getScene();
				} catch (final IOException e) {
					e.printStackTrace();
				}
				System.out.println("done");
			}
		}.start();
	}
	
	public Tree() {
		super(1, 1, 1);
		init();
	}
	
	@Override
	protected void init() {
		super.init();
//		model = treeModel.makeCopy(true);
		
		mesh = new Quad("Tree Quad", 30, 30);
//		mesh.setRotation(new Matrix3().fromAngles(Math.PI / 2, 0, 0));		
//		final BlendState rs = new BlendState();
//		rs.setBlendEnabled(true);
//		rs.setTestEnabled(true);
//		mesh.getSceneHints().setRenderBucketType(RenderBucketType.Transparent);
		
		
        final BlendState bs = new BlendState();
        bs.setEnabled(true);
        bs.setBlendEnabled(false);
        bs.setTestEnabled(true);
        bs.setTestFunction(TestFunction.GreaterThan);
        bs.setReference(0.7f);		
        mesh.setRenderState(bs);
        mesh.getSceneHints().setRenderBucketType(RenderBucketType.Transparent);
		
        billboard = new BillboardNode("Billboard");
//        billboard.setAlignment(BillboardAlignment.AxialZ);
		billboard.attachChild(mesh);
		root.attachChild(billboard);		
//        root.attachChild(mesh);
	}
	
	@Override
	public void setPreviewPoint(final int x, final int y) {
		final int index = 0;
		final PickedHousePart pick = SelectUtil.pickPart(x, y, (Spatial) null);
		Vector3 p = points.get(index);
		if (pick != null) {
			p = pick.getPoint();
			snapToGrid(p, getAbsPoint(index), getGridSize());
			p.setZ(15);
			points.get(index).set(p);
		}
		draw();
		setEditPointsVisible(true);				
	}
	
	@Override
	protected boolean mustHaveContainer() {
		return false;
	}	
	
	@Override
	public boolean isDrawable() {
		return true;
	}
	
	@Override
	protected void drawMesh() {		
		billboard.setTranslation(getAbsPoint(0));
//		mesh.setTranslation(getAbsPoint(0));
	}

	@Override
	protected String getTextureFileName() {
		return "tree.png";
	}

	@Override
	public void updateTextureAndColor() {
		updateTextureAndColor(mesh, Scene.getInstance().getWallColor());
	}
	
}

@@ -490,24 +490,58 @@
 daStorage;%0D%0A
+import com.ardor3d.math.Matrix3;%0D%0A
 import com.a
@@ -814,24 +814,99 @@
 boardNode;%0D%0A
+import com.ardor3d.scenegraph.extension.BillboardNode.BillboardAlignment;%0D%0A
 import com.a
@@ -2205,26 +2205,24 @@
 , 30, 30);%0D%0A
-//
 %09%09mesh.setRo
@@ -2281,195 +2281,8 @@
 %09%09%0D%0A
-//%09%09final BlendState rs = new BlendState();%0D%0A//%09%09rs.setBlendEnabled(true);%0D%0A//%09%09rs.setTestEnabled(true);%0D%0A//%09%09mesh.getSceneHints().setRenderBucketType(RenderBucketType.Transparent);%0D%0A%09%09%0D%0A
 %09%09%0D%0A
@@ -2691,18 +2691,16 @@
 ard%22);%0D%0A
-//
         

Fix problem with Ardor3D billboard