package nl.hsac.fitnesse.fixture.slim;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Fixture that helps converting dates to and from various formats. Intended to be used as a utility fixture i.e. as library
 */

public class DateFormatFixture extends SlimFixture {
    private String dateFormat = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT)).toPattern();
    private TimeZone timezone = TimeZone.getDefault();
    private boolean timestampHasMilliseconds = true;

    public DateFormatFixture() {}
    public DateFormatFixture(String dateformat, String timezone) {
        setDateFormat(dateformat);
        setTimezone(timezone);
    }

    public void setDateFormat(String df) {
        dateFormat = df;
    }

    public void setTimezone(String newTimezone) {
        timezone = TimeZone.getTimeZone(newTimezone);
    }

    public void timestampHasMilliseconds(boolean hasMillis) {
        timestampHasMilliseconds = hasMillis;
    }

    public String formatTimestamp(long timestamp) {
        return formatTimestamp(getConfiguredDateFormat(), timestamp);
    }

    public String formatTimestampAs(long timestamp, String dateFormat) {
        DateFormat sdf = getDateFormat(dateFormat);
        return formatTimestamp(sdf, timestamp);
    }

    public String formatDateAs(String date, String newFormat) {
        Date parsedDate = parseDate(date);
        DateFormat targetFormat = getDateFormat(newFormat);
        return targetFormat.format(parsedDate);
    }

    public long timestampForDate(String date) {
        Date parsedDate = parseDate(date);
        long timeStamp = parsedDate.getTime();
        if (!timestampHasMilliseconds) {
            timeStamp = timeStamp / 1000L;
        }
        return timeStamp;
    }

    protected String formatTimestamp(DateFormat sdf, long timestamp) {
        if(!timestampHasMilliseconds) {
            timestamp = timestamp * 1000L;
        }

        Date date = new Date(timestamp);
        return sdf.format(date);
    }

    protected Date parseDate(String date) {
        SimpleDateFormat sdf = getConfiguredDateFormat();
        return parseDate(sdf, date);
    }

    protected Date parseDate(SimpleDateFormat sdf, String date) {
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            String pattern = sdf.toPattern();
            throw new SlimFixtureException(false, "Could not parse " + date + " using format: " + pattern, e);
        }
    }

    protected SimpleDateFormat getConfiguredDateFormat() {
        return getDateFormat(dateFormat);
    }

    protected SimpleDateFormat getDateFormat(String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        sdf.setTimeZone(timezone);
        return sdf;
    }
}


@@ -314,17 +314,16 @@
 ary%0A */%0A
-%0A
 public c
@@ -400,88 +400,8 @@
 rmat
- = ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT)).toPattern()
 ;%0A  
@@ -431,32 +431,8 @@
 zone
- = TimeZone.getDefault()
 ;%0A  
@@ -515,17 +515,79 @@
 ture() %7B
-%7D
+%0A        this(getDefaultFormat(), getDefaultTimeZone());%0A    %7D%0A
 %0A    pub
@@ -2834,12 +2834,266 @@
 f;%0A    %7D
+%0A%0A    protected static String getDefaultFormat() %7B%0A        return ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT)).toPattern();%0A    %7D%0A%0A    protected static String getDefaultTimeZone() %7B%0A        return TimeZone.getDefault().getID();%0A    %7D
 %0A%7D%0A%0A

Move logic to determine defaults to static methods