package it.mice.voila.runtime.springmvc.taglibs;

import it.mice.voila.runtime.springmvc.interceptors.ControllerBasePathExposingInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.tags.form.TagWriter;
/**
 * sdasa
 * @author zzy9v4
 */
public class FormTag extends org.springframework.web.servlet.tags.form.FormTag {
    /**
	 * 
	 */
	private static final long serialVersionUID = 18574878L;

	private static final Log logger = LogFactory.getLog(FormTag.class);

    public static final String FORM_BEAN_NAME = FormTag.class + ".FormBeanName";
    
    private StringBuffer script = new StringBuffer();

	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		if (request.getParameter(org.displaytag.tags.TableTagParameters.PARAMETER_EXPORTING) == null) {
			TagUtils.removeSecuritySessionBean();
		}
		this.pageContext.setAttribute(FORM_BEAN_NAME, this, PageContext.REQUEST_SCOPE);
		if (TagUtils.isBootstrapTheme()) {
			setCssClass(TagUtils.safePrepend(getCssClass(), "form-horizontal "));
		}
		return super.writeTagContent(tagWriter);
	}
	
	/**
	 * Closes the '<code>form</code>' block tag and removes the form object name
	 * from the {@link javax.servlet.jsp.PageContext}.
	 */
	@Override
	public int doEndTag() throws JspException {
		super.doEndTag();
		if (script.length() > 0) {
			TagUtils.safeWrite(pageContext, "<script type=\"text/javascript\">\n\r");
			//TagUtils.safeWrite(pageContext, "function configureDecorations() {");
			TagUtils.safeWrite(pageContext, script.toString());
			//TagUtils.safeWrite(pageContext, "}");
			TagUtils.safeWrite(pageContext, "</script>");
		}
		return EVAL_PAGE;
	}
	
	@Override
	public void doFinally() {
		this.pageContext.removeAttribute(FORM_BEAN_NAME, PageContext.REQUEST_SCOPE);
		script = new StringBuffer();

		setCssClass(null);
		
		super.doFinally();
	}
	
	public String getActionUrl() throws JspException {
		return resolveAction();
	}

	/**
	 * Resolve the value of the '<code>action</code>' attribute.
	 * <p>If the user configured an '<code>action</code>' value then
	 * the result of evaluating this value is used. Otherwise, the
	 * {@link org.springframework.web.servlet.support.RequestContext#getRequestUri() originating URI}
	 * is used.
	 * @return the value that is to be used for the '<code>action</code>' attribute
	 */
	protected String resolveAction() throws JspException {
		String action = getAction();
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		String baseControllerUrl = (String)request.getAttribute(ControllerBasePathExposingInterceptor.CONTROLLER_BASE_URL);
		if (baseControllerUrl.startsWith("**")) {
			baseControllerUrl = baseControllerUrl.substring(2);
		}

		if (StringUtils.hasText(action) || !StringUtils.hasText(baseControllerUrl)) {
			return super.resolveAction(); 
		}

		return request.getContextPath() + baseControllerUrl;
	}

	public void addScript(String aScript) {
		script.append(aScript).append("\n\r");
	}

	public void addScript(StringBuffer aScript) {
		script.append(aScript).append("\n\r");
	}
	
}
