package org.opengroupware.jope.appserver.elements;

import java.util.Map;

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;

public class WORadioButton extends WOInput {

  protected WOAssociation selection;
  protected WOAssociation checked;

  public WORadioButton(String _name, Map<String, WOAssociation> _assocs,
                       WOElement _template)
  {
    super(_name, _assocs, _template);

    this.selection = grabAssociation(_assocs, "selection");
    this.checked   = grabAssociation(_assocs, "checked");
  }

  /* responder */

  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return;
    }
    
    /* retrieve form values */

    String formName  = this.elementNameInContext(_ctx);
    String formValue = _rq.stringFormValueForKey(formName);
    if (formValue == null) {
      /* radio buttons are special, they are selected based upon there value */ 
      this.log.debug("got not form value for radio button elem: " + formName);
      return;
    }
    
    /* retrieve model values */
    
    if (this.value == null) { /* nothing to push to */
      this.log.error("missing value binding for element: " + this);
      return;
    }
    Object v  = this.value.valueInComponent(cursor);
    String vs = v.toString();
    
    /* check whether we are the selected radio button */
    
    if (!formValue.equals(vs)) {
      /* ok, was a different element (with the same form name) */
      
      /*
       * Note: we set the checked binding to false which implies that the
       *       checked bindings of the radio buttons MUST bind to different
       *       variables! Otherwise they will overwrite each other!
       */
      if (this.checked != null) {
        if (this.checked.isValueSettableInComponent(cursor))
          this.checked.setBooleanValue(false, cursor);
      }
      return;
    }
    
    /* yup, we are the selected radio button, fill the bindings */
    
    if (this.checked != null) {
      if (this.checked.isValueSettableInComponent(cursor))
        this.checked.setBooleanValue(true, cursor);
    }
    
    if (this.selection != null) {
      if (this.selection.isValueSettableInComponent(cursor))
        this.selection.setValue(v, cursor);
    }
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (_ctx.isRenderingDisabled())
      return;

    // Note: we MUST have a value binding
    if (this.value == null) {
      _r.appendContentString("[ERROR: radio w/o value binding]");
      this.log.error("missing value binding for radio button: " + this);
      return;
    }
    
    Object cursor   = _ctx.cursor();
    String formName = this.elementNameInContext(_ctx);
      
    _r.appendBeginTag("input");
    _r.appendAttribute("type", "radio");
    _r.appendAttribute("name", formName);
    
    Object v  = this.value.valueInComponent(cursor);
    String vs = v.toString();
    _r.appendAttribute("value", vs);
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(_ctx.cursor()))
        _r.appendAttribute("disabled", "disabled");
    }

    /* Note: the 'checked' binding has precedence, but its better to use either
     *       'checked' _or_ 'selection'.
     */
    if (this.checked != null) {
      if (this.checked.booleanValueInComponent(_ctx.cursor()))
        _r.appendAttribute("checked", "checked");
    }
    else if (this.selection != null) {
      /* compare selection with value */
      Object s = this.selection.valueInComponent(cursor);
      if (v == s)
        _r.appendAttribute("checked", "checked");
      else if (v != null && v.equals(s))
        _r.appendAttribute("checked", "checked");
    }
    else {
      /* if the button isn't handled by the page but by some DirectAction or
       * other script, its not an error not to have those bindings
       */
      this.log.info("no selection or checked binding set for radio button");
    }
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    // TODO: otherTagString
    
    _r.appendBeginTagClose();
  }

  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "selection", this.selection);
    this.appendAssocToDescription(_d, "checked",   this.checked);
  }  
}
