package de.fitsample.timerecording;

import fit.ColumnFixture;

/**
 * The ColumnFixture for the TimeRecording FIT test.
 * 
 * @author Ralf
 */
public class TimeRecordingColumnFixture extends ColumnFixture {

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

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

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

    /**
     * The TimeRecording instance used for testing.
     */
    private TimeRecording timeRecording;

    /**
     * Create the Fixture.
     */
    public TimeRecordingColumnFixture() {
        timeRecording = new TimeRecording();
        TimeRecordingRepository.getInstance().store(timeRecording);
    }

    /**
     * @return the content for the <code>recorded time</code> cell.
     */
    public double recordedTime() {
        return timeRecording.getRecordedTimeForDay(day);
    }

    /**
     * @return the content for the <code>recorded break</code> cell.
     */
    public double recordedBreak() {
        return timeRecording.getRecordedBreakForDay(day);
    }

    /**
     * @return the content for the <code>mandatory break</code> cell.
     */
    public double mandatoryBreak() {
        return timeRecording.getMandatoryBreak(timeRecording.getRecordedTimeForDay(day));
    }

    /**
     * @return the content for the <code>working time</code> cell.
     */
    public double workingTime() {
        return timeRecording.getWorkingTimeForDay(day);
    }

    /**
     * @return the content for the <code>sum</code> cell.
     */
    public double sum() {
        return timeRecording.getWorkingTimeUpToDay(day);
    }

    /**
     * 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() {
        timeRecording.addRecord(new TimeRecord(day, new TimeFrame(from, to)));
    }

    /**
     * Overwrite parse() in order to support custom type {@link Time Time}.
     * 
     * @see fit.Fixture#parse(java.lang.String, java.lang.Class)
     */
    public Object parse(String text, Class type) throws Exception {
        if (type == Time.class) {
            return Time.valueOf(text);
        }
        return super.parse(text, type);
    }

}
