package de.rico.editor.gui.dialog;

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

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import de.rico.adventure.entity.hotspot.CubicalHotpot;
import de.rico.adventure.entity.hotspot.SphericalHotspot;
import de.rico.editor.WorldEditor;
import de.rico.editor.gui.EditorMenu;
import de.rico.engine.geometry.Point3D;

/**
 * GUI dialog for adding interactive hotspots.
 * @author Frank Bruns
 */
public class DialogAddHotspot extends JDialog
{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTextField txtX = null;

	private JTextField txtZ = null;

	private JLabel labelPosX = null;

	private JLabel labelPosZ = null;

	private JButton btnAddHotspot = null;

	private JLabel labelHeadline = null;

	private JLabel labelPos = null;

	private JLabel labelScriptId = null;

	private JTextField txtScriptId = null;

	private JLabel labelWidth = null;

	private JTextField txtWidth = null;

	private JLabel labelLength = null;

	private JTextField txtLength = null;

	private JLabel labelRadius = null;

	private JTextField txtRadius = null;

	private JRadioButton radioSphere = null;

	private JRadioButton radioBox = null;

	private JLabel labelPosY = null;

	private JTextField txtY = null;

	private JLabel labelHeight = null;

	private JTextField txtHeight = null;

	private JLabel labelScreenName = null;

	private JTextField txtName = null;

	private JCheckBox checkBoxNeedsItem = null;

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

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(357, 267);
		this.setTitle("Add a hotspot");
		this.setContentPane(this.getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelScreenName = new JLabel();
			this.labelScreenName.setBounds(new Rectangle(15, 90, 91, 16));
			this.labelScreenName.setText("Screen name:");
			this.labelHeight = new JLabel();
			this.labelHeight.setBounds(new Rectangle(120, 165, 46, 16));
			this.labelHeight.setText("height:");
			this.labelHeight.setVisible(false);
			this.labelPosY = new JLabel();
			this.labelPosY.setBounds(new Rectangle(105, 135, 16, 16));
			this.labelPosY.setText("y:");
			this.labelRadius = new JLabel();
			this.labelRadius.setBounds(new Rectangle(15, 195, 46, 14));
			this.labelRadius.setText("radius:");
			this.labelLength = new JLabel();
			this.labelLength.setBounds(new Rectangle(225, 165, 46, 14));
			this.labelLength.setText("length:");
			this.labelLength.setVisible(false);
			this.labelWidth = new JLabel();
			this.labelWidth.setBounds(new Rectangle(15, 165, 46, 14));
			this.labelWidth.setText("width:");
			this.labelWidth.setVisible(false);
			this.labelScriptId = new JLabel();
			this.labelScriptId.setBounds(new Rectangle(15, 60, 53, 16));
			this.labelScriptId.setText("Script id:");
			this.labelPos = new JLabel();
			this.labelPos.setBounds(new Rectangle(14, 116, 116, 16));
			this.labelPos.setText("Hotspot position:");
			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 hotspot to the scene");
			this.labelPosZ = new JLabel();
			this.labelPosZ.setBounds(new Rectangle(195, 135, 16, 14));
			this.labelPosZ.setText("z:");
			this.labelPosX = new JLabel();
			this.labelPosX.setBounds(new Rectangle(15, 135, 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.labelPosZ, null);
			this.jContentPane.add(this.getBtnAddHotspot(), null);
			this.jContentPane.add(this.labelHeadline, null);
			this.jContentPane.add(this.labelPos, null);
			this.jContentPane.add(this.labelScriptId, null);
			this.jContentPane.add(this.getTxtScriptId(), null);
			this.jContentPane.add(this.labelWidth, null);
			this.jContentPane.add(this.getTxtWidth(), null);
			this.jContentPane.add(this.labelLength, null);
			this.jContentPane.add(this.getTxtLength(), null);
			this.jContentPane.add(this.labelRadius, null);
			this.jContentPane.add(this.getTxtRadius(), null);
			this.jContentPane.add(this.getRadioSphere(), null);
			this.jContentPane.add(this.getRadioBox(), null);

			this.jContentPane.add(this.labelPosY, null);
			this.jContentPane.add(this.getTxtY2(), null);
			this.jContentPane.add(this.labelHeight, null);
			this.jContentPane.add(this.getTxtHeight(), null);
			this.jContentPane.add(this.labelScreenName, null);
			this.jContentPane.add(this.getTxtName(), null);
			this.jContentPane.add(this.getCheckBoxNeedsItem(), null);
			ButtonGroup btnGroup = new ButtonGroup();
			btnGroup.add(this.radioSphere);
			btnGroup.add(this.radioBox);
		}
		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, 135, 61, 19));
			this.txtX.setText("0.0");
		}
		return this.txtX;
	}

	/**
	 * This method initializes txtY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtY()
	{
		if (this.txtZ == null)
		{
			this.txtZ = new JTextField();
			this.txtZ.setBounds(new Rectangle(210, 135, 61, 19));
			this.txtZ.setText("0.0");
		}
		return this.txtZ;
	}

	/**
	 * This method initializes btnSaveCam
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAddHotspot()
	{
		if (this.btnAddHotspot == null)
		{
			this.btnAddHotspot = new JButton();
			this.btnAddHotspot.setBounds(new Rectangle(195, 195, 121, 32));
			this.btnAddHotspot.setText("Add Hotspot");
			this.btnAddHotspot.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					// add spherical hotspot
					if(DialogAddHotspot.this.radioSphere.isSelected() == true)
					{
						String id = DialogAddHotspot.this.txtScriptId.getText();
						float x = new Float(DialogAddHotspot.this.txtX.getText());
						float y = new Float(DialogAddHotspot.this.txtY.getText());
						float z = new Float(DialogAddHotspot.this.txtZ.getText());
						float radius = new Float(DialogAddHotspot.this.txtRadius.getText());
						boolean needsItem = DialogAddHotspot.this.checkBoxNeedsItem.isSelected();
						SphericalHotspot hotspot = new SphericalHotspot(new Point3D(x,y,z),radius,needsItem);
						hotspot.setScriptId(id);
						hotspot.setScreenName(DialogAddHotspot.this.txtName.getText());

						WorldEditor.room.addHotspot(hotspot);
						EditorMenu.addHotspotToListMenu(id);
					}
					// add box hotspot
					if(DialogAddHotspot.this.radioBox.isSelected() == true)
					{
						String id = DialogAddHotspot.this.txtScriptId.getText();
						float x = new Float(DialogAddHotspot.this.txtX.getText());
						float y = new Float(DialogAddHotspot.this.txtY.getText());
						float z = new Float(DialogAddHotspot.this.txtZ.getText());
						float width = new Float(DialogAddHotspot.this.txtWidth.getText());
						float height = new Float(DialogAddHotspot.this.txtHeight.getText());
						float length = new Float(DialogAddHotspot.this.txtLength.getText());
						boolean needsItem = DialogAddHotspot.this.checkBoxNeedsItem.isSelected();

						CubicalHotpot hotspot = new CubicalHotpot(new Point3D(x,y,z),width,height,length,needsItem);
						hotspot.setScriptId(id);
						hotspot.setScreenName(DialogAddHotspot.this.txtName.getText());

						WorldEditor.room.addHotspot(hotspot);
						EditorMenu.addHotspotToListMenu(id);
					}

					// dispose dialogue
					DialogAddHotspot.this.dispose();
				}
			});
		}
		return this.btnAddHotspot;
	}

	/**
	 * This method initializes txtScriptId
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtScriptId()
	{
		if (this.txtScriptId == null)
		{
			this.txtScriptId = new JTextField();
			this.txtScriptId.setBounds(new Rectangle(75, 60, 121, 19));
		}
		return this.txtScriptId;
	}

	/**
	 * This method initializes txtWidth
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtWidth()
	{
		if (this.txtWidth == null)
		{
			this.txtWidth = new JTextField();
			this.txtWidth.setBounds(new Rectangle(60, 165, 46, 19));
			this.txtWidth.setText("1.0");
			this.txtWidth.setVisible(false);
		}
		return this.txtWidth;
	}

	/**
	 * This method initializes txtLength
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtLength()
	{
		if (this.txtLength == null)
		{
			this.txtLength = new JTextField();
			this.txtLength.setBounds(new Rectangle(270, 165, 46, 19));
			this.txtLength.setText("1.0");
			this.txtLength.setVisible(false);
		}
		return this.txtLength;
	}

	/**
	 * This method initializes txtRadius
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtRadius()
	{
		if (this.txtRadius == null)
		{
			this.txtRadius = new JTextField();
			this.txtRadius.setBounds(new Rectangle(60, 195, 46, 19));
			this.txtRadius.setText("1.0");
		}
		return this.txtRadius;
	}

	/**
	 * This method initializes radioCircular
	 *
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRadioSphere()
	{
		if (this.radioSphere == null)
		{
			this.radioSphere = new JRadioButton();
			this.radioSphere.setBounds(new Rectangle(210, 60, 106, 16));
			this.radioSphere.setSelected(true);
			this.radioSphere.setText("Spherical");
			this.radioSphere.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					// make circular trigger fields visible
					DialogAddHotspot.this.labelRadius.setVisible(true);
					DialogAddHotspot.this.txtRadius.setVisible(true);

					// make rectangluar trigger fields fields invisible
					DialogAddHotspot.this.labelWidth.setVisible(false);
					DialogAddHotspot.this.labelHeight.setVisible(false);
					DialogAddHotspot.this.labelLength.setVisible(false);
					DialogAddHotspot.this.txtWidth.setVisible(false);
					DialogAddHotspot.this.txtHeight.setVisible(false);
					DialogAddHotspot.this.txtLength.setVisible(false);
				}
			});
		}
		return this.radioSphere;
	}

	/**
	 * This method initializes radioRectangular
	 *
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRadioBox()
	{
		if (this.radioBox == null)
		{
			this.radioBox = new JRadioButton();
			this.radioBox.setBounds(new Rectangle(210, 75, 106, 21));
			this.radioBox.setText("Cubic");
			this.radioBox.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					// make circular trigger fields invisible
					DialogAddHotspot.this.labelRadius.setVisible(false);
					DialogAddHotspot.this.txtRadius.setVisible(false);

					// make rectangluar trigger fields fields visible
					DialogAddHotspot.this.labelWidth.setVisible(true);
					DialogAddHotspot.this.labelHeight.setVisible(true);
					DialogAddHotspot.this.labelLength.setVisible(true);
					DialogAddHotspot.this.txtWidth.setVisible(true);
					DialogAddHotspot.this.txtHeight.setVisible(true);
					DialogAddHotspot.this.txtLength.setVisible(true);
				}
			});
		}
		return this.radioBox;
	}

	/**
	 * This method initializes txtY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtY2()
	{
		if (this.txtY == null)
		{
			this.txtY = new JTextField();
			this.txtY.setBounds(new Rectangle(120, 135, 61, 20));
			this.txtY.setText("0.0");
		}
		return this.txtY;
	}

	/**
	 * This method initializes txtHeight
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtHeight()
	{
		if (this.txtHeight == null)
		{
			this.txtHeight = new JTextField();
			this.txtHeight.setBounds(new Rectangle(165, 165, 46, 20));
			this.txtHeight.setText("1.0");
			this.txtHeight.setVisible(false);
		}
		return this.txtHeight;
	}

	/**
	 * This method initializes txtName
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtName()
	{
		if (this.txtName == null)
		{
			this.txtName = new JTextField();
			this.txtName.setBounds(new Rectangle(105, 90, 91, 20));
		}
		return this.txtName;
	}

	/**
	 * This method initializes checkBoxNeedsItem
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxNeedsItem()
	{
		if (this.checkBoxNeedsItem == null)
		{
			this.checkBoxNeedsItem = new JCheckBox();
			this.checkBoxNeedsItem.setBounds(new Rectangle(210, 105, 136, 21));
			this.checkBoxNeedsItem.setText("needs item to use?");
			this.checkBoxNeedsItem.setSelected(true);
		}
		return this.checkBoxNeedsItem;
	}

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