package org.opengroupware.jope.appserver.elements;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;

/*
 * WOHyperlink
 * 
 * Sample:
 *   Form: WOForm {
 *     directActionName = "postComment";
 *     actionClass      = "CommentPage";
 *   }
 * 
 * Renders:
 *   <form href="/servlet/app/wa/CommentPage/postComment?comment=blub">
 *     [sub-template]
 *   </a>
 * 
 * Bindings (WOLinkGenerator):
 *   href               [in] - string
 *   action             [in] - action
 *   pageName           [in] - string
 *   directActionName   [in] - string
 *   actionClass        [in] - string
 *   fragmentIdentifier [in] - string
 *   queryDictionary    [in] - Map<String,String>
 *   ?wosid             [in] - boolean (constant!)
 *   - all bindings starting with a ? are stored as query parameters.
 *   
 *   target             [in] - string
 *   method             [in] - string (POST/GET)
 *   
 *   - all bindings starting with a ? are stored as query parameters.
 */
public class WOForm extends WOHTMLDynamicElement {
  protected static Log log = LogFactory.getLog("WOForms");
  static boolean alwaysPassIn = false;
  
  /* common */
  protected WOAssociation   target;
  protected WOAssociation   method;
  protected WOElement       template;
  protected WOLinkGenerator link;
  
  public WOForm(String _name, Map<String, WOAssociation> _assocs,
                WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.target   = grabAssociation(_assocs, "target");
    this.method   = grabAssociation(_assocs, "method");
    this.link     = WOLinkGenerator.linkGeneratorForAssociations(_assocs);
    this.template = _template;
    
    if (grabAssociation(_assocs, "multipleSubmit") != null)
      ; /* not required in JOPE? */
  }

  /* responder */
  
  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    if (_ctx.isInForm())
      log.error("detected a nested form");
    
    _ctx.setIsInForm(true);
    try {
      /* apply values to ?style parameters */
      if (this.link != null)
        this.link.takeValuesFromRequest(_rq, _ctx);
      
      if (this.template != null) {
        boolean doTakeValues = false;
        if (alwaysPassIn)
          doTakeValues = true;
        else if (this.link != null)
          doTakeValues = this.link.shouldFormTakeValues(_rq, _ctx);
      
        if (doTakeValues)
          this.template.takeValuesFromRequest(_rq, _ctx);
      }
    }
    finally {
      if (!_ctx.isInForm())
        log.error("inconsistent form setup detected!");
      _ctx.setIsInForm(false);
    }
  }
  
  public WOActionResults invokeAction(WORequest _rq, WOContext _ctx) {
    if (_ctx.isInForm())
      log.error("detected a nested form");
    
    WOActionResults result = null;
    _ctx.setIsInForm(true);
    {
      //Object cursor = _ctx.cursor();
      WOElement element = _ctx.activeFormElement();
      
      if (element != null) {
        // TODO: do we need to patch the senderID?
        result = element.invokeAction(_rq, _ctx);
      }
      
      // TODO: implement me!
      
      if (this.template != null)
        result = this.template.invokeAction(_rq, _ctx);
    }
    _ctx.setIsInForm(false);

    return result;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (_ctx.isInForm())
      log.error("detected a nested form");
    
    _ctx.setIsInForm(true);
    try {
      Object cursor = _ctx.cursor();
      
      /* start form tag */
      
      _r.appendBeginTag("form");
      
      if (this.link != null) {
        String url = this.link.fullHrefInContext(_ctx);
        if (url != null) _r.appendAttribute("action", url);
      }
      
      String m = null;
      if (this.method != null)
        m = this.method.stringValueInComponent(cursor);
      _r.appendAttribute("method", m != null ? m : "POST");
      
      if (this.target != null) {
        String s = this.target.stringValueInComponent(cursor);
        _r.appendAttribute("target", s);
      }
      
      this.appendExtraAttributesToResponse(_r, _ctx);
      // TODO: otherTagString
      
      _r.appendBeginTagEnd();
      
      /* render form content */
    
      if (this.template != null)
        this.template.appendToResponse(_r, _ctx);
    
      /* render form close tag */    
      _r.appendEndTag("form");
    }
    finally {
      if (!_ctx.isInForm())
        log.error("inconsistent form setup detected!");
      _ctx.setIsInForm(false);
    }
  }
}
