package org.ajax4jsf.bean;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.sun.faces.context.FacesContextImpl;

public class A4JActionParam {
	
	private static final String PARAMETER_NAME = "param";
	
	private static final String PARAMETER_VALUE = "Action Parameter";
	
	public static class ERRORS {
		public static final String NO_PARAM = "1"; 
		
		public static final String INVALID_VALUE = "2";
		
		public static final String NOT_ASSIGNED = "3";
	}
	
	private String parameterName;
	
	private String parameterValue;
	
	private String errorMessage = null;
	
	public void listener(ActionEvent event) {
		FacesContext context = FacesContextImpl.getCurrentInstance();
		Map<String, String> p = context.getExternalContext().getRequestParameterMap();
		if (p.get(PARAMETER_NAME) == null) {
			errorMessage = ERRORS.NO_PARAM;
		} else if (!PARAMETER_VALUE.equals(p.get(PARAMETER_NAME))) {
			errorMessage = ERRORS.INVALID_VALUE;
		} else if (parameterValue == null) {
			errorMessage = ERRORS.NOT_ASSIGNED;
		} else {
			parameterName = PARAMETER_NAME;
		}
		
	}

	/**
	 * @return the parameter
	 */
	public String getParameter() {
		return (errorMessage == null && (parameterName != null || parameterValue!=null)) ? (parameterName + "='" + parameterValue + "'") : "";
	}

	/**
	 * @param parameter the parameter to set
	 */
	public void setParameter(String parameter) {
		;
	}

	/**
	 * @return the parameterValue
	 */
	public String getParameterValue() {
		return parameterValue;
	}

	/**
	 * @param parameterValue the parameterValue to set
	 */
	public void setParameterValue(String parameterValue) {
		this.parameterValue = parameterValue;
	}
	
	

}
