package org.opengroupware.jope.appserver.elements;

import java.text.ParseException;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.appserver.WOAssociation;
import org.opengroupware.jope.appserver.WOContext;
import org.opengroupware.jope.appserver.WOElement;
import org.opengroupware.jope.appserver.WORequest;

/*
 * WOInput
 * 
 * Abstract superclass for elements which participate in FORM processing.
 * 
 * Bindings:
 *   name     [in] - string
 *   value    [io] - object
 *   disabled [in] - boolean
 */
public abstract class WOInput extends WOHTMLDynamicElement {
  protected Log log = LogFactory.getLog("WOForms");
  
  protected WOAssociation name;
  protected WOAssociation value;
  protected WOAssociation disabled;

  public WOInput(String _name, Map<String, WOAssociation> _assocs,
                 WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.name     = grabAssociation(_assocs, "name");
    this.value    = grabAssociation(_assocs, "value");
    this.disabled = grabAssociation(_assocs, "disabled");
    
    /* type is defined by the element itself ... */
    grabAssociation(_assocs, "type");
    
    // TODO: warn against NAME association?
  }
  
  /* methods */
  
  protected String elementNameInContext(WOContext _ctx) {
    if (this.name == null)
      return _ctx.elementID();
    
    String s = this.name.stringValueInComponent(_ctx);
    if (s != null) return s;
    
    // TODO: print a warning about missing 'name' value?
    return _ctx.elementID();
  }

  /* taking form values */
  
  @SuppressWarnings("unused")
  protected Object parseFormValue(Object _value, WOContext _ctx)
    throws ParseException
  {
    /* redefined in subclasses */
    return _value;
  }
  
  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return;
    }
    
    if (this.value == null) { /* nothing to push to */
      this.log.info("missing value binding for element: " + this);
      return;
    }
    if (!this.value.isValueSettableInComponent(cursor)) {
      this.log.info("value binding cannot be set for element: " + this);
      return;
    }
    
    String formName  = this.elementNameInContext(_ctx);
    Object formValue = _rq.formValueForKey(formName);
    if (formValue == null) {
      // TODO: is this correct?
      this.log.debug("got not form value for form: " + formName);
      return;
    }
    
    try {
      formValue = this.parseFormValue(formValue, _ctx);
    }
    catch (ParseException e) {
      // TODO: add to some 'error report' object?
      this.log.warn("failed to parse form value with Format: " + formValue, e);
    }
    
    if (this.log.isDebugEnabled()) {
      this.log.debug("push field " + formName + " value: " + formValue + 
                     " => " + this.value);
    }
    this.value.setValue(formValue, cursor);
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "name",  this.name);
    this.appendAssocToDescription(_d, "value", this.value);
  }  
}
