/**
 * 
 */
package org.ajax4jsf.portlet;

import java.util.Map;

import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.ajax4jsf.context.AjaxContextImpl;
import org.ajax4jsf.portlet.context.AbstractExternalContext;
import org.ajax4jsf.webapp.WebXml;

/**
 * @author asmirnov
 * 
 */
public class PortletAjaxContext extends AjaxContextImpl {
    public String getAjaxActionURL(FacesContext context) {
	// Check arguments
	if (null == context) {
	    throw new NullPointerException(
		    "Faces context for build AJAX Action URL is null");
	}
//	if (context.getExternalContext().getContext() instanceof PortletContext) {
	    UIViewRoot viewRoot = context.getViewRoot();
	    if (null == viewRoot) {
		throw new NullPointerException(
			"Faces view tree for build AJAX Action URL is null");
	    }
	    String viewId = viewRoot.getViewId();
	    if (null == viewId) {
		throw new NullPointerException(
			"View id for build AJAX Action URL is null");
	    }
	    if (!viewId.startsWith("/")) {
		throw new IllegalArgumentException(
			"Illegal view Id for build AJAX Action URL: " + viewId);
	    }
	    ExternalContext externalContext = context.getExternalContext();
	    WebXml webXml = (WebXml) externalContext.getApplicationMap().get(
		    WebXml.CONTEXT_ATTRIBUTE);
	    if (null == webXml) {
		throw new NullPointerException(
			"Ajax Filter not properly configured for web application");
	    }
	    StringBuffer actionUrl = new StringBuffer(externalContext
		    .getRequestContextPath());
	    if (webXml.isPrefixMapping()) {
		String facesFilterPrefix = webXml.getFacesFilterPrefix();
		if (facesFilterPrefix.endsWith("/")) {
		    facesFilterPrefix = facesFilterPrefix.substring(0,facesFilterPrefix.length()-1);
		}
		actionUrl.append(facesFilterPrefix).append(viewId);
	    } else {
		int dot = viewId.lastIndexOf('.');
		if (dot < 0) {
		    actionUrl.append(viewId).append(
			    webXml.getFacesFilterSuffix());
		} else {
		    actionUrl.append(viewId.substring(0, dot)).append(
			    webXml.getFacesFilterSuffix());
		}
	    }
	    // ViewHandler viewHandler =
                // context.getApplication().getViewHandler();
	    // TODO - append optional seam/portlet.. parameters
	    return externalContext.encodeActionURL(actionUrl.toString());
//	} else {
//	    return super.getAjaxActionURL(context);
//	}
    }

    public Map getCommonAjaxParameters() {
	Map map = super.getCommonAjaxParameters();
	FacesContext facesContext = FacesContext.getCurrentInstance();
	String namespace = AjaxFacesPortlet.getPortletNamespace(facesContext);
	if (null != namespace) {
	    map.put(AbstractExternalContext.NAMESPACE_PARAMETER, namespace);
	}
	String actionUrl = (String) facesContext.getExternalContext().getRequestParameterMap().get(AbstractExternalContext.ACTION__PARAMETER);
	if(null != actionUrl){
	    map.put(AbstractExternalContext.ACTION__PARAMETER, actionUrl);
	}
	String portletMode = (String) facesContext.getExternalContext().getRequestParameterMap().get(AbstractExternalContext.PORTLET_MODE_PARAMETER);
	if(null != actionUrl){
	    map.put(AbstractExternalContext.PORTLET_MODE_PARAMETER, portletMode);
	}
	return map;
    }
}
