/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.dom.service;

import org.nakedobjects.applib.adapters.AbstractValueSemanticsProvider;
import org.nakedobjects.applib.adapters.Parser;

import com.domainlanguage.time.CalendarDate;
import com.domainlanguage.time.CalendarInterval;
import com.domainlanguage.time.Duration;

public class CalendarIntervalValueSemanticsProvider extends
        AbstractValueSemanticsProvider<CalendarInterval> {

    private static final String PARSEABLE_PATTERN = "yyyyMMdd";
    private static final String TITLE_PATTERN = "MMM dd, yyyy";

    
    @Override
    public Parser<CalendarInterval> getParser() {
        return new Parser<CalendarInterval>() {
            public CalendarInterval parseTextEntry(
                    final CalendarInterval context, final String entry) {
                String[] split = entry.split(":");
                String dateEntry = split[0];
                String durationEntry = split.length > 1?split[1]:null;
                CalendarDate start = parseDate(dateEntry, PARSEABLE_PATTERN);
                int howMany = parseDays(durationEntry);
                return start != null?
                    CalendarInterval.startingFrom(
                        start, Duration.days(howMany+1)):
                    null;
            }
            private CalendarDate parseDate(
                    final String dateEntry, final String candidatePattern) {
                try {
                    return CalendarDate.from(dateEntry, candidatePattern);
                } catch(NullPointerException ex) {
                    return null;
                } catch(IllegalArgumentException ex) {
                    return null;
                }
            }
            private int parseDays(final String daysEntry) {
                if (daysEntry == null) { return 0; }
                try {
                    return Integer.parseInt(daysEntry);
                } catch(NumberFormatException ex) {
                    return 0;
                }
            }
            // more to follow...
        };
    }
    
}
