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

import java.net.MalformedURLException;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.portlet.faces.Bridge;

import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.portlet.application.PortletStateHolder;
import org.ajax4jsf.portlet.application.PortletViewState;
import org.ajax4jsf.portlet.context.AbstractExternalContext;
import org.ajax4jsf.portlet.context.PortalActionURL;

/**
 * @author asmirnov
 * 
 */
public class PortalPhaseListener implements PhaseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4023885603543145666L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
	 */
	public void afterPhase(PhaseEvent event) {
		PhaseId phaseId = event.getPhaseId();
		FacesContext context = event.getFacesContext();
		Object portletPhase = context.getExternalContext().getRequestMap().get(
				Bridge.PORTLET_LIFECYCLE_PHASE);
		if (phaseId.equals(PhaseId.RESTORE_VIEW)) {
			if (Bridge.PortletPhase.RenderPhase.equals(portletPhase)) {
				context.renderResponse();
			}
		} else if (phaseId.equals(PhaseId.RENDER_RESPONSE)) {
			// save Seam conversation Id.
			PortletViewState windowState = PortletStateHolder.getInstance(
					context).getWindowState(context);
			if (null != windowState) {
				windowState.saveSeamConversationId(context);
			}
		} else if (phaseId.equals(PhaseId.INVOKE_APPLICATION)
				|| context.getRenderResponse() || context.getResponseComplete()) {
			// save request scope variables and Faces Messages for a Servlet
			// request.
			PortletViewState windowState = PortletStateHolder.getInstance(
					context).getWindowState(context);
			if (null != windowState) {
				if (null == portletPhase) {
					windowState.saveRequest(context);
				} else if (Bridge.PortletPhase.ActionPhase.equals(portletPhase)) {
					windowState.saveSeamConversationId(context);
				}

			}
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
	 */
	@SuppressWarnings("unchecked")
	public void beforePhase(PhaseEvent event) {
		PhaseId phaseId = event.getPhaseId();
		FacesContext context = event.getFacesContext();
		Object portletPhase = context.getExternalContext().getRequestMap().get(
				Bridge.PORTLET_LIFECYCLE_PHASE);
		if (null == portletPhase && phaseId.equals(PhaseId.RENDER_RESPONSE)) {
			// For a servlet phase, put all portlet-related parameters
			// back to ajaxContext.
			Map<String, String> requestParameters = context
					.getExternalContext().getRequestParameterMap();

			AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
			Map commonAjaxParameters = ajaxContext.getCommonAjaxParameters();
			PortletStateHolder portletStateHolder = PortletStateHolder
					.getInstance(context);
			String stateId = requestParameters
					.get(PortletStateHolder.STATE_ID_PARAMETER);
			// Change viewId parameter in the portal action URL.
			PortletViewState windowState = portletStateHolder
					.getWindowState(stateId);
			if (null != windowState) {
				PortalActionURL pal = windowState.getPortalActionURL();
				if (null != pal) {
					pal.addParameter(AbstractExternalContext.VIEW_ID_PARAMETER,
							context.getViewRoot().getViewId());
				}
			}
			commonAjaxParameters.put(PortletStateHolder.STATE_ID_PARAMETER,
					stateId);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.event.PhaseListener#getPhaseId()
	 */
	public PhaseId getPhaseId() {
		// This listener process all phases.
		return PhaseId.ANY_PHASE;
	}

}
