package org.opengroupware.jope.appserver.elements;

import java.util.Map;

import org.opengroupware.jope.appserver.WOActionResults;
import org.opengroupware.jope.appserver.WOAssociation;
import org.opengroupware.jope.appserver.WOContext;
import org.opengroupware.jope.appserver.WOElement;
import org.opengroupware.jope.appserver.WORequest;
import org.opengroupware.jope.appserver.WOResponse;

/*
 * WOSubmitButton
 * 
 * Abstract superclass for elements which participate in FORM processing.
 * 
 * Sample:
 *   OK: WOSubmitButton {
 *     name  = "OK";
 *     value = "OK";
 *   }
 * 
 * Renders:
 *   <input type="submit" name="OK" value="OK" />
 * 
 * Bindings (WOInput):
 *   name     [in] - string
 *   value    [io] - object
 *   disabled [in] - boolean
 * Bindings:
 *   action   [in] - action
 *   pageName [in] - string
 */
public class WOSubmitButton extends WOInput {
  
  protected WOAssociation action;
  protected WOAssociation pageName;
  
  public WOSubmitButton(String _name, Map<String, WOAssociation> _assocs,
                        WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.action   = grabAssociation(_assocs, "action");
    this.pageName = grabAssociation(_assocs, "pageName");
  }
  
  /* responder */

  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return;
    }
    
    Object formValue = _rq.formValueForKey(this.elementNameInContext(_ctx));
    
    if (this.value != null && this.value.isValueSettableInComponent(cursor))
      this.value.setValue(formValue, cursor);
    
    if (this.action != null || this.pageName != null)
      _ctx.addActiveFormElement(this);
  }
  
  public WOActionResults invokeAction(WORequest _rq, WOContext _ctx) {
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return null;
    }
    
    if (!_ctx.elementID().equals(_ctx.senderID())) {
      // TODO: print a log?
      return null;
    }
    
    if (this.action != null)
      return (WOActionResults)this.action.valueInComponent(cursor);
    
    if (this.pageName != null) {
      String pname;
      
      pname = this.pageName.stringValueInComponent(cursor);
      if (pname == null) {
        // TODO: print a log
        return null;
      }
      
      return _ctx.application().pageWithName(pname, _ctx);
    }
    
    return null;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (_ctx.isRenderingDisabled())
      return;

    _r.appendBeginTag("input");
    _r.appendAttribute("type",  "submit");
    _r.appendAttribute("name",  this.elementNameInContext(_ctx));
    
    if (this.value != null) {
      _r.appendAttribute("value",
                         this.value.stringValueInComponent(_ctx.cursor()));
    }
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(_ctx.cursor()))
        _r.appendAttribute("disabled", "disabled");
    }
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    // TODO: otherTagString
    
    _r.appendBeginTagClose();
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "action",   this.action);
    this.appendAssocToDescription(_d, "pageName", this.pageName);
  }  
}
