package it.mice.voila.runtime.web.filter;

import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;

public class RuntimeMultipartResolver extends StandardServletMultipartResolver {

	/**
	 * Set whether to resolve the multipart request lazily at the time of file
	 * or parameter access.
	 * <p>
	 * Default is "false", resolving the multipart elements immediately,
	 * throwing corresponding exceptions at the time of the
	 * {@link #resolveMultipart} call. Switch this to "true" for lazy multipart
	 * parsing, throwing parse exceptions once the application attempts to
	 * obtain multipart files or parameters.
	 */
	public void setResolveLazily(boolean resolveLazily) {
		if (resolveLazily) {
			throw new IllegalArgumentException(
					"Cannot use resolveLazily for this type of Multipart Request");
		}
	}

	public MultipartHttpServletRequest resolveMultipart(
			HttpServletRequest request) throws MultipartException {

		ServletRequest appoRequest = request;
		while (appoRequest instanceof HttpServletRequestWrapper) {
			if (appoRequest instanceof MultipartHttpServletRequest) {
				return (MultipartHttpServletRequest)appoRequest;
			}
			appoRequest = ((HttpServletRequestWrapper)appoRequest).getRequest(); 
		}
		return super.resolveMultipart(request);
	}

	public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request, String submitButtonPrefix) throws MultipartException {
		Assert.notNull(request, "Request must not be null");
		StandardMultipartHttpServletRequest standardMultipartHttpServletRequest = new StandardMultipartHttpServletRequest(request);
		Map<String, String[]> parametersMap = standardMultipartHttpServletRequest.getParameterMap();
		Iterator<String> i = parametersMap.keySet().iterator();
		while (i.hasNext()) {
			String parmName = (String) i.next();
            if (parmName.startsWith(submitButtonPrefix)) {
                int endIndex = parmName.indexOf("?");
                String methodName;
                String queryString = null;
                if (endIndex == -1) {
                    methodName = parmName.substring(submitButtonPrefix.length());
                } else {
                    methodName = parmName.substring(submitButtonPrefix.length(), endIndex);
                    queryString = parmName.substring(endIndex + 1);                    
                    parametersMap.putAll(RequestUtils.convertToParameterMap(queryString));
                }
        		return new RuntimeMultipartHttpServletRequest(request,
        				standardMultipartHttpServletRequest.getMultiFileMap(),
        				parametersMap, methodName, queryString);
            }

		}
		
		return new RuntimeMultipartHttpServletRequest(request,
				standardMultipartHttpServletRequest.getMultiFileMap(),
				parametersMap, null, null);
	}

}
