package j3deditor.bin.hierarchy;


import j3deditor.bin.hierarchy.util.HNodeType;
import j3deditor.bin.hierarchy.util.PrimitiveType;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;

import javax.media.j3d.Appearance;
import javax.media.j3d.Node;


/**
 * Contains and manages an instance of
 * <code>com.sun.j3d.utils.geometry.Primitive</code>.
 *
 * @author Risto Seene
 * 
 * @see com.sun.j3d.utils.geometry.Primitive
 */
public class J3DePrimitive extends J3DeObject{

	/** actual <code>Primitive</code> object */
	private Primitive object;

	/** appearance of this primitive object */
	private J3DeAppearance appearance;

	/** number of <code>J3DePrimitive</code> nodes */
	private static long numOfNodes = 0;


	/**
	 * Creates an empty <code>J3DePrimitive</code> object.
	 * Requires initialization with <code>init3DObject()</code> method
	 */
	public J3DePrimitive(){
		super("Primitive", HNodeType.PRIMITIVE, numOfNodes);
		numOfNodes++;
	}


	/**
	 * Creates an instance of <code>J3DePrimitive</code> with the given type.
	 *
	 * @param type
	 *	the type of the primitive
	 */
	public J3DePrimitive(PrimitiveType type){
		super("Primitive" + "[" + numOfNodes + "]_" + type, HNodeType.PRIMITIVE, numOfNodes);
		numOfNodes++;

		if(type == PrimitiveType.BOX){
			object = new Box();
			init3DObject(object);
		} else if(type == PrimitiveType.CONE){
			object = new Cone();
			init3DObject(object);
		} else if(type == PrimitiveType.SPHERE){
			object = new Sphere();
			init3DObject(object);
		} else if(type == PrimitiveType.CYLINDER){
			object = new Cylinder();
			init3DObject(object);
		}
	}


	/**
	 * Applies the given <code>Node</code> object to this node.
	 *
	 * @param obj
	 *	object to be applied to this node
	 */
	public void init3DObject(Node obj){
		object = (Primitive)obj;
		initObject(object);

		if(object.getAppearance() == null){
			object.setAppearance(new Appearance());
		}
		appearance = new J3DeAppearance(object.getAppearance());
		appearance.setParent(this);
	}


	/**
	 * Clones this node.
	 *
	 * @param node
	 *	object sent by subclass, otherwise <code>null</code>
	 * @return Returns the clone of this node
	 */
	public HierarchyNode clone(HierarchyNode node){
		if(node == null){
			node = new J3DePrimitive();
		}

		node = super.clone(node);
		((J3DePrimitive)node).init3DObject(((J3DeObject)node).get3DObject());
		return node;
	}


	/**
	 * Returns the child at the specified index.
	 *
	 * @param index
	 *	index of child
	 * @return Returns the child with the specified index
	 */
	public HierarchyNode getChild(int index){
		if(appearance != null){
			return appearance.getChild(index);
		}
		return null;
	}


	/**
	 * Returns the number of children.
	 *
	 * @return Returns the number of children as integer
	 */
	public int getNumberOfChildren(){
		if(appearance != null){
			return appearance.getNumberOfChildren();
		}
		return 0;
	}


	/**
	 * Returns the index of specified child.
	 *
	 * @param child
	 *	child node which index will be returned
	 * @return Returns the index of specified child
	 */
	public int getIndexOfChild(HierarchyNode child){
		if(appearance != null){
			return appearance.getIndexOfChild(child);
		}
		return 0;
	}


	/**
	 * Returns the appearance object of this node.
	 *
	 * @return Returns the appearance object of this node
	 */
	public J3DeAppearance getAppearance(){
		return appearance;
	}

}
