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

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

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import de.rico.editor.WorldEditor;
import de.rico.editor.gui.EditorMenu;
import de.rico.engine.camera.Camera;
import de.rico.engine.camera.CameraPerspective;
import de.rico.engine.util.Tools;

/**
 * A dialog to save a camera perspective.
 * @author Frank Bruns
 *
 */
public class DialogAddCamPersp extends JDialog
{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTextField txtX = null;

	private JTextField txtY = null;

	private JLabel labelPosX = null;

	private JLabel labelPosY = null;

	private JLabel labelPosZ = null;

	private JTextField txtZ = null;

	private JButton btnSaveCam = null;

	private JLabel labelHeadline = null;

	private JLabel labelPos = null;

	private JLabel labelOrientation = null;

	private JLabel labelRotX = null;

	private JTextField txtRotX = null;

	private JLabel labelRotY = null;

	private JTextField txtRotY = null;

	private JLabel labelNr = null;

	private JComboBox comboNr = null;

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

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(313, 258);
		this.setTitle("Add camera perspective");
		this.setContentPane(this.getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelNr = new JLabel();
			this.labelNr.setBounds(new Rectangle(15, 60, 93, 16));
			this.labelNr.setText("Perpsective No.:");
			this.labelRotY = new JLabel();
			this.labelRotY.setBounds(new Rectangle(195, 150, 31, 16));
			this.labelRotY.setText("rot y:");
			this.labelRotX = new JLabel();
			this.labelRotX.setBounds(new Rectangle(105, 150, 31, 16));
			this.labelRotX.setText("rot x:");
			this.labelOrientation = new JLabel();
			this.labelOrientation.setBounds(new Rectangle(15, 151, 76, 16));
			this.labelOrientation.setText("Orientation:");
			this.labelPos = new JLabel();
			this.labelPos.setBounds(new Rectangle(14, 101, 242, 16));
			this.labelPos.setText("Camera 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("Save current camera perspective");
			this.labelPosZ = new JLabel();
			this.labelPosZ.setBounds(new Rectangle(195, 120, 16, 14));
			this.labelPosZ.setText("z:");
			this.labelPosY = new JLabel();
			this.labelPosY.setBounds(new Rectangle(105, 120, 16, 14));
			this.labelPosY.setText("y:");
			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.labelPosY, null);
			this.jContentPane.add(this.labelPosZ, null);
			this.jContentPane.add(this.getTxtZ(), null);
			this.jContentPane.add(this.getBtnSaveCam(), null);
			this.jContentPane.add(this.labelHeadline, null);
			this.jContentPane.add(this.labelPos, null);
			this.jContentPane.add(this.labelOrientation, null);
			this.jContentPane.add(this.labelRotX, null);
			this.jContentPane.add(this.getTxtRotX(), null);
			this.jContentPane.add(this.labelRotY, null);
			this.jContentPane.add(this.getTxtRotY(), null);
			this.jContentPane.add(this.labelNr, null);
			this.jContentPane.add(this.getComboNr(), null);
		}
		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(""+Tools.round(Camera.getInstance().getPosition().x,2));
		}
		return this.txtX;
	}

	/**
	 * This method initializes txtY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtY()
	{
		if (this.txtY == null)
		{
			this.txtY = new JTextField();
			this.txtY.setBounds(new Rectangle(120, 120, 61, 19));
			this.txtY.setText(""+Tools.round(Camera.getInstance().getPosition().y,2));
		}
		return this.txtY;
	}

	/**
	 * This method initializes txtZ
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtZ()
	{
		if (this.txtZ == null)
		{
			this.txtZ = new JTextField();
			this.txtZ.setBounds(new Rectangle(210, 120, 61, 19));
			this.txtZ.setText(""+Tools.round(Camera.getInstance().getPosition().z,2));
		}
		return this.txtZ;
	}

	/**
	 * This method initializes btnSaveCam
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnSaveCam()
	{
		if (this.btnSaveCam == null)
		{
			this.btnSaveCam = new JButton();
			this.btnSaveCam.setBounds(new Rectangle(180, 180, 91, 32));
			this.btnSaveCam.setText("Save");
			this.btnSaveCam.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					CameraPerspective p = Camera.getInstance().getCurrentCamPerspective().clone();

					// take changed values from the dialog form into account
					p.setPosition(
							-Float.parseFloat(DialogAddCamPersp.this.txtX.getText()),
							-Float.parseFloat(DialogAddCamPersp.this.txtY.getText()),
							-Float.parseFloat(DialogAddCamPersp.this.txtZ.getText()));
					p.setRotX(-Float.parseFloat(DialogAddCamPersp.this.txtRotX.getText()));
					p.setRotY(-Float.parseFloat(DialogAddCamPersp.this.txtRotY.getText()));
					int id = Integer.parseInt((String)DialogAddCamPersp.this.comboNr.getSelectedItem());

					WorldEditor.room.addCameraPerspective(p, id);
					EditorMenu.addCamPerspectiveToListMenu(id);

					// dispose dialogue
					DialogAddCamPersp.this.dispose();
				}
			});
		}
		return this.btnSaveCam;
	}

	/**
	 * This method initializes txtRotX
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtRotX()
	{
		if (this.txtRotX == null)
		{
			this.txtRotX = new JTextField();
			this.txtRotX.setBounds(new Rectangle(135, 150, 46, 20));
			this.txtRotX.setText(""+Tools.round(Camera.getInstance().getRotX(),2));
		}
		return this.txtRotX;
	}

	/**
	 * This method initializes txtRotY
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtRotY()
	{
		if (this.txtRotY == null)
		{
			this.txtRotY = new JTextField();
			this.txtRotY.setBounds(new Rectangle(225, 150, 46, 20));
			this.txtRotY.setText(""+Tools.round(Camera.getInstance().getRotY(),2));
		}
		return this.txtRotY;
	}

	/**
	 * This method initializes comboNr
	 *
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getComboNr()
	{
		if (this.comboNr == null)
		{
			this.comboNr = new JComboBox(new DefaultComboBoxModel());
			this.comboNr.setBounds(new Rectangle(120, 60, 55, 25));

			// get occupied id numbers
			ArrayList<Integer> occNumbers = new ArrayList<Integer>();
			for(Integer id : WorldEditor.room.getCameraPerspectives().keySet())
			{
				occNumbers.add(id);
			}

			// only add free id numbers between 0 and 31
			for(int i=0; i<32; i++)
			{
				if(occNumbers.contains(i) == false)
				this.comboNr.addItem(""+i);
			}
		}
		return this.comboNr;
	}

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