/**
 *
 */
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.ButtonGroup;
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.JRadioButton;
import javax.swing.JTextField;

import de.rico.editor.WorldEditor;
import de.rico.editor.datamodel.CharacterData;
import de.rico.editor.datamodel.ItemData;
import de.rico.editor.datamodel.MeshData;
import de.rico.editor.datamodel.PropData;
import de.rico.editor.gui.filter.FilterImage;
import de.rico.engine.enums.BoundingVolume;
import de.rico.engine.geometry.Point3D;

/**
 * A doalog to add a 3d mesh.
 * @author Frank Bruns
 *
 */
public class DialogAddMesh extends JDialog
{

	private static final long serialVersionUID = 1L;

	private DialogAddMesh 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 labelInternalId = null;

	private JTextField txtInternalId = null;

	private File modelFile = 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 JCheckBox checkBoxSmooth = null;

	private JLabel labelSmooth = null;

	private JRadioButton radioBtnCharacter = null;

	private JRadioButton radioBtnProp = null;

	private JRadioButton radioBtnItem = null;

	private JLabel labelInGameName = null;

	private JTextField txtInGameName = null;

	private JButton btnChooseInventoryImage = null;

	private JLabel labelInventImg = null;

	private JTextField txtInventoryImg = null;

	private JLabel labelScriptId = null;

	private JTextField txtScriptId = null;

	private JCheckBox checkBoxShadow = null;

	private JLabel labelVolume = null;

	private JComboBox comboVolumeType = null;

	/**
	 * @param owner
	 */
	public DialogAddMesh(Frame owner, File modelFile)
	{
		super(owner, "Add a mesh", true);
		this.modelFile = modelFile;
		this.initialize();

		this.self = this;
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(344, 414);
		this.setTitle("Add a mesh (character, item or prop).");
		this.setContentPane(this.getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelVolume = new JLabel();
			this.labelVolume.setBounds(new Rectangle(15, 210, 129, 16));
			this.labelVolume.setText("Bounding volume type");
			this.labelScriptId = new JLabel();
			this.labelScriptId.setBounds(new Rectangle(15, 270, 86, 16));
			this.labelScriptId.setText("Scripting id:");
			this.labelScriptId.setVisible(true);
			this.labelInventImg = new JLabel();
			this.labelInventImg.setBounds(new Rectangle(15, 300, 106, 16));
			this.labelInventImg.setText("Inventory image:");
			this.labelInventImg.setVisible(false);
			this.labelInGameName = new JLabel();
			this.labelInGameName.setBounds(new Rectangle(15, 240, 91, 16));
			this.labelInGameName.setText("In game name:");
			this.labelInGameName.setVisible(false);
			this.labelSmooth = new JLabel();
			this.labelSmooth.setBounds(new Rectangle(38, 143, 111, 16));
			this.labelSmooth.setText("Smooth normals?");
			this.labelFlipTex = new JLabel();
			this.labelFlipTex.setBounds(new Rectangle(38, 189, 97, 14));
			this.labelFlipTex.setText("Flip texture?");
			this.labelRotTex = new JLabel();
			this.labelRotTex.setBounds(new Rectangle(37, 166, 97, 14));
			this.labelRotTex.setText("Rotate texture?");
			this.labelInternalId = new JLabel();
			this.labelInternalId.setBounds(new Rectangle(18, 61, 73, 14));
			this.labelInternalId.setText("Internal id:");
			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 mesh 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.labelInternalId, null);
			this.jContentPane.add(this.getTxtInternalId(), 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.getCheckBoxSmooth(), null);
			this.jContentPane.add(this.labelSmooth, null);
			this.jContentPane.add(this.getRadioBtnCharacter(), null);
			this.jContentPane.add(this.getRadioBtnProp(), null);
			this.jContentPane.add(this.getRadioBtnItem(), null);

			this.jContentPane.add(this.labelInGameName, null);
			this.jContentPane.add(this.getTxtInGameName(), null);
			this.jContentPane.add(this.getBtnChooseInventoryImage(), null);
			this.jContentPane.add(this.labelInventImg, null);
			this.jContentPane.add(this.getTxtInventoryImg(), null);
			this.jContentPane.add(this.labelScriptId, null);
			this.jContentPane.add(this.getTxtScriptId(), null);
			this.jContentPane.add(this.getCheckBoxShadow(), null);
			this.jContentPane.add(this.labelVolume, null);
			this.jContentPane.add(this.getComboVolumeType(), null);
			ButtonGroup radioBtnGroup = new ButtonGroup();
			radioBtnGroup.add(this.radioBtnCharacter);
			radioBtnGroup.add(this.radioBtnItem);
			radioBtnGroup.add(this.radioBtnProp);
		}
		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, 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(252, 85, 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(225, 330, 91, 32));
			this.btnAddMesh.setText("Add Mesh");
			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(DialogAddMesh.this.txtPosX.getText()),
							Float.parseFloat(DialogAddMesh.this.txtPosY.getText()),
							Float.parseFloat(DialogAddMesh.this.txtPosZ.getText()));

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

					String internalId = DialogAddMesh.this.txtInternalId.getText();
					String tex = DialogAddMesh.this.txtTexture.getText();
					boolean rotate = DialogAddMesh.this.checkBoxRotate.isSelected();
					boolean flip = DialogAddMesh.this.checkBoxFlip.isSelected();
					boolean smooth = DialogAddMesh.this.checkBoxSmooth.isSelected();
					boolean shadow = DialogAddMesh.this.checkBoxShadow.isSelected();

					// find bounding volume type
					BoundingVolume vol = null;
					if(DialogAddMesh.this.comboVolumeType.getSelectedItem().equals("Sphere"))
						vol = BoundingVolume.SPHERE;
					if(DialogAddMesh.this.comboVolumeType.getSelectedItem().equals("Box"))
						vol = BoundingVolume.BOX;
					if(DialogAddMesh.this.comboVolumeType.getSelectedItem().equals("Cylinder"))
						vol = BoundingVolume.CYLINDER;

					MeshData mesh = new MeshData(DialogAddMesh.this.modelFile.getAbsolutePath(),internalId,tex,pos,rot,rotate,flip,smooth,shadow,vol);

					// add the entities according to the seleceted
					// radio button
					if(DialogAddMesh.this.radioBtnCharacter.isSelected())
					{
						String name = DialogAddMesh.this.txtInGameName.getText();
						String scriptId = DialogAddMesh.this.txtScriptId.getText();

						CharacterData character = new CharacterData(mesh,name,scriptId);
						WorldEditor.markEntityForAdding(character);
					}
					if(DialogAddMesh.this.radioBtnItem.isSelected())
					{
						String name = DialogAddMesh.this.txtInGameName.getText();
						String scriptId = DialogAddMesh.this.txtScriptId.getText();
						String img = DialogAddMesh.this.txtInventoryImg.getText();

						ItemData item = new ItemData(mesh,name,scriptId,img);
						WorldEditor.markEntityForAdding(item);
					}
					if(DialogAddMesh.this.radioBtnProp.isSelected())
					{
						String scriptId = DialogAddMesh.this.txtScriptId.getText();
						PropData prop = new PropData(mesh, scriptId);
						WorldEditor.markEntityForAdding(prop);
					}

					DialogAddMesh.this.dispose();
				}
			});
		}
		return this.btnAddMesh;
	}

	/**
	 * This method initializes txtInternalId
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtInternalId()
	{
		if (this.txtInternalId == null)
		{
			this.txtInternalId = new JTextField();
			this.txtInternalId.setBounds(new Rectangle(105, 59, 207, 20));
		}
		return this.txtInternalId;
	}

	/**
	 * This method initializes btnChooseTex
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChooseTex()
	{
		if (this.btnChooseTex == null)
		{
			this.btnChooseTex = new JButton();
			this.btnChooseTex.setBounds(new Rectangle(14, 115, 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(DialogAddMesh.this.self);

					if (returnVal == JFileChooser.APPROVE_OPTION)
					{
						File file = fc.getSelectedFile();
						DialogAddMesh.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(165, 114, 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(13, 162, 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(14, 186, 21, 21));
			this.checkBoxFlip.setSelected(true);
		}
		return this.checkBoxFlip;
	}

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

	/**
	 * This method initializes radioBtnCharacter
	 *
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRadioBtnCharacter()
	{
		if (this.radioBtnCharacter == null)
		{
			this.radioBtnCharacter = new JRadioButton();
			this.radioBtnCharacter.setBounds(new Rectangle(165, 150, 91, 17));
			this.radioBtnCharacter.setText("Character");
			this.radioBtnCharacter.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					// make needed elements visible and not needed ones invisible
					DialogAddMesh.this.labelInGameName.setVisible(true);
					DialogAddMesh.this.labelInventImg.setVisible(false);
					DialogAddMesh.this.labelScriptId.setVisible(true);
					DialogAddMesh.this.txtInGameName.setVisible(true);
					DialogAddMesh.this.txtInventoryImg.setVisible(false);
					DialogAddMesh.this.btnChooseInventoryImage.setVisible(false);
					DialogAddMesh.this.txtScriptId.setVisible(true);
				}
			});
		}
		return this.radioBtnCharacter;
	}

	/**
	 * This method initializes radioBtnProp
	 *
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRadioBtnProp()
	{
		if (this.radioBtnProp == null)
		{
			this.radioBtnProp = new JRadioButton();
			this.radioBtnProp.setBounds(new Rectangle(165, 183, 91, 18));
			this.radioBtnProp.setSelected(true);
			this.radioBtnProp.setText("Prop");
			this.radioBtnProp.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					// make not needed elements invisible
					DialogAddMesh.this.labelInGameName.setVisible(false);
					DialogAddMesh.this.labelInventImg.setVisible(false);
					DialogAddMesh.this.labelScriptId.setVisible(true);
					DialogAddMesh.this.txtInGameName.setVisible(false);
					DialogAddMesh.this.txtInventoryImg.setVisible(false);
					DialogAddMesh.this.btnChooseInventoryImage.setVisible(false);
					DialogAddMesh.this.txtScriptId.setVisible(true);
				}
			});
		}
		return this.radioBtnProp;
	}

	/**
	 * This method initializes radioBtnItem
	 *
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRadioBtnItem()
	{
		if (this.radioBtnItem == null)
		{
			this.radioBtnItem = new JRadioButton();
			this.radioBtnItem.setBounds(new Rectangle(165, 165, 91, 19));
			this.radioBtnItem.setText("Item");
			this.radioBtnItem.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					// make needed elements visible
					DialogAddMesh.this.labelInGameName.setVisible(true);
					DialogAddMesh.this.labelInventImg.setVisible(true);
					DialogAddMesh.this.labelScriptId.setVisible(true);
					DialogAddMesh.this.txtInGameName.setVisible(true);
					DialogAddMesh.this.txtInventoryImg.setVisible(true);
					DialogAddMesh.this.btnChooseInventoryImage.setVisible(true);
					DialogAddMesh.this.txtScriptId.setVisible(true);
				}
			});
		}
		return this.radioBtnItem;
	}

	/**
	 * This method initializes txtInGameName
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtInGameName()
	{
		if (this.txtInGameName == null)
		{
			this.txtInGameName = new JTextField();
			this.txtInGameName.setBounds(new Rectangle(120, 240, 151, 20));
			this.txtInGameName.setVisible(false);
		}
		return this.txtInGameName;
	}

	/**
	 * This method initializes btnChooseInventoryImage
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChooseInventoryImage()
	{
		if (this.btnChooseInventoryImage == null)
		{
			this.btnChooseInventoryImage = new JButton();
			this.btnChooseInventoryImage.setBounds(new Rectangle(240, 300, 31, 16));
			this.btnChooseInventoryImage.setText("...");
			this.btnChooseInventoryImage.setVisible(false);
			this.btnChooseInventoryImage.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(DialogAddMesh.this.self);

					if (returnVal == JFileChooser.APPROVE_OPTION)
					{
						File file = fc.getSelectedFile();
						DialogAddMesh.this.txtInventoryImg.setText(file.getAbsolutePath());
					}
				}
			});
		}
		return this.btnChooseInventoryImage;
	}

	/**
	 * This method initializes txtInventoryImg
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtInventoryImg()
	{
		if (this.txtInventoryImg == null)
		{
			this.txtInventoryImg = new JTextField();
			this.txtInventoryImg.setBounds(new Rectangle(120, 300, 106, 20));
			this.txtInventoryImg.setVisible(false);
		}
		return this.txtInventoryImg;
	}

	/**
	 * This method initializes txtScriptId
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtScriptId()
	{
		if (this.txtScriptId == null)
		{
			this.txtScriptId = new JTextField();
			this.txtScriptId.setBounds(new Rectangle(120, 270, 151, 20));
			this.txtScriptId.setVisible(true);
		}
		return this.txtScriptId;
	}

	/**
	 * This method initializes checkBoxShadow
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxShadow()
	{
		if (this.checkBoxShadow == null)
		{
			this.checkBoxShadow = new JCheckBox();
			this.checkBoxShadow.setBounds(new Rectangle(15, 330, 145, 21));
			this.checkBoxShadow.setText("Casts a shadow?");
		}
		return this.checkBoxShadow;
	}

	/**
	 * This method initializes comboVolumeType
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboVolumeType()
	{
		if (this.comboVolumeType == null)
		{
			this.comboVolumeType = new JComboBox();
			this.comboVolumeType.setBounds(new Rectangle(165, 210, 151, 22));
			this.comboVolumeType.addItem("Sphere");
			this.comboVolumeType.addItem("Box");
			this.comboVolumeType.addItem("Cylinder (not functional)");
		}
		return this.comboVolumeType;
	}

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