/*
  Copyright (C) 2006 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

package org.opengroupware.jope.appserver.elements;

import java.util.Map;

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

/*
 * WOImageButton
 * 
 * Create HTML form textfields.
 * 
 * Sample:
 *   Firstname: WOImageButton {
 *     src   = "/images/ok.gif";
 *     name  = "ok";
 *     value = firstname;
 *   }
 * 
 * Renders:
 *   <input type="image" name="ok" src="/images/ok.gif" />
 * 
 * Bindings (WOInput):
 *   name      [in]  - string
 *   value     [io]  - object
 *   disabled  [in]  - boolean
 *   
 * Bindings:
 *   action    [in]  - action
 *   pageName  [in]  - string
 *   x         [out] - int
 *   y         [out] - int
 *   
 * Bindings (WOLinkGenerator for image resource):
 *   filename         [in] - string
 *   framework        [in] - string
 *   src              [in] - string
 *   actionClass      [in] - string
 *   directActionName [in] - string
 *   queryDictionary  [in] - Map<String,String>
 *   ?wosid           [in] - boolean (constant!)
 *   - all bindings starting with a ? are stored as query parameters.
 */
public class WOImageButton extends WOInput {
  
  protected WOAssociation   action;
  protected WOAssociation   pageName;
  protected WOAssociation   x;
  protected WOAssociation   y;
  protected WOLinkGenerator link;
  // TODO: add support for disabledFilename? (SOPE)
  
  public WOImageButton(String _name, Map<String, WOAssociation> _assocs,
                       WOElement _template)
  {
    super(_name, _assocs, _template);
    
    this.action   = grabAssociation(_assocs, "action");
    this.pageName = grabAssociation(_assocs, "pageName");
    this.x        = grabAssociation(_assocs, "x");
    this.y        = grabAssociation(_assocs, "y");
    
    this.link = WOLinkGenerator
      .rsrcLinkGeneratorForAssociations("src", _assocs);
    
    if (this.value != null)
      log.warn("WOImageButton does not use 'value' binding ...");
    if (this.action != null && this.pageName != null)
      log.warn("Both, 'action' and 'pageName' bindings are specified ..");
  }

  public void takeValuesFromRequest(WORequest _rq, WOContext _ctx) {
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return;
    }
    
    String baseId = this.elementNameInContext(_ctx);
    String xVal   = _rq.stringFormValueForKey(baseId + ".x");
    String yVal   = _rq.stringFormValueForKey(baseId + ".x");
    
    if (xVal != null && xVal.length() > 0 && this.x != null)
      this.x.setIntValue(Integer.parseInt(xVal), cursor);
    if (yVal != null && yVal.length() > 0 && this.y != null)
      this.x.setIntValue(Integer.parseInt(xVal), cursor);
    
    if (this.value != null && this.value.isValueSettableInComponent(cursor))
      this.value.setValue("x=" + xVal + "&y=" + yVal, cursor);
    
    if (xVal != null && yVal != null) {
      if (this.action != null || this.pageName != null)
        _ctx.addActiveFormElement(this);
    }
  }
  
  public Object invokeAction(WORequest _rq, WOContext _ctx) {
    // TODO: can we share the code with WOSubmitButton?
    Object cursor = _ctx.cursor();
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(cursor))
        return null;
    }
    
    if (!_ctx.elementID().equals(_ctx.senderID())) {
      // TODO: print a log?
      return null;
    }
    
    if (this.action != null)
      return this.action.valueInComponent(cursor);
    
    if (this.pageName != null) {
      String pname;
      
      pname = this.pageName.stringValueInComponent(cursor);
      if (pname == null) {
        // TODO: print a log
        return null;
      }
      
      return _ctx.application().pageWithName(pname, _ctx);
    }
    
    return null;
  }
  
  public void appendToResponse(WOResponse _r, WOContext _ctx) {
    if (_ctx.isRenderingDisabled())
      return;

    _r.appendBeginTag("input");
    _r.appendAttribute("type",  "image");
    _r.appendAttribute("name",  this.elementNameInContext(_ctx));
    
    if (this.link != null)
      _r.appendAttribute("src", this.link.fullHrefInContext(_ctx));
    
    if (this.disabled != null) {
      if (this.disabled.booleanValueInComponent(_ctx.cursor()))
        _r.appendAttribute("disabled", "disabled");
    }
    
    this.appendExtraAttributesToResponse(_r, _ctx);
    // TODO: otherTagString
    
    _r.appendBeginTagClose();
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    this.appendAssocToDescription(_d, "action",   this.action);
    this.appendAssocToDescription(_d, "pageName", this.pageName);
    this.appendAssocToDescription(_d, "x",        this.x);
    this.appendAssocToDescription(_d, "y",        this.y);
    
    if (this.link != null) {
      _d.append(" src=");
      _d.append(this.link.toString());
    }
  }  
}
