package j3deditor.bin.editor.tools;

import j3deditor.bin.hierarchy.J3DeMaterial;

import j3deditor.bin.util.Utilities;

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

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


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


	/** <code>J3DeMaterial</code> this panel manipulates */
	private J3DeMaterial material;


	/** @see J3DeMaterial#setLightingEnable(boolean state) */
	private JCheckBox enable;

	/** @see J3DeMaterial#setColorTarget(int colorTarget) */
	private JComboBox target;

	/** @see J3DeMaterial#setDiffuseColor(Color color) */
	private JButton dif;

	/** @see J3DeMaterial#setAmbientColor(Color color) */
	private JButton amb;

	/** @see J3DeMaterial#setSpecularColor(Color color) */
	private JButton spec;

	/** @see J3DeMaterial#setEmissiveColor(Color color) */
	private JButton emis;

	/** @see J3DeMaterial#setShininess(int value) */
	private JSlider shin;


	/**
	 * Creates an instance of <code>MaterialPanel</code>
	 * that manipulates the given <code>J3DeMaterial</code> node.
	 *
	 * @param material
	 *	hierarchy node this panel manipulates
	 */
	public MaterialPanel(J3DeMaterial material){
		this.material = material;

		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);

		enable = new JCheckBox(Utilities.translator.translate(this.getClass(), "enable"), material.getLightingEnable());
		enable.addActionListener(this);
		enable.setOpaque(false);
		tools.add(enable);

		tools.add(new JLabel());
		tools.add(new JLabel());
		tools.add(new JLabel());

		dif = new JButton(Utilities.translator.translate(this.getClass(), "diffuse"));
		dif.setForeground(material.getDiffuseColor());
		dif.addActionListener(this);
		tools.add(dif);

		amb = new JButton(Utilities.translator.translate(this.getClass(), "ambient"));
		amb.setForeground(material.getAmbientColor());
		amb.addActionListener(this);
		tools.add(amb);

		spec = new JButton(Utilities.translator.translate(this.getClass(), "specular"));
		spec.setForeground(material.getSpecularColor());
		spec.addActionListener(this);
		tools.add(spec);

		emis = new JButton(Utilities.translator.translate(this.getClass(), "emissive"));
		emis.setForeground(material.getEmissiveColor());
		emis.addActionListener(this);
		tools.add(emis);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "shininess"), 4));
		shin = new JSlider(10, 1280, material.getShininess());
		shin.addChangeListener(this);
		shin.setOpaque(false);
		tools.add(shin);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "target"), 4));
		target = new JComboBox(material.getColorTargetItems());
		target.setSelectedIndex(material.getColorTarget());
		target.addActionListener(this);
		tools.add(target);

		enableAll();
	}


	/**
	 * Enables/disables all controls (except <code>enable</code>)
	 */
	private void enableAll(){
		boolean state = enable.getModel().isSelected();
		target.setEnabled(state);
		dif.setEnabled(state);
		amb.setEnabled(state);
		spec.setEnabled(state);
		emis.setEnabled(state);
		shin.setEnabled(state);
	}


	/**
	 * Listens action events sent by control buttons attached to this panel.
	 *
	 * @param e
	 *	event sent by a control button
	 */
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == enable){
			material.setLightingEnable(enable.getModel().isSelected());
			enableAll();
		} else if(e.getSource() == dif){
			String title = Utilities.translator.translate(this.getClass(), "diffuse");
			Color color = material.getDiffuseColor();
			material.setDiffuseColor(new JColorChooser().showDialog(new JFrame(), title, color));
			dif.setForeground(material.getDiffuseColor());
		} else if(e.getSource() == amb){
			String title = Utilities.translator.translate(this.getClass(), "ambient");
			Color color = material.getAmbientColor();
			material.setAmbientColor(new JColorChooser().showDialog(new JFrame(), title, color));
			amb.setForeground(material.getAmbientColor());
		} else if(e.getSource() == spec){
			String title = Utilities.translator.translate(this.getClass(), "specular");
			Color color = material.getSpecularColor();
			material.setSpecularColor(new JColorChooser().showDialog(new JFrame(), title, color));
			spec.setForeground(material.getSpecularColor());
		} else if(e.getSource() == emis){
			String title = Utilities.translator.translate(this.getClass(), "emissive");
			Color color = material.getEmissiveColor();
			material.setEmissiveColor(new JColorChooser().showDialog(new JFrame(), title, color));
			emis.setForeground(material.getEmissiveColor());
		} else if(e.getSource() == target){
			material.setColorTarget(target.getSelectedIndex());
		}
	}


	/**
	 * Listens action events sent by sliders attached to this panel.
	 *
	 * @param e
	 *	event sent by a sliders
	 */
	public void stateChanged(ChangeEvent e){
		if(e.getSource() == shin){
			material.setShininess(shin.getValue());
		}
	}

}
