package j3deditor.bin.editor;

import j3deditor.bin.components.ButtonPanelLayout;
import j3deditor.bin.hierarchy.util.HierarchyEvent;
import j3deditor.bin.hierarchy.util.HierarchyEventListener;
import j3deditor.bin.components.SimpleButton;
import j3deditor.bin.components.SimpleButtonEvent;
import j3deditor.bin.components.SimpleButtonListener;

import j3deditor.bin.hierarchy.J3DeLight;
import j3deditor.bin.hierarchy.J3DePrimitive;
import j3deditor.bin.hierarchy.J3DeScene;

import j3deditor.bin.hierarchy.J3DeShape3D;

import j3deditor.bin.hierarchy.util.LightType;
import j3deditor.bin.hierarchy.util.PrimitiveType;
import j3deditor.bin.hierarchy.util.Shape3DType;

import j3deditor.bin.util.Utilities;

import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 * Contains buttons for creating 3D objects.
 *
 * @author Risto Seene
 */
public class ObjectPanel extends JPanel implements SimpleButtonListener{


	/** the scene this panel adds objects to */
	private J3DeScene node;

	/** listener that listens hierarchy manipulation events */
	private HierarchyEventListener listener;


	/** button for creating <code>ColorCube</code> */
	private SimpleButton colorcube;


	/** button for creating <code>Box</code> */
	private SimpleButton box;

	/** button for creating <code>Cone</code> */
	private SimpleButton cone;

	/** button for creating <code>Sphere</code> */
	private SimpleButton sphere;

	/** button for creating <code>Cylinder</code> */
	private SimpleButton cylinder;


	/** button for creating <code>AmbientLight</code> */
	private SimpleButton alight;

	/** button for creating <code>DirectionalLight</code> */
	private SimpleButton dlight;

	/** button for creating <code>PointLight</code> */
	private SimpleButton plight;

	/** button for creating <code>SpotLight</code> */
	private SimpleButton slight;


	/** infopanel that shows active button info */
	private InfoPanel infoPanel = null;


	/**
	 * Creates an instance of <code>objectPanel</code>.
	 *
	 * @param node
	 *	root node of the node this panel adds objects to
	 */
	public ObjectPanel(J3DeScene node){
		this.node = node;

		initButtons();
	}


	/**
	 * Initializes all buttons
	 */
	private void initButtons(){
		setOpaque(false);
		setLayout(new ButtonPanelLayout(40, 50, 0, 10, ButtonPanelLayout.HORIZONTAL, ButtonPanelLayout.POSITIVE));

		colorcube = new SimpleButton(SimpleButton.CLICK);
		colorcube.setButtonInfo(Utilities.translator.translate(this.getClass(), "colorcube"));
		colorcube.setImage("colorcube.png", SimpleButton.NORMAL_IMAGE);
		colorcube.setImage("colorcube_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		colorcube.setImage("colorcube_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		colorcube.setImage("box_off.png", SimpleButton.DISABLED_IMAGE);
		colorcube.addSimpleButtonListener(this);
		add(colorcube);

		add(new JLabel());

		box = new SimpleButton(SimpleButton.CLICK);
		box.setButtonInfo(Utilities.translator.translate(this.getClass(), "box"));
		box.setImage("box.png", SimpleButton.NORMAL_IMAGE);
		box.setImage("box_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		box.setImage("box_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		box.setImage("box_off.png", SimpleButton.DISABLED_IMAGE);
		box.addSimpleButtonListener(this);
		add(box);

		cylinder = new SimpleButton(SimpleButton.CLICK);
		cylinder.setButtonInfo(Utilities.translator.translate(this.getClass(), "cylinder"));
		cylinder.setImage("cylinder.png", SimpleButton.NORMAL_IMAGE);
		cylinder.setImage("cylinder_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		cylinder.setImage("cylinder_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		cylinder.setImage("cylinder_off.png", SimpleButton.DISABLED_IMAGE);
		cylinder.addSimpleButtonListener(this);
		add(cylinder);

		cone = new SimpleButton(SimpleButton.CLICK);
		cone.setButtonInfo(Utilities.translator.translate(this.getClass(), "cone"));
		cone.setImage("cone.png", SimpleButton.NORMAL_IMAGE);
		cone.setImage("cone_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		cone.setImage("cone_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		cone.setImage("cone_off.png", SimpleButton.DISABLED_IMAGE);
		cone.addSimpleButtonListener(this);
		add(cone);

		sphere = new SimpleButton(SimpleButton.CLICK);
		sphere.setButtonInfo(Utilities.translator.translate(this.getClass(), "sphere"));
		sphere.setImage("sphere.png", SimpleButton.NORMAL_IMAGE);
		sphere.setImage("sphere_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		sphere.setImage("sphere_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		sphere.setImage("sphere_off.png", SimpleButton.DISABLED_IMAGE);
		sphere.addSimpleButtonListener(this);
		add(sphere);

		add(new JLabel());

		alight = new SimpleButton(SimpleButton.CLICK);
		alight.setButtonInfo(Utilities.translator.translate(this.getClass(), "amblight"));
		alight.setImage("ambientlight.png", SimpleButton.NORMAL_IMAGE);
		alight.setImage("ambientlight_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		alight.setImage("ambientlight_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		//alight.setImage("ambientlight_off.png", SimpleButton.DISABLED_IMAGE);
		alight.addSimpleButtonListener(this);
		add(alight);

		dlight = new SimpleButton(SimpleButton.CLICK);
		dlight.setButtonInfo(Utilities.translator.translate(this.getClass(), "dirlight"));
		dlight.setImage("directionallight.png", SimpleButton.NORMAL_IMAGE);
		dlight.setImage("directionallight_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		dlight.setImage("directionallight_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		//dlight.setImage("directionallight_off.png", SimpleButton.DISABLED_IMAGE);
		dlight.addSimpleButtonListener(this);
		add(dlight);

		plight = new SimpleButton(SimpleButton.CLICK);
		plight.setButtonInfo(Utilities.translator.translate(this.getClass(), "pointlight"));
		plight.setImage("pointlight.png", SimpleButton.NORMAL_IMAGE);
		plight.setImage("pointlight_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		plight.setImage("pointlight_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		//plight.setImage("pointlight_off.png", SimpleButton.DISABLED_IMAGE);
		plight.addSimpleButtonListener(this);
		add(plight);

		slight = new SimpleButton(SimpleButton.CLICK);
		slight.setButtonInfo(Utilities.translator.translate(this.getClass(), "spotlight"));
		slight.setImage("spotlight.png", SimpleButton.NORMAL_IMAGE);
		slight.setImage("spotlight_1.png", SimpleButton.MOUSE_OVER_IMAGE);
		slight.setImage("spotlight_2.png", SimpleButton.MOUSE_DOWN_IMAGE);
		//slight.setImage("spotlight_off.png", SimpleButton.DISABLED_IMAGE);
		slight.addSimpleButtonListener(this);
		add(slight);
	}


	/**
	 * Sets the listener this panel can send hierarchy manipulation events to.
	 *
	 * @param listener
	 *	object which listens hierarchy manipulation events sent by this panel
	 */
	public void setEditorEventListener(HierarchyEventListener listener){
		this.listener = listener;
	}


	/**
	 * Listens the mouse click events sent by <code>SimpleButton</code> objects.
	 *
	 * @param e
	 *	event sent by <code>SimpleButton</code>
	 */
	public void buttonClicked(SimpleButtonEvent e){
		if(e.getSource() == colorcube){
			node.addNewObject(new J3DeShape3D(Shape3DType.COLORCUBE));
		} else if(e.getSource() == box){
			node.addNewObject(new J3DePrimitive(PrimitiveType.BOX));
		} else if(e.getSource() == cone){
			node.addNewObject(new J3DePrimitive(PrimitiveType.CONE));
		} else if(e.getSource() == sphere){
			node.addNewObject(new J3DePrimitive(PrimitiveType.SPHERE));
		} else if(e.getSource() == cylinder){
			node.addNewObject(new J3DePrimitive(PrimitiveType.CYLINDER));
		} else if(e.getSource() == alight){
			node.addNewObject(new J3DeLight(LightType.AMBIENTLIGHT));
		} else if(e.getSource() == dlight){
			node.addNewObject(new J3DeLight(LightType.DIRECTIONALLIGHT));
		} else if(e.getSource() == plight){
			node.addNewObject(new J3DeLight(LightType.POINTLIGHT));
		} else if(e.getSource() == slight){
			node.addNewObject(new J3DeLight(LightType.SPOTLIGHT));
		}


		if(listener != null){
			listener.hierarchyEventOccured(new HierarchyEvent(this, HierarchyEvent.OBJECT_ADDITION));
		}
	}


	/**
	 * Not in use.
	 * 
	 * @param e
	 * 	doesn't do anything
	 */
	public void buttonDragged(SimpleButtonEvent e){
	}


	/**
	 * Listens the mouse enter events sent by <code>SimpleButton</code> objects.
	 *
	 * @param e
	 *	event sent by <code>SimpleButton</code>
	 */
	public void buttonEntered(SimpleButtonEvent e){
		if(infoPanel != null){
			infoPanel.setInfo(e.getSource().getButtonInfo());
		}
	}


	/**
	 * Listens the mouse exit events sent by <code>SimpleButton</code> objects.
	 *
	 * @param e
	 *	event sent by <code>SimpleButton</code>
	 */
	public void buttonExited(SimpleButtonEvent e){
		if(infoPanel != null){
			infoPanel.setInfo(null, null);
		}
	}


	/**
	 * Not in use.
	 * 
	 * @param e
	 * 	doesn't do anything
	 */
	public void buttonDragEnded(SimpleButtonEvent e){
	}


	/**
	 * Sets the infopanel which shows information about the buttons of this panel.
	 *
	 * @param infoPanel
	 *	infopanel which shows information about the buttons of this panel
	 */
	public void setInfoListener(InfoPanel infoPanel){
		this.infoPanel = infoPanel;
	}

}
