package org.castor.xmlctf.fieldhandler;

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

import org.exolab.castor.mapping.ClonableFieldHandlerMarker;
import org.exolab.castor.mapping.ConfigurableFieldHandler;
import org.exolab.castor.mapping.FieldHandler;
import org.exolab.castor.mapping.ValidityException;

public class FieldHandlerOldImpl
    implements FieldHandler, ConfigurableFieldHandler, ClonableFieldHandlerMarker {

  private DateFormat formatter;

  public DateFormat getFormatter() {
    return formatter;
  }

  public void setConfiguration(final Properties config) throws ValidityException {
    String pattern = config.getProperty("date-format");
    if (pattern == null) {
      throw new ValidityException(
          "Required parameter \"date-format\" is missing for FieldHandlerImpl.");
    }
    try {
      formatter = new SimpleDateFormat(pattern);
    } catch (IllegalArgumentException e) {
      throw new ValidityException("Pattern \"" + pattern + "\" is not a valid date format.");
    }
  }

  /**
   * Returns the value of the field from the object.
   * 
   * @param object The object
   * @return The value of the field
   * @throws IllegalStateException The Java object has changed and is no longer supported by this
   *         handler, or the handler is not compatible with the Java object
   */
  public Object getValue(Object object) throws IllegalStateException {
    Root root = (Root) object;
    Date value = root.getDate();
    if (value == null)
      return null;
    return formatter.format(value);
  }

  /**
   * Sets the value of the field on the object.
   * 
   * @param object The object
   * @param value The new value
   * @throws IllegalStateException The Java object has changed and is no longer supported by this
   *         handler, or the handler is not compatible with the Java object
   * @throws IllegalArgumentException The value passed is not of a supported type
   */
  public void setValue(Object object, Object value)
      throws IllegalStateException, IllegalArgumentException {
    Root root = (Root) object;
    Date date = null;
    try {
      date = formatter.parse((String) value);
    } catch (ParseException px) {
      throw new IllegalArgumentException(px.getMessage());
    }
    root.setDate(date);
  }

  /**
   * Creates a new instance of the object described by this field.
   * 
   * @param parent The object for which the field is created
   * @return A new instance of the field's value
   * @throws IllegalStateException This field is a simple type and cannot be instantiated
   */
  public Object newInstance(Object parent) throws IllegalStateException {
    // -- Since it's marked as a string...just return null,
    // -- it's not needed.
    return null;
  }

  /**
   * Sets the value of the field to a default value.
   * 
   * Reference fields are set to null, primitive fields are set to their default value, collection
   * fields are emptied of all elements.
   * 
   * @param object The object
   * @throws IllegalStateException The Java object has changed and is no longer supported by this
   *         handler, or the handler is not compatible with the Java object
   */
  public void resetValue(Object object) throws IllegalStateException, IllegalArgumentException {
    ((Root) object).setDate(null);
  }

  public void checkValidity(Object object) throws ValidityException, IllegalStateException {}

  public FieldHandler copyFieldHandler() {
    FieldHandlerOldImpl newFieldHandler = new FieldHandlerOldImpl();
    newFieldHandler.setFormatter(this.getFormatter());
    return newFieldHandler;
  }

  public void setFormatter(DateFormat formatter) {
    this.formatter = formatter;
  }

}
