package j3deditor.bin.editor.tools;

import j3deditor.bin.hierarchy.util.HierarchyEvent;
import j3deditor.bin.hierarchy.util.HierarchyEventListener;

import j3deditor.bin.hierarchy.J3DeObject;

import j3deditor.bin.util.Utilities;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;


/**
 * User interface for manipulating  the attributes of
 * active <code>J3DeObject</code> node.
 *
 * @author Risto Seene
 */
public class ObjPanel extends JPanel implements ActionListener, UpdateblePanel{


	/** <code>J3DeObject</code> this panel manipulates */
	private J3DeObject object;

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


	/**
	 * @see J3DeObject#getTranslationX()
	 * @see J3DeObject#translate(float x, float y, float z)
	 */
	private JTextField posX;

	/**
	 * @see J3DeObject#getTranslationY()
	 * @see J3DeObject#translate(float x, float y, float z)
	 */
	private JTextField posY;

	/**
	 * @see J3DeObject#getTranslationZ()
	 * @see J3DeObject#translate(float x, float y, float z)
	 */
	private JTextField posZ;


	/**
	 * @see J3DeObject#getScaleX()
	 * @see J3DeObject#scale(float x, float y, float z)
	 */
	private JTextField sclX;

	/**
	 * @see J3DeObject#getScaleY()
	 * @see J3DeObject#scale(float x, float y, float z)
	 */
	private JTextField sclY;

	/**
	 * @see J3DeObject#getScaleZ()
	 * @see J3DeObject#scale(float x, float y, float z)
	 */
	private JTextField sclZ;


	/**
	 * @see J3DeObject#getRotationX()
	 * @see J3DeObject#rotate(float x, float y, float z)
	 */
	private JTextField rotX;

	/**
	 * @see J3DeObject#getRotationY()
	 * @see J3DeObject#rotate(float x, float y, float z)
	 */
	private JTextField rotY;

	/**
	 * @see J3DeObject#getRotationZ()
	 * @see J3DeObject#rotate(float x, float y, float z)
	 */
	private JTextField rotZ;


	/**
	 * Creates an instance of <code>ObjPanel</code>
	 * that manipulates the given <code>J3DeObject</code> node.
	 *
	 * @param object
	 *	hierarchy node this panel manipulates
	 * @param listener
	 *	listener that listens hierarchy manipulation events
	 *	made by this panel
	 */
	public ObjPanel(J3DeObject object, HierarchyEventListener listener){
		this.object = object;
		this.listener = listener;

		setLayout(new BorderLayout());
		setOpaque(false);
		initContent();
	}


	/**
	 * Initializes the content of this panel
	 */
	private void initContent(){
		add(new JSeparator(), BorderLayout.NORTH);


		Container tools = new Container();
		tools.setLayout(new GridLayout(3, 4, 5, 5));
		add(tools, BorderLayout.CENTER);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "pos"), 4));
		posX = new JTextField();
		posX.addActionListener(this);
		posX.setToolTipText(Utilities.translator.translate(this.getClass(), "x"));
		tools.add(posX);
		posY = new JTextField();
		posY.addActionListener(this);
		posY.setToolTipText(Utilities.translator.translate(this.getClass(), "y"));
		tools.add(posY);
		posZ = new JTextField();
		posZ.addActionListener(this);
		posZ.setToolTipText(Utilities.translator.translate(this.getClass(), "z"));
		tools.add(posZ);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "scl"), 4));
		sclX = new JTextField();
		sclX.addActionListener(this);
		sclX.setToolTipText(Utilities.translator.translate(this.getClass(), "x"));
		tools.add(sclX);
		sclY = new JTextField();
		sclY.addActionListener(this);
		sclY.setToolTipText(Utilities.translator.translate(this.getClass(), "y"));
		tools.add(sclY);
		sclZ = new JTextField();
		sclZ.addActionListener(this);
		sclZ.setToolTipText(Utilities.translator.translate(this.getClass(), "z"));
		tools.add(sclZ);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "rot"), 4));
		rotX = new JTextField();
		rotX.addActionListener(this);
		rotX.setToolTipText(Utilities.translator.translate(this.getClass(), "x"));
		tools.add(rotX);
		rotY = new JTextField();
		rotY.addActionListener(this);
		rotY.setToolTipText(Utilities.translator.translate(this.getClass(), "y"));
		tools.add(rotY);
		rotZ = new JTextField();
		rotZ.addActionListener(this);
		rotZ.setToolTipText(Utilities.translator.translate(this.getClass(), "z"));
		tools.add(rotZ);

		updateValues();
	}


	/**
	 * Listens action events sent by controls attached to this panel.
	 *
	 * @param e
	 *	event sent by a control button
	 */
	public void actionPerformed(ActionEvent e){
		try{
			if(e.getSource() == posX || e.getSource() == posY || e.getSource() == posZ){
				float pX = Float.valueOf(posX.getText()).floatValue();
				float pY = Float.valueOf(posY.getText()).floatValue();
				float pZ = Float.valueOf(posZ.getText()).floatValue();
				object.translate(pX, pY, pZ);
			} else if(e.getSource() == sclX || e.getSource() == sclY || e.getSource() == sclZ){
				float sX = Float.valueOf(sclX.getText()).floatValue();
				float sY = Float.valueOf(sclY.getText()).floatValue();
				float sZ = Float.valueOf(sclZ.getText()).floatValue();
				object.scale(sX, sY, sZ);
			} else if(e.getSource() == rotX || e.getSource() == rotY || e.getSource() == rotZ){
				float rX = Float.valueOf(rotX.getText()).floatValue();
				float rY = Float.valueOf(rotY.getText()).floatValue();
				float rZ = Float.valueOf(rotZ.getText()).floatValue();
				object.rotate(rX, rY, rZ);
			}
		} catch(Throwable t){
		}
		if(listener != null){
			listener.hierarchyEventOccured(new HierarchyEvent(this, HierarchyEvent.OBJECT_TRANSFORMATION));
		}
	}


	/**
	 * Updates all values in text fields
	 * if the transformation of the object has occured.
	 */
	public void updateValues(){
		posX.setText(object.getTranslationX() + "");
		posY.setText(object.getTranslationY() + "");
		posZ.setText(object.getTranslationZ() + "");
		sclX.setText(object.getScaleX() + "");
		sclY.setText(object.getScaleY() + "");
		sclZ.setText(object.getScaleZ() + "");
		rotX.setText(object.getRotationX() + "");
		rotY.setText(object.getRotationY() + "");
		rotZ.setText(object.getRotationZ() + "");
	}

}
