/*
 * This file is part of Nuts Framework.
 * Copyright(C) 2009-2012 Nuts Develop Team.
 *
 * Nuts Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License any later version.
 *
 * Nuts Framework 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nuts Framework. If not, see <http://www.gnu.org/licenses/>.
 */
package nuts.exts.struts2.components;


import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

/**
 * <!-- START SNIPPET: javadoc -->
 * Render a submit button. The submit tag is used together with the form tag to provide asynchronous form submissions.
 * The submit can have three different types of rendering:
 * <ul>
 * <li>input: renders as html &lt;input type="submit"...&gt;</li>
 * <li>image: renders as html &lt;input type="image"...&gt;</li>
 * <li>button: renders as html &lt;button type="submit"...&gt;</li>
 * </ul>
 * Please note that the button type has advantages by adding the possibility to seperate the submitted value from the
 * text shown on the button face, but has issues with Microsoft Internet Explorer at least up to 6.0
 * <!-- END SNIPPET: javadoc -->
 */
@StrutsTag(
	name="submit",
	tldTagClass="org.apache.struts2.views.jsp.ui.SubmitTag",
	description="Render a submit button",
	allowDynamicAttributes=true)
public class Submit extends FormButton {

	private static final Logger LOG = LoggerFactory.getLogger(Submit.class);

	/**
	 * OPEN_TEMPLATE = "n-submit";
	 */
	final public static String OPEN_TEMPLATE = "n-submit";

	/**
	 * TEMPLATE = "n-submit-close";
	 */
	final public static String TEMPLATE = "n-submit-close";

	protected String src;
	protected String icon;
	protected String sicon;

	/**
	 * @see UIBean
	 * @param stack value stack
	 * @param request http servlet request
	 * @param response http servlet response
	 */
	public Submit(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
		super(stack, request, response);
	}

	/**
	 * @return default open template
	 */
	public String getDefaultOpenTemplate() {
		return OPEN_TEMPLATE;
	}

	protected String getDefaultTemplate() {
		return TEMPLATE;
	}

	/**
	 * @see org.apache.struts2.components.UIBean#evaluateParams()
	 */
	public void evaluateParams() {
		if ((key == null) && (value == null)) {
			value = "Submit";
		}

		if (((key != null)) && (value == null)) {
			this.value = "%{getText('"+key +"')}";
		}

		super.evaluateParams();
	}

	/**
	 * @see org.apache.struts2.components.FormButton#evaluateExtraParams()
	 */
	public void evaluateExtraParams() {
		super.evaluateExtraParams();

		if (src != null) {
			addParameter("src", findString(src));
		}
		
        if (icon != null) {
            addParameter("icon", findString(icon));
        }

        if (sicon != null) {
            addParameter("sicon", findString(sicon));
        }
	}

	/**
	 * Indicate whether the concrete button supports the type "image".
	 *
	 * @return <tt>true</tt> to indicate type image is supported.
	 */
	protected boolean supportsImageType() {
		return true;
	}

	/**
	 * @param src the image src to set
	 */
	@StrutsTagAttribute(description="Supply an image src for <i>image</i> type submit button. Will have no effect for types <i>input</i> and <i>button</i>.")
	public void setSrc(String src) {
		this.src = src;
	}


	/**
	 * @return true
	 */
	@Override
	public boolean usesBody() {
		return true;
	}

	/**
	 * Overrides to be able to render body in a template rather than always before the template
	 * @see org.apache.struts2.components.UIBean#end(java.io.Writer, java.lang.String)
	 */
	public boolean end(Writer writer, String body) {
		evaluateParams();
		try {
			addParameter("body", body);

			mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate()));
		} catch (Exception e) {
			LOG.error("error when rendering", e);
		}
		finally {
			popComponentStack();
		}

		return false;
	}

	/**
	 * @param icon the icon to set
	 */
    @StrutsTagAttribute(description="icon attribute",  type="String")
	public void setIcon(String icon) {
		this.icon = icon;
	}

	/**
	 * @param sicon the sicon to set
	 */
    @StrutsTagAttribute(description="second icon attribute",  type="String")
	public void setSicon(String sicon) {
		this.sicon = sicon;
	}

}
