package com.fitbank.web;

import com.fitbank.common.properties.PropertiesHandler;
import com.fitbank.util.MultiplePropertyResourceBundle;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.sf.json.JSONObject;
import org.apache.commons.collections.EnumerationUtils;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;

public final class ParametrosWeb {

    private static final MultiplePropertyResourceBundle BUNDLE =
            new MultiplePropertyResourceBundle("parametros");

    private static final Configuration FIT_BUNDLE = 
            PropertiesHandler.getConfig("parametrosweb");

    /**
     * No se puede construir.
     */
    private ParametrosWeb() {
    }

    /**
     * Obiene el valor como String
     * 
     * @param clase
     * @param nombre
     * @return Valor
     */
    public static String getValueString(Class<?> clase, String nombre) {
        String property = StringUtils.EMPTY;
        if (clase != null) {
            property = clase.getName() + ".";
        }

        property = property + nombre;
        return FIT_BUNDLE.getString(property, BUNDLE.getString(property));
    }

    /**
     * Obiene el valor como boolean
     * 
     * @param clase
     * @param nombre
     * @return Valor
     */
    public static boolean getValueBoolean(Class<?> clase, String nombre) {
        String property = StringUtils.EMPTY;
        if (clase != null) {
            property = clase.getName() + ".";
        }

        property = property + nombre;
        String value = FIT_BUNDLE.getString(property, BUNDLE.getString(property));
        return "1".equals(value) || "true".equalsIgnoreCase(value);
    }

    /**
     * Obiene el valor como arreglo de String (separados por coma)
     *
     * @param clase Clase a la que le pertenece la propiedad
     * @param nombre Nombre de la propiedad a buscar
     * @return Valor
     */
    public static List<String> getValueStringList(Class<?> clase, String nombre) {
        String singleValue = ParametrosWeb.getValueString(clase, nombre);
        String[] values = singleValue.split(",");
        return Arrays.asList(values);
    }

    /**
     * Obtiene parámetros genéricos que estarán disponibles en el entorno. Estos
     * parámetros deben estar en el archivo parametros.properties y deben empezar
     * con el prefijo "fitbank".
     * @return Devuelve todos los parámetros encontrados en dicho archivo que
     *  inicien con "fitbank" como un objeto javascript.
     */
    public static String obtenerParametrosJS() {
        JSONObject json = new JSONObject();
        String clasePrefijo = "fitbank";

        Set<String> sKeys = new HashSet<String>(EnumerationUtils.toList(BUNDLE.getKeys()));
        Set<String> sFitKeys = new HashSet<String>(IteratorUtils.toList(FIT_BUNDLE.getKeys()));
        sKeys.addAll(sFitKeys);
        for (String key : sKeys) {
            if (key.startsWith(clasePrefijo)) {
                json.put(key, ParametrosWeb.getValueString(null, key));
            }
        }

        return "var Parametros = " + json.toString(4) + ";";
    }

}
