package org.opengroupware.jope.appserver.elements;

import java.util.HashMap;
import java.util.Map;

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

/*
 * WOSwitchComponent
 * 
 * Bindings:
 *   WOComponentName - String [in]
 *   other bindings  - will be bindings for the instantiated component
 */
public class WOSwitchComponent extends WOHTMLDynamicElement {
  
  protected WOAssociation              componentName;
  protected WOElement                  template;
  protected Map<String, WOAssociation> bindings;
  
  public WOSwitchComponent(String _name, Map<String, WOAssociation> _assocs,
                           WOElement _template)
  {
    super(_name, _assocs, _template);

    this.componentName = grabAssociation(_assocs, "WOComponentName");
    this.template      = _template;
    
    if (_assocs != null) {
      _assocs.remove("NAME");
      this.bindings = new HashMap<String, WOAssociation>(_assocs);
      _assocs.clear();
    }
  }

  /* component lookup */
  
  protected WOComponent lookupComponent(String _name, WOContext _ctx) {
    if (_name == null)
      return null;
    
    WOComponent component = _ctx.component().pageWithName(_name);
    if (component == null)
      return null;
    
    component.setParent(_ctx.component());
    component.setBindings(this.bindings);
    return component;
  }
  
  /* responder */
  
  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    String      cname = this.componentName.stringValueInComponent(_ctx);
    WOComponent c     = this.lookupComponent(cname, _ctx);
    if (c == null) return;
    
    cname = cname.replace('.', '_'); /* escape for EID */
    _ctx.appendElementIDComponent(cname);
    _ctx.enterComponent(c, this.template);
    c.takeValuesFromRequest(_rq, _ctx);
    _ctx.leaveComponent(c);
    _ctx.deleteLastElementIDComponent();
  }
  
  public WOActionResults invokeAction(WORequest _rq, WOContext _ctx) {
    // TODO: implement me to support component actions
    return null;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    String cname = this.componentName.stringValueInComponent(_ctx.component());
    
    WOComponent c = this.lookupComponent(cname, _ctx);
    if (c == null) return;
    
    
    cname = cname.replace('.', '_'); /* escape for EID */
    _ctx.appendElementIDComponent(cname);
    _ctx.enterComponent(c, this.template);
    
    c.appendToResponse(_r, _ctx);
    
    _ctx.leaveComponent(c);
    _ctx.deleteLastElementIDComponent();
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "name", this.componentName);
  }  
}
