/**
 *
 */
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.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
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.HMTerrainData;
import de.rico.editor.datamodel.PropData;
import de.rico.editor.gui.filter.FilterImage;
import de.rico.engine.geometry.Point3D;

/**
 * A dialog to add a heightmap terrain.
 * @author Frank Bruns
 *
 */
public class DialogAddHMTerrain extends JDialog
{

	private static final long serialVersionUID = 1L;

	private DialogAddHMTerrain 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 File hmapFile = 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 labelResFactor = null;

	private JComboBox comboTexRes = null;

	private JCheckBox checkBoxSmooth = null;

	private JLabel labelSmooth = null;

	private JLabel labelGridNodeSize = null;

	private JTextField txtGridNodeSize = null;

	/**
	 * @param owner
	 */
	public DialogAddHMTerrain(Frame owner, File modelFile)
	{
		super(owner, "Add a heightmap terrain", true);
		this.hmapFile = modelFile;
		this.initialize();

		this.self = this;
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(351, 310);
		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, 121, 14));
			this.labelGridNodeSize.setText("Path grid square size:");
			this.labelSmooth = new JLabel();
			this.labelSmooth.setBounds(new Rectangle(30, 195, 105, 16));
			this.labelSmooth.setText("Smooth normals?");
			this.labelResFactor = new JLabel();
			this.labelResFactor.setBounds(new Rectangle(16, 107, 120, 16));
			this.labelResFactor.setText("Resolution factor:");
			this.labelFlipTex = new JLabel();
			this.labelFlipTex.setBounds(new Rectangle(34, 244, 97, 14));
			this.labelFlipTex.setText("Flip texture?");
			this.labelRotTex = new JLabel();
			this.labelRotTex.setBounds(new Rectangle(34, 220, 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, 297, 31));
			this.labelHeadline.setFont(new Font("Tahoma", Font.BOLD, 14));
			this.labelHeadline.setText("Add a heightmap terrain to the scene");
			this.labelPosZ = new JLabel();
			this.labelPosZ.setBounds(new Rectangle(235, 84, 16, 14));
			this.labelPosZ.setText("z:");
			this.labelPosY = new JLabel();
			this.labelPosY.setBounds(new Rectangle(126, 84, 16, 14));
			this.labelPosY.setText("y:");
			this.labelPosX = new JLabel();
			this.labelPosX.setBounds(new Rectangle(15, 84, 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.labelResFactor, null);
			this.jContentPane.add(this.getComboTexRes(), null);
			this.jContentPane.add(this.getCheckBoxSmooth(), null);
			this.jContentPane.add(this.labelSmooth, 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(30, 84, 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(141, 84, 60, 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(251, 84, 63, 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(212, 224, 105, 32));
			this.btnAddMesh.setText("Add Terrain");
			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(DialogAddHMTerrain.this.txtPosX.getText()),
							Float.parseFloat(DialogAddHMTerrain.this.txtPosY.getText()),
							Float.parseFloat(DialogAddHMTerrain.this.txtPosZ.getText()));

					Point3D rot = new Point3D(0f,0f,0f);
					int res = new Integer((String)DialogAddHMTerrain.this.comboTexRes.getSelectedItem());
					String name = DialogAddHMTerrain.this.txtName.getText();
					String tex = DialogAddHMTerrain.this.txtTexture.getText();
					int gridNodeSize = new Integer(DialogAddHMTerrain.this.txtGridNodeSize.getText());
					boolean rotate = DialogAddHMTerrain.this.checkBoxRotate.isSelected();
					boolean flip = DialogAddHMTerrain.this.checkBoxFlip.isSelected();
					boolean smooth = DialogAddHMTerrain.this.checkBoxSmooth.isSelected();

					HMTerrainData terrain = new HMTerrainData(DialogAddHMTerrain.this.hmapFile.getAbsolutePath(),name,tex,pos,rot,res,gridNodeSize,rotate,flip,smooth);

					WorldEditor.markEntityForAdding(new PropData(terrain,"ground"));
					DialogAddHMTerrain.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, 235, 20));
			this.txtName.setText("terrain");
		}
		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(12, 166, 133, 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(DialogAddHMTerrain.this.self);

					if (returnVal == JFileChooser.APPROVE_OPTION)
					{
						File file = fc.getSelectedFile();
						DialogAddHMTerrain.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(152, 165, 163, 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(10, 216, 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(11, 240, 21, 21));
			this.checkBoxFlip.setSelected(true);
		}
		return this.checkBoxFlip;
	}

	/**
	 * This method initializes comboTexRes
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboTexRes()
	{
		if (this.comboTexRes == null)
		{
			this.comboTexRes = new JComboBox(new DefaultComboBoxModel());
			this.comboTexRes.setBounds(new Rectangle(150, 105, 61, 19));
			this.comboTexRes.addItem("1");
			this.comboTexRes.addItem("2");
			this.comboTexRes.addItem("4");
			this.comboTexRes.addItem("8");
			this.comboTexRes.addItem("16");
			this.comboTexRes.addItem("32");
			this.comboTexRes.addItem("64");
			this.comboTexRes.addItem("128");
			this.comboTexRes.getModel().setSelectedItem("16");
		}
		return this.comboTexRes;
	}

	/**
	 * This method initializes checkBoxSmooth
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxSmooth()
	{
		if (this.checkBoxSmooth == null)
		{
			this.checkBoxSmooth = new JCheckBox();
			this.checkBoxSmooth.setBounds(new Rectangle(10, 192, 21, 21));
		}
		return this.checkBoxSmooth;
	}

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

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