package com.ejie.ad74a.util.constantes;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.util.PropertyPlaceholderHelper;

/**
 * 
 * @author GFI-NORTE
 * 
 */
@Component(value = "applicationProperties")
public class PropertiesConstantes extends PropertyPlaceholderConfigurer {

	private static final Logger logger = LoggerFactory.getLogger(PropertiesConstantes.class);

	private static Map<String, String> propertiesMap;

	public static enum Props {
		ES_LOCAL("entorno.local"), //
		URL_MI_CARPETA("url.micarpeta"), //
		URL_LIBRO_REGISTRO("url.libroregistro"), //
		URL_TOOLKIT("url.toolkit"), //
		URL_AE01B("ae01b.url"), //
		MAIL_HOST("mail.host"), //
		MAIL_PROTOCOL("mail.protocol"), //
		MAIL_PORT("mail.port"), //
		MAIL_FROM("mail.from"), //
		MAIL_GRUPO("mail.grupo"), //
		MAIL_ENVIO_REAL("mail.envio.real"), //
		MAIL_TEST_ADDRESS("mail.test.address"), //
		MAIL_PLANTILLA_PATH("mail.plantillas.path"), //
		ENTORNO("entorno.aplicacion"), //
		NISAE_COD_PROCEDIMIENTO("nisae.cod.procedimiento");//

		private String name;

		/**
		 * Constructor del enumerado
		 * 
		 * @param name String
		 */
		private Props(String name) {
			this.name = name;
		}

		/**
		 * Obtiene el valor de la propiedad
		 * 
		 * @return String
		 */
		public String getValue() {
			return getProperty(this.name);
		}
	}

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactory, final Properties props)
			throws BeansException {
		super.processProperties(beanFactory, props);
		PropertiesConstantes.propertiesMap = new HashMap<String, String>();
		final PropertyPlaceholderHelper.PlaceholderResolver resolver = new PropertyPlaceholderHelper.PlaceholderResolver() {
			@Override
			public String resolvePlaceholder(final String key) {
				return props.getProperty(key);
			}
		};
		final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX,
				DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, true);
		for (final Object key : props.keySet()) {
			PropertiesConstantes.propertiesMap.put((String) key,
					helper.replacePlaceholders(props.getProperty((String) key), resolver));
		}

	}

	/**
	 * Obtiene el valor de una propiedad definida en el fichero de properties de
	 * seguridad
	 * 
	 * @param name String name
	 * @return String String
	 */
	public static String getProperty(String name) {
		PropertiesConstantes.logger.trace("getProperty {}", name);
		return PropertiesConstantes.propertiesMap.get(name);
	}
}
