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

import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import de.rico.editor.WorldEditor;
import de.rico.editor.datamodel.GroundPlaneData;
import de.rico.editor.datamodel.PropData;
import de.rico.editor.gui.filter.FilterImage;
import de.rico.engine.geometry.Point3D;

/**
 * A dialog to add a simple ground plane.
 * @author Frank Bruns
 *
 */
public class DialogAddGroundPlane extends JDialog
{

	private static final long serialVersionUID = 1L;

	private DialogAddGroundPlane self = null;

	private JPanel jContentPane = null;

	private JTextField txtPosX = null;

	private JTextField txtPosY = null;

	private JLabel labelPosX = null;

	private JLabel labelPosY = null;

	private JLabel labelPosZ = null;

	private JTextField txtPosZ = null;

	private JButton btnAddMesh = null;

	private JLabel labelHeadline = null;

	private JLabel labelName = null;

	private JTextField txtName = null;

	private JButton btnChooseTex = null;

	private JTextField txtTexture = null;

	private JCheckBox checkBoxRotate = null;

	private JLabel labelRotTex = null;

	private JCheckBox checkBoxFlip = null;

	private JLabel labelFlipTex = null;

	private JLabel labelSize = null;

	private JTextField txtSize = null;

	private JLabel labeltexRepeat = null;

	private JTextField txtTexRepeat = null;

	private JLabel labelGridNodeSize = null;

	private JTextField txtGridNodeSize = null;

	/**
	 * @param owner
	 */
	public DialogAddGroundPlane(Frame owner)
	{
		super(owner, "Add a ground plane", true);
		this.initialize();

		this.self = this;
	}

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

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelGridNodeSize = new JLabel();
			this.labelGridNodeSize.setBounds(new Rectangle(15, 135, 106, 14));
			this.labelGridNodeSize.setText("Path grid square size:");
			this.labeltexRepeat = new JLabel();
			this.labeltexRepeat.setBounds(new Rectangle(125, 109, 139, 16));
			this.labeltexRepeat.setText("Texture repeat factor:");
			this.labelSize = new JLabel();
			this.labelSize.setBounds(new Rectangle(16, 109, 38, 16));
			this.labelSize.setText("Size:");
			this.labelFlipTex = new JLabel();
			this.labelFlipTex.setBounds(new Rectangle(39, 217, 97, 14));
			this.labelFlipTex.setText("Flip texture?");
			this.labelRotTex = new JLabel();
			this.labelRotTex.setBounds(new Rectangle(38, 194, 97, 14));
			this.labelRotTex.setText("Rotate texture?");
			this.labelName = new JLabel();
			this.labelName.setBounds(new Rectangle(18, 61, 41, 14));
			this.labelName.setText("Name:");
			this.labelHeadline = new JLabel();
			this.labelHeadline.setBounds(new Rectangle(15, 15, 241, 31));
			this.labelHeadline.setFont(new Font("Tahoma", Font.BOLD, 14));
			this.labelHeadline.setText("Add a ground plane to the scene");
			this.labelPosZ = new JLabel();
			this.labelPosZ.setBounds(new Rectangle(236, 85, 16, 14));
			this.labelPosZ.setText("z:");
			this.labelPosY = new JLabel();
			this.labelPosY.setBounds(new Rectangle(127, 85, 16, 14));
			this.labelPosY.setText("y:");
			this.labelPosX = new JLabel();
			this.labelPosX.setBounds(new Rectangle(16, 85, 16, 14));
			this.labelPosX.setText("x:");
			this.jContentPane = new JPanel();
			this.jContentPane.setLayout(null);
			this.jContentPane.add(this.getTxtPosX(), null);
			this.jContentPane.add(this.getTxtPosY(), null);
			this.jContentPane.add(this.labelPosX, null);
			this.jContentPane.add(this.labelPosY, null);
			this.jContentPane.add(this.labelPosZ, null);
			this.jContentPane.add(this.getTxtPosZ(), null);
			this.jContentPane.add(this.getBtnAddMesh(), null);
			this.jContentPane.add(this.labelHeadline, null);
			this.jContentPane.add(this.labelName, null);
			this.jContentPane.add(this.getTxtName(), null);
			this.jContentPane.add(this.getBtnChooseTex(), null);
			this.jContentPane.add(this.getTxtTexture(), null);
			this.jContentPane.add(this.getCheckBoxRotate(), null);
			this.jContentPane.add(this.labelRotTex, null);
			this.jContentPane.add(this.getCheckBoxFlip(), null);
			this.jContentPane.add(this.labelFlipTex, null);
			this.jContentPane.add(this.labelSize, null);
			this.jContentPane.add(this.getTxtSize(), null);
			this.jContentPane.add(this.labeltexRepeat, null);
			this.jContentPane.add(this.getTxtTexRepeat(), null);
			this.jContentPane.add(this.labelGridNodeSize, null);
			this.jContentPane.add(this.getTxtGridNodeSize(), null);
		}
		return this.jContentPane;
	}

	/**
	 * This method initializes txtPosX
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtPosX()
	{
		if (this.txtPosX == null)
		{
			this.txtPosX = new JTextField();
			this.txtPosX.setBounds(new Rectangle(31, 85, 70, 19));
			this.txtPosX.setText("0");
		}
		return this.txtPosX;
	}

	/**
	 * This method initializes txtPosY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtPosY()
	{
		if (this.txtPosY == null)
		{
			this.txtPosY = new JTextField();
			this.txtPosY.setBounds(new Rectangle(142, 85, 69, 19));
			this.txtPosY.setText("0");
		}
		return this.txtPosY;
	}

	/**
	 * This method initializes txtPosZ
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtPosZ()
	{
		if (this.txtPosZ == null)
		{
			this.txtPosZ = new JTextField();
			this.txtPosZ.setBounds(new Rectangle(252, 85, 65, 19));
			this.txtPosZ.setText("0");
		}
		return this.txtPosZ;
	}

	/**
	 * This method initializes btnAddMesh
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAddMesh()
	{
		if (this.btnAddMesh == null)
		{
			this.btnAddMesh = new JButton();
			this.btnAddMesh.setBounds(new Rectangle(176, 201, 143, 32));
			this.btnAddMesh.setText("Add Ground Plane");
			this.btnAddMesh.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					// construct position light data model
					Point3D pos = new Point3D(
							Float.parseFloat(DialogAddGroundPlane.this.txtPosX.getText()),
							Float.parseFloat(DialogAddGroundPlane.this.txtPosY.getText()),
							Float.parseFloat(DialogAddGroundPlane.this.txtPosZ.getText()));

					Point3D rot = new Point3D(0f,0f,0f);

					String name = DialogAddGroundPlane.this.txtName.getText();
					String tex = DialogAddGroundPlane.this.txtTexture.getText();
					int size = new Integer(DialogAddGroundPlane.this.txtSize.getText());
					float texRep = new Float(DialogAddGroundPlane.this.txtTexRepeat.getText());
					int gridNodeSize = new Integer(DialogAddGroundPlane.this.txtGridNodeSize.getText());
					boolean rotate = DialogAddGroundPlane.this.checkBoxRotate.isSelected();
					boolean flip = DialogAddGroundPlane.this.checkBoxFlip.isSelected();

					GroundPlaneData plane = new GroundPlaneData(name,tex,pos,rot,size,texRep,gridNodeSize,rotate,flip,false);
					WorldEditor.markEntityForAdding(new PropData(plane,"ground"));
					DialogAddGroundPlane.this.dispose();
				}
			});
		}
		return this.btnAddMesh;
	}

	/**
	 * This method initializes txtName
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtName()
	{
		if (this.txtName == null)
		{
			this.txtName = new JTextField();
			this.txtName.setBounds(new Rectangle(77, 59, 238, 20));
			this.txtName.setText("ground plane");
		}
		return this.txtName;
	}

	/**
	 * This method initializes btnChooseTex
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChooseTex()
	{
		if (this.btnChooseTex == null)
		{
			this.btnChooseTex = new JButton();
			this.btnChooseTex.setBounds(new Rectangle(15, 165, 142, 19));
			this.btnChooseTex.setText("Choose texture ...");
			this.btnChooseTex.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					JFileChooser fc = new JFileChooser();
					fc.setCurrentDirectory(new File("./"));
					fc.setAcceptAllFileFilterUsed(false);
					fc.setFileFilter(new FilterImage());

					int returnVal = fc.showOpenDialog(DialogAddGroundPlane.this.self);

					if (returnVal == JFileChooser.APPROVE_OPTION)
					{
						File file = fc.getSelectedFile();
						DialogAddGroundPlane.this.txtTexture.setText(file.getAbsolutePath());
					}
				}
			});
		}
		return this.btnChooseTex;
	}

	/**
	 * This method initializes txtTexture
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtTexture()
	{
		if (this.txtTexture == null)
		{
			this.txtTexture = new JTextField();
			this.txtTexture.setBounds(new Rectangle(166, 164, 152, 23));
		}
		return this.txtTexture;
	}

	/**
	 * This method initializes checkBoxRotate
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxRotate()
	{
		if (this.checkBoxRotate == null)
		{
			this.checkBoxRotate = new JCheckBox();
			this.checkBoxRotate.setBounds(new Rectangle(14, 190, 21, 21));
			this.checkBoxRotate.setSelected(true);
		}
		return this.checkBoxRotate;
	}

	/**
	 * This method initializes checkBoxFlip
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxFlip()
	{
		if (this.checkBoxFlip == null)
		{
			this.checkBoxFlip = new JCheckBox();
			this.checkBoxFlip.setBounds(new Rectangle(15, 214, 21, 21));
			this.checkBoxFlip.setSelected(true);
		}
		return this.checkBoxFlip;
	}

	/**
	 * This method initializes txtSize
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtSize()
	{
		if (this.txtSize == null)
		{
			this.txtSize = new JTextField();
			this.txtSize.setBounds(new Rectangle(56, 107, 49, 20));
			this.txtSize.setText("50");
		}
		return this.txtSize;
	}

	/**
	 * This method initializes txtTexRepeat
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtTexRepeat()
	{
		if (this.txtTexRepeat == null)
		{
			this.txtTexRepeat = new JTextField();
			this.txtTexRepeat.setBounds(new Rectangle(268, 108, 48, 20));
			this.txtTexRepeat.setText("10");
		}
		return this.txtTexRepeat;
	}

	/**
	 * This method initializes txtGridNodeSize
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtGridNodeSize()
	{
		if (this.txtGridNodeSize == null)
		{
			this.txtGridNodeSize = new JTextField();
			this.txtGridNodeSize.setBounds(new Rectangle(135, 135, 31, 19));
			this.txtGridNodeSize.setText("2");
		}
		return this.txtGridNodeSize;
	}

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