package j3deditor.bin.components;

import j3deditor.bin.util.Utilities;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JTextField;


/**
 * Dialog for inputting an <code>int</code>, <code>float</code>, <code>double</code> or
 * <code>String</code> value.
 * 
 * @author Risto Seene
 */
public class InputDialog extends PopupDialog{

	/** specifies that this dialog accepts <code>int</code> type input. */
	public static final int INT = 0;

	/** specifies that this dialog accepts <code>float</code> type input. */
	public static final int FLOAT = 1;

	/** specifies that this dialog accepts <code>double</code> type input. */
	public static final int DOUBLE = 2;

	/** specifies that this dialog accepts <code>String</code> type input. */
	public static final int STRING = 3;

	/** text field that takes input */
	private JTextField field;

	/** type of this InputDialog: INT, FLOAT, DOUBLE, STRING */
	private int type;

	/** value */
	private int i;

	/** value */
	private float f;

	/** value */
	private double d;

	/** value */
	private String s;


	/**
	 * Creates an instance of <code>InputDialog</code> and initializes its content.
	 */
	protected InputDialog(){
		initDialog(Utilities.translator.translate(this.getClass(), "title"), 126, 100);

		this.setLayout(null);
		field = new JTextField();
		field.setBounds(10, 10, 100, (int)(field.getPreferredSize().getHeight()));
		this.add(field);

		JButton ok = new JButton(Utilities.translator.translate(this.getClass(), "ok"));
		ok.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent e){
					apply();
				}
			});
		ok.setBounds(10, 40, 100, (int)(ok.getPreferredSize().getHeight()));
		this.add(ok);
	}


	/**
	 * Sets the previous value that will be shown
	 * and specifies the type of this <code>InputDialog</code>.
	 * 
	 * @param currentValue
	 * 	value that will be shown
	 */
	private void setValue(int currentValue){
		this.type = InputDialog.INT;
		this.i = currentValue;
		this.field.setText(currentValue + "");
	}


	/**
	 * Sets the previous value that will be shown
	 * and specifies the type of this <code>InputDialog</code>.
	 * 
	 * @param currentValue
	 * 	value that will be shown
	 */
	private void setValue(float currentValue){
		this.type = InputDialog.FLOAT;
		this.f = currentValue;
		this.field.setText(currentValue + "");
	}


	/**
	 * Sets the previous value that will be shown
	 * and specifies the type of this <code>InputDialog</code>.
	 * 
	 * @param currentValue
	 * 	value that will be shown
	 */
	private void setValue(double currentValue){
		this.type = InputDialog.DOUBLE;
		this.d = currentValue;
		this.field.setText(currentValue + "");
	}


	/**
	 * Sets the previous value that will be shown
	 * and specifies the type of this <code>InputDialog</code>.
	 * 
	 * @param currentValue
	 * 	value that will be shown
	 */
	private void setValue(String currentValue){
		this.type = InputDialog.STRING;
		this.s = currentValue;
		this.field.setText(currentValue);
	}
	
	
	/**
	 * Verifies the input value and closes the dialog
	 * if value is acceptable.
	 */
	private void apply(){
		try{
			if(type == InputDialog.INT){
				this.i = Integer.parseInt(this.field.getText());
			}
		          else if(type == InputDialog.FLOAT){
		                    this.f = Float.parseFloat(this.field.getText());
		          }
		          else if(type == InputDialog.DOUBLE){
		                    this.d = Double.parseDouble(this.field.getText());
		          }
		          else if(type == InputDialog.STRING){
		                    this.s = this.field.getText();
		          }
		          dispose();
		} catch(Throwable t){
			Utilities.out.throwWarning(Utilities.translator.translate(this.getClass(), "invalid"));
		}
	}



	/**
	 * Brings up a dialog requesting <code>int</code> input from user.
	 * 
	 * @param currentValue
	 * 	previous value that will be shown in the text field
	 * @return the value that was inserted by user
	 */
	public static int askInt(int currentValue){
		InputDialog id = new InputDialog();
		id.setValue(currentValue);
		id.setVisible(true);
		return id.i;
	}


	/**
	 * Brings up a dialog requesting <code>float</code> input from user.
	 * 
	 * @param currentValue
	 * 	previous value that will be shown in the text field
	 * @return the value that was inserted by user
	 */
	public static float askFloat(float currentValue){
		InputDialog id = new InputDialog();
		id.setValue(currentValue);
		id.setVisible(true);
		return id.f;
	}


	/**
	 * Brings up a dialog requesting <code>double</code> input from user.
	 * 
	 * @param currentValue
	 * 	previous value that will be shown in the text field
	 * @return the value that was inserted by user
	 */
	public static double askDouble(double currentValue){
		InputDialog id = new InputDialog();
		id.setValue(currentValue);
		id.setVisible(true);
		return id.d;
	}


	/**
	 * Brings up a dialog requesting <code>String</code> input from user.
	 * 
	 * @param currentValue
	 * 	previous value that will be shown in the text field
	 * @return the value that was inserted by user
	 */
	public static String askString(String currentValue){
		InputDialog id = new InputDialog();
		id.setValue(currentValue);
		id.setVisible(true);
		return id.s;
	}

}
