package com.ejie.ab61.comun.utilidades;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public abstract class Utils implements Serializable {

	/**
	 * Fomateo general de miles y decimales al idioma ES
	 * 
	 * @param pattern
	 *            a cumplir
	 * @param value
	 *            double
	 * @param loc
	 *            locale, en nuestro caso new Locale("es", "ES")
	 * @return String valor formateado
	 */
	private static String localizedFormat(String pattern, double value, Locale loc) {
		NumberFormat nf = NumberFormat.getNumberInstance(loc);
		DecimalFormat df = (DecimalFormat) nf;
		df.applyPattern(pattern);
		return df.format(value);
	}

	/**
	 * Formatear valores de base de datos para que cumplan ###,##0.00 y
	 * aplicandose el locale ES
	 * 
	 * @param valor
	 *            BigDecimal
	 * @return String
	 */
	public static String formatearMiles(BigDecimal valor) {
		if (valor != null) {
			return localizedFormat("###,##0.00", valor.doubleValue(), new Locale("es", "ES"));
		} else
			return "";
	}

	/**
	 * Devuelve el content type para la extension pasada por parametro
	 * 
	 * @param extension
	 *            String
	 * @return String
	 */
	public static String obtenerContentType(String extension) {

		String contentType = "";

		if ("rtf".equals(extension) || "RTF".equals(extension)) {
			contentType = "application/rtf";
		} else if ("xls".equals(extension) || "XLS".equals(extension)) {
			contentType = "application/vnd.ms-excel";
		} else if ("xlsx".equals(extension) || "XLSX".equals(extension)) {
			contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
		} else if ("doc".equals(extension) || "DOC".equals(extension)) {
			contentType = "application/msword";
		} else if ("docx".equals(extension) || "DOCX".equals(extension)) {
			contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
		}

		return contentType;
	}

}
