package org.openntf.xsp.debugtoolbar.lifecycle;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;

import org.openntf.xsp.debugtoolbar.beans.DebugToolbarBean;

import com.ibm.commons.util.StringUtil;

/*
 * @author Tony McGuckin, IBM
 * 
 * Originally found at the XSnippets site: http://openntf.org/XSnippets.nsf/snippet.xsp?id=xrpl-isphase-api
 * 
 * All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0.
 */
public class DebugBeanPhaseListener implements javax.faces.event.PhaseListener {

	private static final long serialVersionUID = 1L;

	private static DebugBeanPhaseListener instance;

	private PhaseId phaseId;

	public DebugBeanPhaseListener() {
		instance = this;
	}

	// ------------------------------------------------------------------------

	public static DebugBeanPhaseListener getInstance() {
		if (null == instance) {
			instance = new DebugBeanPhaseListener();
		}
		return instance;
	}

	// ------------------------------------------------------------------------

	public boolean isRestoreViewPhase() {
		return (null != phaseId && phaseId.equals(PhaseId.RESTORE_VIEW));
	}

	public boolean isApplyRequestValuesPhase() {
		return (null != phaseId && phaseId.equals(PhaseId.APPLY_REQUEST_VALUES));
	}

	public boolean isProcessValidationsPhase() {
		return (null != phaseId && phaseId.equals(PhaseId.PROCESS_VALIDATIONS));
	}

	public boolean isUpdateModelValuesPhase() {
		return (null != phaseId && phaseId.equals(PhaseId.UPDATE_MODEL_VALUES));
	}

	public boolean isInvokeApplicationPhase() {
		return (null != phaseId && phaseId.equals(PhaseId.INVOKE_APPLICATION));
	}

	public boolean isRenderResponsePhase() {
		return (null != phaseId && phaseId.equals(PhaseId.RENDER_RESPONSE));
	}

	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

	// ------------------------------------------------------------------------

	public void beforePhase(PhaseEvent event) {
		try {
			phaseId = event.getPhaseId();

			if (doDebug(event)) {
				DebugToolbarBean dBar = DebugToolbarBean.get();
				if (dBar.isDebugLifecycle()) {
					if (event.getPhaseId().equals(PhaseId.RESTORE_VIEW)) {
						dBar.addDivider();
						dBar.debug("Request:\tStarted...");
					}
					//dBar.debug(" ");
					dBar.debug("Lifecycle:\tBefore Phase: "
							+ event.getPhaseId());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// ------------------------------------------------------------------------

	public void afterPhase(PhaseEvent event) {
		try {
			phaseId = event.getPhaseId();

			if (doDebug(event)) {

				DebugToolbarBean dBar = DebugToolbarBean.get();

				if (dBar.isDebugLifecycle()) {

					dBar.debug(event.getFacesContext().getExternalContext()
							.getRequestParameterMap().get("$$ajaxid"));

					dBar.debug("Lifecycle:\tAfter Phase: " + event.getPhaseId());
					//dBar.debug(" ");
					if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
						dBar.debug("Request:\tCompleted.");
						dBar.addDivider();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean doDebug(PhaseEvent event) {

		String ajaxId = (String) event.getFacesContext().getExternalContext()
				.getRequestParameterMap().get("$$ajaxid");

		// only debug messages not coming from the debug toolbar
		return StringUtil.isEmpty(ajaxId) || !ajaxId.endsWith("debugToolbar");

	}

} // end DebugBeanPhaseListener

@@ -657,16 +657,17 @@
 D = 1L;%0A
+%09
 %0A%09privat
@@ -708,16 +708,73 @@
 stance;%0A
+%09%0A%09private static final String LOG_CONTEXT = %22lifecycle%22;
 %0A%09privat
@@ -2335,18 +2335,17 @@
 Request:
-%5Ct
+ 
 Started.
@@ -2351,71 +2351,77 @@
 ...%22
-);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09//dBar.debug(%22 %22);%0A%09%09%09%09%09dBar.debug(%22Lifecycle:%5Ct
+, LOG_CONTEXT);%0A%09%09%09%09%09%09dBar.debug(%22 %22);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%09dBar.debug(%22
 Befo
@@ -2455,24 +2455,37 @@
 getPhaseId()
+, LOG_CONTEXT
 );%0A%09%09%09%09%7D%0A%09%09%09
@@ -2843,134 +2843,9 @@
 bug(
-event.getFacesContext().getExternalContext()%0A%09%09%09%09%09%09%09.getRequestParameterMap().get(%22$$ajaxid%22));%0A%0A%09%09%09%09%09dBar.debug(%22Lifecycle:%5Ct
+%22
 Afte
@@ -2875,24 +2875,37 @@
 haseId()
+, LOG_CONTEXT
 );%0A%09%09%09%09%09
 //dBar.d
@@ -2896,18 +2896,16 @@
 );%0A%09%09%09%09%09
-//
 dBar.deb
@@ -2909,24 +2909,29 @@
 debug(%22 %22);%0A
+%09%09%09%09%0A
 %09%09%09%09%09if (eve
@@ -3011,10 +3011,9 @@
 est:
-%5Ct
+ 
 Comp
@@ -3019,16 +3019,29 @@
 pleted.%22
+, LOG_CONTEXT
 );%0A%09%09%09%09%09

format for debug messages from the phase listeners changed