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;

/*
 * WOFrame
 * 
 * Can be used to generate a <frame> tag with a dynamic content URL.
 * 
 * Sample:
 *   Frame: WOFrame {
 *     actionClass      = "LeftMenu";
 *     directActionName = "default";
 *   }
 * 
 * Renders:
 *   <frame src="/App/x/LeftMenu/default">[sub-template]</frame>
 *   
 * Bindings:
 *   name             [in] - string
 *   href             [in] - string
 *   directActionName [in] - string
 *   actionClass      [in] - string
 *   pageName         [in] - string
 *   action           [in] - action
 * 
 * TODO: document me
 * TODO: implement me
 */
public class WOFrame extends WOHTMLDynamicElement {

  protected WOAssociation   name;
  protected WOElement       template;
  protected WOLinkGenerator link;
  
  public WOFrame(String _name, Map<String, WOAssociation> _assocs,
                 WOElement _template)
  {
    super(_name, _assocs, _template);

    this.name = grabAssociation(_assocs, "name");
    this.link = WOLinkGenerator.linkGeneratorForAssociations(_assocs);
    this.template = _template;
  }

  /* responder */
  
  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    /* links can take form values !!!! (for query-parameters) */
    
    if (this.link != null)
      this.link.takeValuesFromRequest(_rq, _ctx);
    
    if (this.template != null)
      this.template.takeValuesFromRequest(_rq, _ctx);
  }
  
  public WOActionResults invokeAction(WORequest _rq, WOContext _ctx) {
    // TODO: add support for page/action links
    
    if (this.template != null)
      return this.template.invokeAction(_rq, _ctx);

    return null;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    _r.appendBeginTag("frame");
    
    if (this.link != null) {
      String url = this.link.fullHrefInContext(_ctx);
      if (url != null) _r.appendAttribute("src", url);
    }
    
    if (this.name != null) {
      String s = this.name.stringValueInComponent(_ctx.cursor());
      if (s != null) _r.appendAttribute("name", s);
    }
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    // TODO: otherTagString
    
    if (this.template != null) {
      _r.appendBeginTagEnd(); /* end begin tag */
      
      /* render content */
      
      this.template.appendToResponse(_r, _ctx);
    
      /* render frame close tag */
    
      _r.appendEndTag("frame");
    }
    else
      _r.appendBeginTagClose();
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    if (this.link != null) {
      _d.append(" link=");
      _d.append(this.link.toString());
    }
  }  
}
