package de.fitsample.timerecording.ui;

import java.lang.reflect.Method;

import javax.swing.table.TableModel;

import fit.ColumnFixture;

/**
 * This ColumnFixture is used as an alternative to ActionFixture for UI based
 * FIT testing.
 * 
 * @author Ralf
 */
public class TimeRecordingUIColumnFixture extends ColumnFixture {

	/**
	 * Filled by Fit with the content of the <code>day</code> cell.
	 */
	public String day;

	/**
	 * Filled by Fit with the content of the <code>from</code> cell.
	 */
	public String from;

	/**
	 * Filled by Fit with the content of the <code>to</code> cell.
	 */
	public String to;

	/**
	 * The UI to test.
	 */
	private TimeRecordingUI timeRecordingUI;

	/**
	 * Creates a TimeRecordingUIFicture.
	 */
	public TimeRecordingUIColumnFixture() {
		timeRecordingUI = new TimeRecordingUI();
		timeRecordingUI.pack();
		timeRecordingUI.setVisible(true);
		timeRecordingUI.toFront();
	}

	/**
	 * Overwrite execute() in order to add the new TimeRecord before any results
	 * are queried, means before
	 * {@link #gebuchteArbeitszeit() gebuchteArbeitszeit()}or
	 * {@link #Summe() Summe()}is called.
	 * 
	 * @see fit.ColumnFixture#execute()
	 */
	public void execute() {
		sleep();
		timeRecordingUI.dayTextField.setText(day);
		sleep();
		timeRecordingUI.fromTextField.setText(from);
		sleep();
		timeRecordingUI.toTextField.setText(to);
		sleep();
		timeRecordingUI.addButton.doClick();
		sleep();
	}

	/**
	 * Sleeps for the time specified in the {@link #slowMotion() slowMotion}
	 * row. This method is called by all actions.
	 */
	protected void sleep() {
		int slowMotionMs = 200;
		// supported only by FitNesse
		//		String[] args = getFixtureArgs();
		//		if ((args != null) && (args.length > 0)) {
		//			slowMotionMs = Integer.parseInt(args[0]);
		//		}
		if (slowMotionMs < 1) {
			return;
		}
		try {
			Thread.sleep(slowMotionMs);
		} catch (InterruptedException excep) {
			excep.printStackTrace();
		}
	}

	/**
	 * Returns the args field of the Fixture. This is only supported for
	 * non-RowFixtures by FitNesse. When run in FIT, <code>null</code> is
	 * returned.
	 * 
	 * @return the args field of the Fixture.
	 */
	protected String[] getFixtureArgs() {
		try {
			Method getArgs = this.getClass().getMethod("getArgs", new Class[0]);
			return (String[]) getArgs.invoke(this, new Object[0]);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @return the <code>working time</code> to check.
	 */
	public double workingTime() {
		TableModel model = timeRecordingUI.timeRecordTable.getModel();
		Object value = model.getValueAt(model.getRowCount() - 1,
				TimeRecordingTableModel.WORKING_TIME_COLUMN);
		return ((Double) value).doubleValue();
	}

	/**
	 * @return the <code>sum</code> to check.
	 */
	public double sum() {
		TableModel model = timeRecordingUI.timeRecordTable.getModel();
		Object value = model.getValueAt(model.getRowCount() - 1,
				TimeRecordingTableModel.SUM_COLUMN);
		return ((Double) value).doubleValue();
	}

}
