package net.mkonrad.synes.gui.elements;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.LineArray;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import net.mkonrad.synes.gui.style.Colors;

import com.sun.j3d.utils.geometry.Sphere;

public class HelperLines
{
	private final int _size;
	private BranchGroup _bg;
	
	public HelperLines(int size)
	{
		_size = size;
		
		_bg = new BranchGroup();
		//_bg.setPickable(false);
		
		_bg.addChild(getCenterSphere());
		_bg.addChild(getAxis());
	}

  public BranchGroup getBG()
  {
  	return _bg;
  }
  
  private TransformGroup getCenterSphere()
  {
  	//make appearance
  	Appearance app = new Appearance();
  	Material mat = new Material();
  	mat.setAmbientColor(Colors.white);
  	app.setMaterial(mat);
  	
  	//make position
    TransformGroup tg = new TransformGroup();
    Sphere sphere = new Sphere(0.1f, app);
    sphere.setPickable(false);
    tg.addChild(sphere);
    
    return tg;
  }
  
  
  private TransformGroup getAxis()
  {
  	Shape3D axis = new Shape3D();
  	
  	LineArray lines = new LineArray(6, 
				LineArray.COORDINATES | LineArray.COLOR_3 );
  	
  	axis.setPickable(false);
  	
  	//make coords
  	Point3f[] points = {
  		new Point3f(-_size / 2, 0, 0), new Point3f(_size / 2, 0, 0),
  		new Point3f(0, -_size / 2, 0), new Point3f(0, _size / 2, 0),
  		new Point3f(0, 0, -_size / 2), new Point3f(0, 0, _size / 2)
  	};
  	
  	lines.setCoordinates(0, points);
  	
  	//make colors
  	Color3f red 	= new Color3f(1.0f, 0.0f, 0.0f);
  	Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
  	Color3f blue 	= new Color3f(0.0f, 0.0f, 1.0f);
  	
  	Color3f[] colors = {
  		red, red,
  		green, green,
  		blue, blue
  	};
  	
  	lines.setColors(0, colors);
  	
  	//set geometry data
  	axis.setGeometry(lines);
  	
  	//make position
    TransformGroup tg = new TransformGroup();
    tg.addChild(axis);
    
    return tg;
  }
}
