/**
 *
 */
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.script.trigger.CircleTriggerArea;
import de.rico.adventure.script.trigger.RectTriggerArea;
import de.rico.editor.WorldEditor;
import de.rico.editor.gui.EditorMenu;

/**
 * @author Frank Bruns
 *
 */
public class DialogAddTrigger 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 btnAddTrigger = 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 radioCircular = null;

	private JRadioButton radioRectangular = null;

	private JCheckBox checkBoxDisabled = null;

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

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

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelRadius = new JLabel();
			this.labelRadius.setBounds(new Rectangle(195, 120, 46, 14));
			this.labelRadius.setText("radius:");
			this.labelLength = new JLabel();
			this.labelLength.setBounds(new Rectangle(120, 150, 46, 14));
			this.labelLength.setText("length:");
			this.labelLength.setVisible(false);
			this.labelWidth = new JLabel();
			this.labelWidth.setBounds(new Rectangle(15, 150, 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, 101, 92, 16));
			this.labelPos.setText("Trigger 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 trigger to the scene");
			this.labelPosZ = new JLabel();
			this.labelPosZ.setBounds(new Rectangle(105, 120, 16, 14));
			this.labelPosZ.setText("z:");
			this.labelPosX = new JLabel();
			this.labelPosX.setBounds(new Rectangle(15, 120, 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.getBtnAddTrigger(), 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.getRadioCircular(), null);
			this.jContentPane.add(this.getRadioRectangular(), null);

			this.jContentPane.add(this.getCheckBoxDisabled(), null);
			ButtonGroup btnGroup = new ButtonGroup();
			btnGroup.add(this.radioCircular);
			btnGroup.add(this.radioRectangular);
		}
		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, 120, 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(120, 120, 61, 19));
			this.txtZ.setText("0.0");
		}
		return this.txtZ;
	}

	/**
	 * This method initializes btnSaveCam
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAddTrigger()
	{
		if (this.btnAddTrigger == null)
		{
			this.btnAddTrigger = new JButton();
			this.btnAddTrigger.setBounds(new Rectangle(210, 180, 91, 32));
			this.btnAddTrigger.setText("Add Trigger");
			this.btnAddTrigger.setText("Add Trigger");
			this.btnAddTrigger.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					// add circular trigger
					if(DialogAddTrigger.this.radioCircular.isSelected() == true)
					{
						String id = DialogAddTrigger.this.txtScriptId.getText();
						float x = new Float(DialogAddTrigger.this.txtX.getText());
						float z = new Float(DialogAddTrigger.this.txtZ.getText());
						float radius = new Float(DialogAddTrigger.this.txtRadius.getText());
						CircleTriggerArea trigger = new CircleTriggerArea(id,x,z,radius);

						// disable trigger at startup, if desired
						if(DialogAddTrigger.this.checkBoxDisabled.isSelected())
							trigger.setEnabled(false);

						WorldEditor.room.addTrigger(trigger);
						EditorMenu.addTriggerToListMenu(id);
					}
					// add rectangular trigger
					if(DialogAddTrigger.this.radioRectangular.isSelected() == true)
					{
						String id = DialogAddTrigger.this.txtScriptId.getText();
						float x = new Float(DialogAddTrigger.this.txtX.getText());
						float z = new Float(DialogAddTrigger.this.txtZ.getText());
						float width = new Float(DialogAddTrigger.this.txtWidth.getText());
						float length = new Float(DialogAddTrigger.this.txtLength.getText());
						RectTriggerArea trigger = new RectTriggerArea(id,x,z,width,length);

						WorldEditor.room.addTrigger(trigger);
						EditorMenu.addTriggerToListMenu(id);
					}

					// dispose dialogue
					DialogAddTrigger.this.dispose();
				}
			});
		}
		return this.btnAddTrigger;
	}

	/**
	 * 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, 106, 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, 150, 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(165, 150, 50, 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(240, 120, 46, 19));
			this.txtRadius.setText("1.0");
		}
		return this.txtRadius;
	}

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

					// make rectangluar trigger fields fields invisible
					DialogAddTrigger.this.labelWidth.setVisible(false);
					DialogAddTrigger.this.labelLength.setVisible(false);
					DialogAddTrigger.this.txtWidth.setVisible(false);
					DialogAddTrigger.this.txtLength.setVisible(false);
				}
			});
		}
		return this.radioCircular;
	}

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

					// make rectangluar trigger fields fields visible
					DialogAddTrigger.this.labelWidth.setVisible(true);
					DialogAddTrigger.this.labelLength.setVisible(true);
					DialogAddTrigger.this.txtWidth.setVisible(true);
					DialogAddTrigger.this.txtLength.setVisible(true);
				}
			});
		}
		return this.radioRectangular;
	}

	/**
	 * This method initializes checkBoxDisabled
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckBoxDisabled()
	{
		if (this.checkBoxDisabled == null)
		{
			this.checkBoxDisabled = new JCheckBox();
			this.checkBoxDisabled.setBounds(new Rectangle(15, 195, 153, 16));
			this.checkBoxDisabled.setText("Initially disabled?");
		}
		return this.checkBoxDisabled;
	}

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