package com.aloeapps.common.helper;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

public class JSFResourceBundleHelper {

	private static Logger logger = Logger.getLogger(JSFResourceBundleHelper.class);

	private static final String BUNDLE_VAR = "msg";

	public static String getMessageCore(String msgKey) {
		return getMessageBundleString(FacesContext.getCurrentInstance(),
				msgKey, null);
	}

	public static String getMessageCoreParams(String msgKey,
			Object... msgParams) {
		return getMessageBundleString(FacesContext.getCurrentInstance(),
				msgKey, msgParams);
	}

	public static String getMessageVar(String resourceBundleVar, String msgKey) {
		return getResourceBundleString(FacesContext.getCurrentInstance(),
				resourceBundleVar, msgKey, null);
	}

	public static String getMessageVarParams(String resourceBundleVar,
			String msgKey, Object... msgParams) {
		return getResourceBundleString(FacesContext.getCurrentInstance(),
				resourceBundleVar, msgKey, msgParams);
	}

	public static String getMessageAloe(String msgKey) {
		return getResourceBundleString(FacesContext.getCurrentInstance(),
				BUNDLE_VAR, msgKey, null);
	}

	public static String getMessageAloeParams(String msgKey,
			Object... msgParams) {
		return getResourceBundleString(FacesContext.getCurrentInstance(),
				BUNDLE_VAR, msgKey, msgParams);
	}

	private static String getMessageBundleString(FacesContext facesContext,
			String msgKey, Object[] msgParams) {

		if (null == facesContext) {
			logger.error("Error getting key: " + msgKey
					+ ". FacesContext is null.");
			return "Error getting key: " + msgKey + ". FacesContext is null.";
		}

		try {
			String rbPath = facesContext.getApplication().getMessageBundle();
			Locale locale = facesContext.getViewRoot().getLocale();
			ResourceBundle resourceBundle = ResourceBundle.getBundle(rbPath,
					locale);
			return getString(resourceBundle, msgKey, msgParams);
		} catch (Exception e) {
			logger.error("Error getting key: " + msgKey + ", msgParams: "
					+ (msgParams == null ? "null" : msgParams.toString()), e);
			return "Error getting key: " + msgKey + ", msgParams: "
					+ (msgParams == null ? "null" : msgParams.toString());
		}
	}

	private static String getResourceBundleString(FacesContext facesContext,
			String resourceBundleVar, String msgKey, Object[] msgParams) {
		if (null == facesContext) {
			logger.error("Error getting key: " + msgKey
					+ ". FacesContext is null.");
			return "Error getting key: " + msgKey + ". FacesContext is null.";
		}

		ResourceBundle resourceBundle = facesContext.getApplication()
				.getResourceBundle(facesContext, resourceBundleVar);

		if (null == resourceBundle) {
			logger.error("Error getting key: " + msgKey
					+ ". ResourceBundle with varName \"" + resourceBundleVar
					+ "\" not found.");
			return "Error getting key: " + msgKey
					+ ". ResourceBundle with varName \"" + resourceBundleVar
					+ "\" not found.";
		}

		return getString(resourceBundle, msgKey, msgParams);
	}

	private static String getString(ResourceBundle resourceBundle,
			String msgKey, Object[] msgParams) {

		if (null == resourceBundle) {
			logger.error("Error getting key: " + msgKey
					+ ". ResourceBundle is null.");
			return "Error getting key: " + msgKey + ". ResourceBundle is null.";
		}

		try {
			String msg = resourceBundle.getString(msgKey);
			if (null != msgParams) {

				MessageFormat mf = new MessageFormat(msg);
				msg = mf.format(msgParams);
			}
			return msg;

		} catch (MissingResourceException e) {
			return "???" + msgKey + "???";
		} catch (Exception e) {
			logger.error("Error getting key: " + msgKey + ", msgParams: "
					+ (msgParams == null ? "null" : msgParams.toString()), e);
			return "Error getting key: " + msgKey + ", msgParams: "
					+ (msgParams == null ? "null" : msgParams.toString());
		}
	}
}
