package j3deditor.bin.editor.tools;

import j3deditor.bin.hierarchy.J3DeLight;

import j3deditor.bin.util.Utilities;

import java.awt.BorderLayout;
import java.awt.Color;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


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


	/** <code>J3DeAppearance</code> this panel manipulates */
	private J3DeLight light;


	/** @see J3DeLight#setEnable(boolean state) */
	private JCheckBox enable;

	/** @see J3DeLight#setColor(Color color) */
	private JButton color;

	/** @see J3DeLight#setAttenuation(int constant, int linear, int quadratic) */
	private JSlider atn1;

	/** @see J3DeLight#setAttenuation(int constant, int linear, int quadratic) */
	private JSlider atn2;

	/** @see J3DeLight#setAttenuation(int constant, int linear, int quadratic) */
	private JSlider atn3;

	/** @see J3DeLight#setConcentration(int conc) */
	private JSlider conc;

	/** @see J3DeLight#setSpreadAngle(int spread) */
	private JSlider spread;


	/** @see J3DeLight#setBoundsRadius(float radius) */
	private JTextField radius;

	/** @see J3DeLight#setBoundsPosition(float x, float y, float z) */
	private JTextField posX;

	/** @see J3DeLight#setBoundsPosition(float x, float y, float z) */
	private JTextField posY;

	/** @see J3DeLight#setBoundsPosition(float x, float y, float z) */
	private JTextField posZ;


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

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


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


		JPanel tools = new JPanel();
		tools.setLayout(new GridLayout(3, 4, 5, 5));

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

		tools.add(new JLabel());
		color = new JButton(Utilities.translator.translate(this.getClass(), "color"));
		color.setForeground(light.getColor());
		color.addActionListener(this);
		tools.add(color);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "atn"), 4));
		int atn[] = light.getAttenuation();
		atn1 = new JSlider(0, 1, atn[0]);
		atn1.addChangeListener(this);
		atn1.setToolTipText(Utilities.translator.translate(this.getClass(), "atn1"));
		atn1.setOpaque(false);
		tools.add(atn1);
		atn2 = new JSlider(0, 1, atn[1]);
		atn2.addChangeListener(this);
		atn2.setToolTipText(Utilities.translator.translate(this.getClass(), "atn2"));
		atn2.setOpaque(false);
		tools.add(atn2);
		atn3 = new JSlider(0, 1, atn[2]);
		atn3.addChangeListener(this);
		atn3.setToolTipText(Utilities.translator.translate(this.getClass(), "atn3"));
		atn3.setOpaque(false);
		tools.add(atn3);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "conc"), 4));
		conc = new JSlider(0, 1280, light.getConcentration());
		conc.addChangeListener(this);
		conc.setOpaque(false);
		tools.add(conc);

		tools.add(new JLabel(Utilities.translator.translate(this.getClass(), "spread"), 4));
		spread = new JSlider(0, 360, light.getSpreadAngle());
		spread.addChangeListener(this);
		spread.setOpaque(false);
		tools.add(spread);


		JPanel tools2 = new JPanel();
		tools2.setLayout(new GridLayout(3, 3, 5, 5));

		tools2.add(new JLabel());
		tools2.add(new JLabel(Utilities.translator.translate(this.getClass(), "bounds")));
		tools2.add(new JLabel());

		tools2.add(new JLabel());
		tools2.add(new JLabel(Utilities.translator.translate(this.getClass(), "radius"), 4));

		radius = new JTextField(light.getBoundsRadius() + "");
		radius.addActionListener(this);
		tools2.add(radius);

		float[] pos = light.getBoundsPosition();
		posX = new JTextField(pos[0] + "");
		posX.addActionListener(this);
		posX.setToolTipText(Utilities.translator.translate(this.getClass(), "centX"));
		tools2.add(posX);
		posY = new JTextField(pos[1] + "");
		posY.addActionListener(this);
		posY.setToolTipText(Utilities.translator.translate(this.getClass(), "centY"));
		tools2.add(posY);
		posZ = new JTextField(pos[2] + "");
		posZ.addActionListener(this);
		posZ.setToolTipText(Utilities.translator.translate(this.getClass(), "centZ"));
		tools2.add(posZ);


		JTabbedPane tabs = new JTabbedPane(JTabbedPane.LEFT);
		add(tabs, BorderLayout.CENTER);
		tabs.addTab("1", tools);
		tabs.addTab("2", tools2);

		enableAll(light.getEnable());
	}


	/**	
	 * Enables/disables all controls (except <code>enable</code>)
	 *
	 * @param enable
	 *	if <code>true</code> enables controls,
	 *	otherwise disables
	 */
	private void enableAll(boolean enable){
		color.setEnabled(enable);

		if(light.getLightType() > 1 && enable){
			atn1.setEnabled(true);
			atn2.setEnabled(true);
			atn3.setEnabled(true);
		} else {
			atn1.setEnabled(false);
			atn2.setEnabled(false);
			atn3.setEnabled(false);
		}

		if(light.getLightType() > 2 && enable){
			conc.setEnabled(true);
			spread.setEnabled(true);
		} else {
			conc.setEnabled(false);
			spread.setEnabled(false);
		}
	}


	/**
	 * 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){
			boolean state = enable.getModel().isSelected();
			light.setEnable(state);
			enableAll(state);
		} else if(e.getSource() == color){
			String title = Utilities.translator.translate(this.getClass(), "color");
			Color clr = light.getColor();
			light.setColor(new JColorChooser().showDialog(new JFrame(), title, clr));
			color.setForeground(light.getColor());
		}

		else if(e.getSource() == radius){
			try{
				float rad = Float.valueOf(radius.getText()).floatValue();
				light.setBoundsRadius(rad);
			} catch(Throwable t){
			}
		} else if(e.getSource() == posX || e.getSource() == posY || e.getSource() == posZ){
			try{
				float pX = Float.valueOf(posX.getText()).floatValue();
				float pY = Float.valueOf(posY.getText()).floatValue();
				float pZ = Float.valueOf(posZ.getText()).floatValue();
				light.setBoundsPosition(pX, pY, pZ);
			} catch(Throwable t){
			}
		}
	}


	/**
	 * Listens action events sent by sliders attached to this panel.
	 *
	 * @param e
	 *	event sent by a slider
	 */
	public void stateChanged(ChangeEvent e){
		if(e.getSource() == atn1 || e.getSource() == atn2 || e.getSource() == atn3){
			light.setAttenuation(atn1.getValue(), atn2.getValue(), atn3.getValue());
		} else if(e.getSource() == conc){
			light.setConcentration(conc.getValue());
		} else if(e.getSource() == spread){
			light.setSpreadAngle(spread.getValue());
		}
	}

}
