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

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;

/**
 * @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();
		if (phaseId.equals(PhaseId.RESTORE_VIEW)) {
			Object portletPhase = context.getExternalContext().getRequestMap()
					.get(Bridge.PORTLET_LIFECYCLE_PHASE);
			if (null == portletPhase) {
				// 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();
				commonAjaxParameters
						.put(
								AbstractExternalContext.ACTION__PARAMETER,
								requestParameters
										.get(AbstractExternalContext.ACTION__PARAMETER));
				commonAjaxParameters
						.put(
								AbstractExternalContext.PORTLET_MODE_PARAMETER,
								requestParameters
										.get(AbstractExternalContext.PORTLET_MODE_PARAMETER));

				commonAjaxParameters
						.put(
								AbstractExternalContext.NAMESPACE_PARAMETER,
								requestParameters
										.get(AbstractExternalContext.NAMESPACE_PARAMETER));

				commonAjaxParameters
						.put(
								AbstractExternalContext.PORTLET_NAME_PARAMETER,
								requestParameters
										.get(AbstractExternalContext.PORTLET_NAME_PARAMETER));

			} else if (Bridge.PortletPhase.RenderPhase.equals(portletPhase)) {
				context.renderResponse();
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
	 */
	public void beforePhase(PhaseEvent event) {
		PhaseId phaseId = event.getPhaseId();
		FacesContext context = event.getFacesContext();
		if (phaseId.equals(PhaseId.RENDER_RESPONSE)) {
			Object portletPhase = context.getExternalContext().getRequestMap()
					.get(Bridge.PORTLET_LIFECYCLE_PHASE);
			if (null == portletPhase) {
				// save request scope variables and Faces Messages.
				PortletViewState windowState = PortletStateHolder.getInstance(
						context).getWindowState(context);
				windowState.saveRequest(context);
			}
		}
	}

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

}
