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.WOResourceManager;
import org.opengroupware.jope.appserver.WOResponse;

/*
 * WOBody
 * 
 * Can be used to generate a <body> tag with a dynamic background image.
 * 
 * Sample:
 *   Body: WOBody {
 *     src = "/images/mybackground.gif";
 *   }
 * 
 * Renders:
 *   <body background="/images/mybackground.gif">[sub-template]</body>
 *   
 * Bindings:
 *   filename  [in] - string
 *   framework [in] - string
 *   src       [in] - string
 *   value     [in] - byte array?
 * 
 * TODO: document me
 * TODO: implement me
 */
public class WOBody extends WOHTMLDynamicElement {

  protected WOAssociation filename;
  protected WOAssociation framework;
  protected WOAssociation src;
  protected WOAssociation value;
  protected WOElement     template;
  
  public WOBody(String _name, Map<String, WOAssociation> _assocs,
                WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.filename  = grabAssociation(_assocs, "filename");
    this.framework = grabAssociation(_assocs, "framework");
    this.src       = grabAssociation(_assocs, "src");
    this.value     = grabAssociation(_assocs, "value");
    this.template  = _template;
    
    // TODO: add warnings for invalid setups, missing template etc
  }
  
  /* URL generation */
  
  protected String urlInContext(WOContext _ctx) {
    Object cursor = _ctx.cursor();
    String s = null;
    
    // TODO: replace with WOLinkGenerator!
    if (this.filename != null) {
      String fn = this.filename.stringValueInComponent(cursor);
      String fw = null;
      if (this.framework != null)
        fw = this.framework.stringValueInComponent(cursor);
      
      if (fn != null && fn.length() > 0) {
        WOResourceManager rm = _ctx.application().resourceManager();
        
        s = rm.urlForResourceNamed(fn, fw, _ctx.languages(), _ctx);
        if (s != null)
          return s;
      }
    }
    
    if (this.src != null) {
      if ((s = this.src.stringValueInComponent(cursor)) != null)
        return s;
    }
    
    if (this.value != null) {
      // TODO: implement me, this uses component actions? or resource manager
      //       keys.
    }
    
    return null;
  }
  
  /* responder */
  
  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    if (this.template != null)
      this.template.takeValuesFromRequest(_rq, _ctx);
  }
  
  public WOActionResults invokeAction(WORequest _rq, WOContext _ctx) {
    return (this.template != null)
      ? this.template.invokeAction(_rq, _ctx) : null;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (_ctx.isRenderingDisabled()) {
      if (this.template != null)
        this.template.appendToResponse(_r, _ctx);
      return;
    }
    
    _r.appendBeginTag("body");
    
    String bgurl = this.urlInContext(_ctx);
    if (bgurl != null && bgurl.length() > 0)
      _r.appendAttribute("background", bgurl);
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    _r.appendBeginTagEnd();
    
    if (this.template != null)
      this.template.appendToResponse(_r, _ctx);
    
    _r.appendEndTag("body");
  }
}
