/**
 *
 */
package de.rico.editor.gui.dialog;

import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import de.rico.editor.WorldEditor;
import de.rico.editor.datamodel.DirectLightData;
import de.rico.editor.datamodel.PosLightData;
import de.rico.editor.datamodel.SpotLightData;
import de.rico.engine.effect.light.LightIface;
import de.rico.engine.effect.light.ogllight.AbstractLight;
import de.rico.engine.geometry.Point3D;
import de.rico.engine.material.Color4f;

/**
 * A dialog to add a light.
 * @author Frank Bruns
 *
 */
public class DialogAddLight extends JDialog
{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTextField txtX = null;

	private JTextField txtY = null;

	private JLabel labelPosX = null;

	private JLabel labelPosY = null;

	private JLabel labelPosZ = null;

	private JTextField txtZ = null;

	private JButton btnAddLight = null;

	private JLabel labelHeadline = null;

	private JComboBox comboLight = null;

	private JLabel labelDirOrPos = null;

	private JLabel labelAmbient = null;

	private JLabel labelAmbRed = null;

	private JTextField txtAmbRed = null;

	private JLabel labelAmbGreen = null;

	private JTextField txtAmbGreen = null;

	private JLabel labelAmbBlue = null;

	private JTextField txtAmbBlue = null;

	private JLabel labelAmbAlpha = null;

	private JTextField txtAmbAlpha = null;

	private JLabel labelDiff = null;

	private JLabel labelDiffRed = null;

	private JLabel labelDiffGreen = null;

	private JLabel labelDiffBlue = null;

	private JLabel labelDiffAlpha = null;

	private JTextField txtDiffRed = null;

	private JTextField txtDiffGreen = null;

	private JTextField txtDiffBlue = null;

	private JTextField txtDiffAlpha = null;

	private JLabel labelSpot = null;

	private JTextField txtSpotCutOff = null;

	private JLabel labelSpotExp = null;

	private JTextField txtSpotExp = null;

	private JLabel labelNr = null;

	private JComboBox comboNr = null;

	private JLabel labelSpotRot = null;

	private JLabel labelRotX = null;

	private JLabel labelRotY = null;

	private JTextField txtRotX = null;

	private JTextField txtRotY = null;

	/**
	 * @param owner
	 */
	public DialogAddLight(Frame owner)
	{
		super(owner, "Add a light", true);
		this.initialize();
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(404, 355);
		this.setContentPane(this.getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelRotY = new JLabel();
			this.labelRotY.setBounds(new Rectangle(208, 245, 38, 16));
			this.labelRotY.setText("rot y:");
			this.labelRotY.setVisible(false);
			this.labelRotX = new JLabel();
			this.labelRotX.setBounds(new Rectangle(113, 245, 38, 16));
			this.labelRotX.setText("rot x:");
			this.labelRotX.setVisible(false);
			this.labelSpotRot = new JLabel();
			this.labelSpotRot.setBounds(new Rectangle(14, 245, 79, 16));
			this.labelSpotRot.setText("Spot rotation:");
			this.labelSpotRot.setVisible(false);
			this.labelNr = new JLabel();
			this.labelNr.setBounds(new Rectangle(197, 59, 61, 16));
			this.labelNr.setText("Light No.:");
			this.labelSpotExp = new JLabel();
			this.labelSpotExp.setBounds(new Rectangle(14, 216, 96, 16));
			this.labelSpotExp.setText("Spot exponent:");
			this.labelSpotExp.setVisible(false);
			this.labelSpot = new JLabel();
			this.labelSpot.setBounds(new Rectangle(14, 191, 72, 16));
			this.labelSpot.setText("Spot cutoff:");
			this.labelSpot.setVisible(false);
			this.labelDiffAlpha = new JLabel();
			this.labelDiffAlpha.setBounds(new Rectangle(277, 161, 19, 16));
			this.labelDiffAlpha.setText("a:");
			this.labelDiffBlue = new JLabel();
			this.labelDiffBlue.setBounds(new Rectangle(222, 162, 16, 16));
			this.labelDiffBlue.setText("b:");
			this.labelDiffGreen = new JLabel();
			this.labelDiffGreen.setBounds(new Rectangle(168, 162, 18, 16));
			this.labelDiffGreen.setText("g:");
			this.labelDiffRed = new JLabel();
			this.labelDiffRed.setBounds(new Rectangle(111, 161, 20, 16));
			this.labelDiffRed.setText("r:");
			this.labelDiff = new JLabel();
			this.labelDiff.setBounds(new Rectangle(15, 161, 90, 16));
			this.labelDiff.setText("Diffuse color:");
			this.labelAmbAlpha = new JLabel();
			this.labelAmbAlpha.setBounds(new Rectangle(276, 135, 18, 16));
			this.labelAmbAlpha.setText("a:");
			this.labelAmbBlue = new JLabel();
			this.labelAmbBlue.setBounds(new Rectangle(220, 136, 18, 16));
			this.labelAmbBlue.setText("b:");
			this.labelAmbGreen = new JLabel();
			this.labelAmbGreen.setBounds(new Rectangle(169, 135, 16, 16));
			this.labelAmbGreen.setText("g:");
			this.labelAmbRed = new JLabel();
			this.labelAmbRed.setBounds(new Rectangle(113, 135, 17, 16));
			this.labelAmbRed.setText("r:");
			this.labelAmbient = new JLabel();
			this.labelAmbient.setBounds(new Rectangle(15, 136, 90, 16));
			this.labelAmbient.setText("Ambient color:");
			this.labelDirOrPos = new JLabel();
			this.labelDirOrPos.setBounds(new Rectangle(14, 86, 354, 16));
			this.labelDirOrPos.setText("Direction (directional light) or position (positional/spot light) of the light:");
			this.labelHeadline = new JLabel();
			this.labelHeadline.setBounds(new Rectangle(15, 15, 177, 31));
			this.labelHeadline.setFont(new Font("Tahoma", Font.BOLD, 14));
			this.labelHeadline.setText("Add a light to the scene");
			this.labelPosZ = new JLabel();
			this.labelPosZ.setBounds(new Rectangle(165, 107, 16, 14));
			this.labelPosZ.setText("z:");
			this.labelPosY = new JLabel();
			this.labelPosY.setBounds(new Rectangle(90, 107, 16, 14));
			this.labelPosY.setText("y:");
			this.labelPosX = new JLabel();
			this.labelPosX.setBounds(new Rectangle(15, 107, 16, 14));
			this.labelPosX.setText("x:");
			this.jContentPane = new JPanel();
			this.jContentPane.setLayout(null);
			this.jContentPane.add(this.getTxtX(), null);
			this.jContentPane.add(this.getTxtY(), null);
			this.jContentPane.add(this.labelPosX, null);
			this.jContentPane.add(this.labelPosY, null);
			this.jContentPane.add(this.labelPosZ, null);
			this.jContentPane.add(this.getTxtZ(), null);
			this.jContentPane.add(this.getBtnAddLight(), null);
			this.jContentPane.add(this.labelHeadline, null);
			this.jContentPane.add(this.getComboLight(), null);
			this.jContentPane.add(this.labelDirOrPos, null);
			this.jContentPane.add(this.labelAmbient, null);
			this.jContentPane.add(this.labelAmbRed, null);
			this.jContentPane.add(this.getTxtAmbRed(), null);
			this.jContentPane.add(this.labelAmbGreen, null);
			this.jContentPane.add(this.getTxtAmbGreen(), null);
			this.jContentPane.add(this.labelAmbBlue, null);
			this.jContentPane.add(this.getTxtAmbBlue(), null);
			this.jContentPane.add(this.labelAmbAlpha, null);
			this.jContentPane.add(this.getTxtAmbAlpha(), null);
			this.jContentPane.add(this.labelDiff, null);
			this.jContentPane.add(this.labelDiffRed, null);
			this.jContentPane.add(this.labelDiffGreen, null);
			this.jContentPane.add(this.labelDiffBlue, null);
			this.jContentPane.add(this.labelDiffAlpha, null);
			this.jContentPane.add(this.getTxtDiffRed(), null);
			this.jContentPane.add(this.getTxtDiffGreen(), null);
			this.jContentPane.add(this.getTxtDiffBlue(), null);
			this.jContentPane.add(this.getTxtDiffAlpha(), null);
			this.jContentPane.add(this.labelSpot, null);
			this.jContentPane.add(this.getTxtSpotCutOff(), null);
			this.jContentPane.add(this.labelSpotExp, null);
			this.jContentPane.add(this.getTxtSpotExp(), null);
			this.jContentPane.add(this.labelNr, null);
			this.jContentPane.add(this.getComboNr(), null);
			this.jContentPane.add(this.labelSpotRot, null);
			this.jContentPane.add(this.labelRotX, null);
			this.jContentPane.add(this.labelRotY, null);
			this.jContentPane.add(this.getTxtRotX(), null);
			this.jContentPane.add(this.getTxtRotY(), null);
		}
		return this.jContentPane;
	}

	/**
	 * This method initializes txtX
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtX()
	{
		if (this.txtX == null)
		{
			this.txtX = new JTextField();
			this.txtX.setBounds(new Rectangle(30, 107, 46, 19));
			this.txtX.setText("0");
		}
		return this.txtX;
	}

	/**
	 * This method initializes txtY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtY()
	{
		if (this.txtY == null)
		{
			this.txtY = new JTextField();
			this.txtY.setBounds(new Rectangle(105, 107, 46, 19));
			this.txtY.setText("0");
		}
		return this.txtY;
	}

	/**
	 * This method initializes txtZ
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtZ()
	{
		if (this.txtZ == null)
		{
			this.txtZ = new JTextField();
			this.txtZ.setBounds(new Rectangle(180, 107, 46, 19));
			this.txtZ.setText("0");
		}
		return this.txtZ;
	}

	/**
	 * This method initializes btnAddLight
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAddLight()
	{
		if (this.btnAddLight == null)
		{
			this.btnAddLight = new JButton();
			this.btnAddLight.setBounds(new Rectangle(278, 279, 91, 32));
			this.btnAddLight.setText("Add Light");
			this.btnAddLight.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{

					if(DialogAddLight.this.comboLight.getSelectedIndex() == 0)
					{
						DialogAddLight.this.addDirectionalLight();
					}
					if(DialogAddLight.this.comboLight.getSelectedIndex() == 1)
					{
						DialogAddLight.this.addPositionalLight();
					}
					if(DialogAddLight.this.comboLight.getSelectedIndex() == 2)
					{
						DialogAddLight.this.addSpotLight();
					}

					DialogAddLight.this.dispose();
				}
			});
		}
		return this.btnAddLight;
	}

	/**
	 * This method initializes comboLight
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboLight()
	{
		if (this.comboLight == null)
		{
			this.comboLight = new JComboBox(new DefaultComboBoxModel());
			this.comboLight.setBounds(new Rectangle(16, 54, 156, 25));
			this.comboLight.addItem("directional");
			this.comboLight.addItem("positional");
			this.comboLight.addItem("spot");
			this.comboLight.setSelectedIndex(1); // positional light
			this.comboLight.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					// make certain item (in)visible
					if(DialogAddLight.this.comboLight.getSelectedIndex() == 0
							|| DialogAddLight.this.comboLight.getSelectedIndex() == 1)
					{
						DialogAddLight.this.labelSpot.setVisible(false);
						DialogAddLight.this.labelSpotExp.setVisible(false);
						DialogAddLight.this.labelSpotRot.setVisible(false);
						DialogAddLight.this.labelRotX.setVisible(false);
						DialogAddLight.this.labelRotY.setVisible(false);

						DialogAddLight.this.txtSpotCutOff.setVisible(false);
						DialogAddLight.this.txtSpotExp.setVisible(false);
						DialogAddLight.this.txtRotX.setVisible(false);
						DialogAddLight.this.txtRotY.setVisible(false);

					}
					if(DialogAddLight.this.comboLight.getSelectedIndex() == 2)
					{
						DialogAddLight.this.labelSpot.setVisible(true);
						DialogAddLight.this.labelSpotExp.setVisible(true);
						DialogAddLight.this.labelSpotRot.setVisible(true);
						DialogAddLight.this.labelRotX.setVisible(true);
						DialogAddLight.this.labelRotY.setVisible(true);

						DialogAddLight.this.txtSpotCutOff.setVisible(true);
						DialogAddLight.this.txtSpotExp.setVisible(true);
						DialogAddLight.this.txtRotX.setVisible(true);
						DialogAddLight.this.txtRotY.setVisible(true);
					}
				}
			});
		}
		return this.comboLight;
	}

	/**
	 * This method initializes txtAmbRed
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtAmbRed()
	{
		if (this.txtAmbRed == null)
		{
			this.txtAmbRed = new JTextField();
			this.txtAmbRed.setBounds(new Rectangle(133, 134, 29, 20));
			this.txtAmbRed.setText("0.0");
		}
		return this.txtAmbRed;
	}

	/**
	 * This method initializes txtAmbGreen
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtAmbGreen()
	{
		if (this.txtAmbGreen == null)
		{
			this.txtAmbGreen = new JTextField();
			this.txtAmbGreen.setBounds(new Rectangle(187, 134, 29, 20));
			this.txtAmbGreen.setText("0.0");
		}
		return this.txtAmbGreen;
	}

	/**
	 * This method initializes txtAmbBlue
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtAmbBlue()
	{
		if (this.txtAmbBlue == null)
		{
			this.txtAmbBlue = new JTextField();
			this.txtAmbBlue.setBounds(new Rectangle(239, 133, 30, 20));
			this.txtAmbBlue.setText("0.0");
		}
		return this.txtAmbBlue;
	}

	/**
	 * This method initializes txtAmbAlpha
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtAmbAlpha()
	{
		if (this.txtAmbAlpha == null)
		{
			this.txtAmbAlpha = new JTextField();
			this.txtAmbAlpha.setBounds(new Rectangle(295, 133, 28, 20));
			this.txtAmbAlpha.setText("1.0");
		}
		return this.txtAmbAlpha;
	}

	/**
	 * This method initializes txtDiffRed
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtDiffRed()
	{
		if (this.txtDiffRed == null)
		{
			this.txtDiffRed = new JTextField();
			this.txtDiffRed.setBounds(new Rectangle(133, 158, 29, 20));
			this.txtDiffRed.setText("1.0");
		}
		return this.txtDiffRed;
	}

	/**
	 * This method initializes txtDiffGreen
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtDiffGreen()
	{
		if (this.txtDiffGreen == null)
		{
			this.txtDiffGreen = new JTextField();
			this.txtDiffGreen.setBounds(new Rectangle(187, 159, 28, 20));
			this.txtDiffGreen.setText("1.0");
		}
		return this.txtDiffGreen;
	}

	/**
	 * This method initializes txtDiffBlue
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtDiffBlue()
	{
		if (this.txtDiffBlue == null)
		{
			this.txtDiffBlue = new JTextField();
			this.txtDiffBlue.setBounds(new Rectangle(240, 159, 29, 20));
			this.txtDiffBlue.setText("1.0");
		}
		return this.txtDiffBlue;
	}

	/**
	 * This method initializes txtDiffAlpha
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtDiffAlpha()
	{
		if (this.txtDiffAlpha == null)
		{
			this.txtDiffAlpha = new JTextField();
			this.txtDiffAlpha.setBounds(new Rectangle(296, 158, 26, 20));
			this.txtDiffAlpha.setText("1.0");
		}
		return this.txtDiffAlpha;
	}

	/**
	 * This method initializes txtSpotCutOff
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtSpotCutOff()
	{
		if (this.txtSpotCutOff == null)
		{
			this.txtSpotCutOff = new JTextField();
			this.txtSpotCutOff.setBounds(new Rectangle(112, 189, 43, 20));
			this.txtSpotCutOff.setText("25.0");
			this.txtSpotCutOff.setVisible(false);
		}
		return this.txtSpotCutOff;
	}

	/**
	 * This method initializes txtSpotExp
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtSpotExp()
	{
		if (this.txtSpotExp == null)
		{
			this.txtSpotExp = new JTextField();
			this.txtSpotExp.setBounds(new Rectangle(113, 213, 41, 20));
			this.txtSpotExp.setText("0.0");
			this.txtSpotExp.setVisible(false);
		}
		return this.txtSpotExp;
	}

	/**
	 * This method initializes comboNr
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboNr()
	{
		if (this.comboNr == null)
		{
			this.comboNr = new JComboBox(new DefaultComboBoxModel());
			this.comboNr.setBounds(new Rectangle(262, 56, 55, 25));

			// get occupied light numbers
			ArrayList<Integer> occNumbers = new ArrayList<Integer>();
			for(LightIface light : WorldEditor.room.getLights())
			{
				AbstractLight l = (AbstractLight)light;
				occNumbers.add(l.getLightNr());
			}

			// only add free light numbers
			for(int i=0; i<8; i++)
			{
				if(occNumbers.contains(i) == false)
				this.comboNr.addItem(""+i);
			}
		}
		return this.comboNr;
	}

	/**
	 * Convenience method to add a directional light to the scene.
	 */
	private void addDirectionalLight()
	{
		// construct directional light data model
		Point3D dir = new Point3D(
				Float.parseFloat(this.txtX.getText()),
				Float.parseFloat(this.txtY.getText()),
				Float.parseFloat(this.txtZ.getText()));
		Color4f amb = new Color4f(
				Float.parseFloat(this.txtAmbRed.getText()),
				Float.parseFloat(this.txtAmbGreen.getText()),
				Float.parseFloat(this.txtAmbBlue.getText()),
				Float.parseFloat(this.txtAmbAlpha.getText()));
		Color4f diff = new Color4f(
				Float.parseFloat(this.txtDiffRed.getText()),
				Float.parseFloat(this.txtDiffGreen.getText()),
				Float.parseFloat(this.txtDiffBlue.getText()),
				Float.parseFloat(this.txtDiffAlpha.getText()));
		DirectLightData dl = new DirectLightData(dir,amb,diff,Integer.parseInt((String)this.comboNr.getSelectedItem()));

		WorldEditor.markDirectLightForAdding(dl);
	}

	/**
	 * Convenience method to add a positional light to the scene.
	 */
	private void addPositionalLight()
	{
		// construct positional light data model
		Point3D pos = new Point3D(
				Float.parseFloat(this.txtX.getText()),
				Float.parseFloat(this.txtY.getText()),
				Float.parseFloat(this.txtZ.getText()));
		Color4f amb = new Color4f(
				Float.parseFloat(this.txtAmbRed.getText()),
				Float.parseFloat(this.txtAmbGreen.getText()),
				Float.parseFloat(this.txtAmbBlue.getText()),
				Float.parseFloat(this.txtAmbAlpha.getText()));
		Color4f diff = new Color4f(
				Float.parseFloat(this.txtDiffRed.getText()),
				Float.parseFloat(this.txtDiffGreen.getText()),
				Float.parseFloat(this.txtDiffBlue.getText()),
				Float.parseFloat(this.txtDiffAlpha.getText()));
		PosLightData pl = new PosLightData(pos,amb,diff,Integer.parseInt((String)this.comboNr.getSelectedItem()));

		WorldEditor.markPosLightForAdding(pl);
	}

	/**
	 * Convenience method to add a spot light to the scene.
	 */
	private void addSpotLight()
	{
		//	construct spot light data model
		Point3D pos = new Point3D(
				Float.parseFloat(this.txtX.getText()),
				Float.parseFloat(this.txtY.getText()),
				Float.parseFloat(this.txtZ.getText()));
		Color4f amb = new Color4f(
				Float.parseFloat(this.txtAmbRed.getText()),
				Float.parseFloat(this.txtAmbGreen.getText()),
				Float.parseFloat(this.txtAmbBlue.getText()),
				Float.parseFloat(this.txtAmbAlpha.getText()));
		Color4f diff = new Color4f(
				Float.parseFloat(this.txtDiffRed.getText()),
				Float.parseFloat(this.txtDiffGreen.getText()),
				Float.parseFloat(this.txtDiffBlue.getText()),
				Float.parseFloat(this.txtDiffAlpha.getText()));
		float rotX = Float.parseFloat(this.txtRotX.getText());
		float rotY = Float.parseFloat(this.txtRotY.getText());
		float cutOff = Float.parseFloat(this.txtSpotCutOff.getText());
		float exp = Float.parseFloat(this.txtSpotExp.getText());
		int nr = Integer.parseInt((String)this.comboNr.getSelectedItem());

		SpotLightData sl = new SpotLightData(pos,rotX,rotY,amb,diff,cutOff,exp,nr);

		WorldEditor.markSpotLightForAdding(sl);
	}

	/**
	 * This method initializes txtRotX
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtRotX()
	{
		if (this.txtRotX == null)
		{
			this.txtRotX = new JTextField();
			this.txtRotX.setBounds(new Rectangle(152, 243, 47, 20));
			this.txtRotX.setText("0.0");
			this.txtRotX.setVisible(false);
		}
		return this.txtRotX;
	}

	/**
	 * This method initializes txtRotY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtRotY()
	{
		if (this.txtRotY == null)
		{
			this.txtRotY = new JTextField();
			this.txtRotY.setBounds(new Rectangle(260, 243, 45, 20));
			this.txtRotY.setText("0.0");
			this.txtRotY.setVisible(false);
		}
		return this.txtRotY;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
