/**
 * Licensed under the MICE s.r.l.
 * End User License Agreement
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.micegroup.it/voila/license.html
 */
package it.mice.voila.runtime.displaytag.basedec;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import org.displaytag.decorator.ColumnDecorator;
import org.displaytag.decorator.ColumnDecoratorHandler;
import org.displaytag.properties.TableProperties;

/**
 * Display Column Decorator base per la trasformazione dei campi numerici.
 */
public abstract class ValueDecorator implements ColumnDecorator, ColumnDecoratorHandler {
	private PageContext pageContext = null;
	public static final String CURRENCY_SYMBOL_KEY = "label.format.currencySymbol";
	public static final String DATE_FORMAT_KEY = "label.format.date";
	public static final String DATETIME_FORMAT_KEY = "label.format.timestamp";
	public static final String CURRENCY_0DECIMAL_KEY = "label.format.0decimal";
	public static final String CURRENCY_1DECIMAL_KEY = "label.format.1decimal";
	public static final String CURRENCY_2DECIMAL_KEY = "label.format.2decimal";
	public static final String CURRENCY_3DECIMAL_KEY = "label.format.3decimal";
	public static final String CURRENCY_4DECIMAL_KEY = "label.format.4decimal";
	public static final String CURRENCY_5DECIMAL_KEY = "label.format.5decimal";
	public static final String NUMBER_GROUP_SEPARATOR = "label.format.groupSeparator";
	public static final String NUMBER_DECIMAL_SEPARATOR = "label.format.decimalSeparator";
	public static final Map<String, String> formatStringNoContext = new HashMap<String, String>(8);
	public static final Locale defaultLocaleNoContext = Locale.ITALY;
	static {
		formatStringNoContext.put(CURRENCY_SYMBOL_KEY, "&euro;");
		formatStringNoContext.put(DATE_FORMAT_KEY, "dd/MM/yyyy");
		formatStringNoContext.put(DATETIME_FORMAT_KEY, "dd/MM/yyyy hh:mm:ss");
		formatStringNoContext.put(CURRENCY_0DECIMAL_KEY, "#,##0");
		formatStringNoContext.put(CURRENCY_2DECIMAL_KEY, "#,##0.00");
		formatStringNoContext.put(CURRENCY_3DECIMAL_KEY, "#,##0.000");
		formatStringNoContext.put(CURRENCY_4DECIMAL_KEY, "#,##0.0000");
		formatStringNoContext.put(CURRENCY_5DECIMAL_KEY, "#,##0.00000");
		formatStringNoContext.put(NUMBER_GROUP_SEPARATOR, ".");
		formatStringNoContext.put(NUMBER_DECIMAL_SEPARATOR, ",");
	}

	public PageContext getPageContext() {
		return pageContext;
	}

	/**
	 * Called after the object has been retrieved from the bean contained in the list and
	 * before the decorate method will executed.
	 * 
	 * @param pageContext
	 *            Column instance object that allow you to surf the entire model of
	 *            displaytag
	 */
	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}

	public ValueDecorator() {
		super();
	}

	public String decorate(Object value, String format) {
		if (value == null) {
			return null;
		}
		String output = null;
		try {
			if (value instanceof java.sql.Timestamp || value instanceof java.sql.Date || value instanceof java.util.Date) {
				// SimpleDateFormat dfOut = new SimpleDateFormat("dd/MM/yyyy",
				// Locale.ITALIAN);
				SimpleDateFormat dfOut;
				if (format == null) {
					dfOut = new SimpleDateFormat(getValueFromResource(DATE_FORMAT_KEY),
							getCurrentLocale());
				} else {
					dfOut = new SimpleDateFormat(format, getCurrentLocale());
				}
				output = dfOut.format(value);
			} else if (value instanceof Number) {
				DecimalFormatSymbols dfs = new DecimalFormatSymbols();
				String outGroupSeparator = getValueFromResource(NUMBER_GROUP_SEPARATOR);
				dfs.setGroupingSeparator(outGroupSeparator.charAt(0));
				String outDecimalSeparator = getValueFromResource(NUMBER_DECIMAL_SEPARATOR);
				dfs.setDecimalSeparator(outDecimalSeparator.charAt(0));
				DecimalFormat dfOut = new DecimalFormat(format, dfs);
				output = dfOut.format(value);
			} else {
				output = value.toString();
			}
			return output;
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	public String getCurrencySymbol() {
		return getValueFromResource(CURRENCY_SYMBOL_KEY);
	}

	public String getValueFromResource(String key) {
		if (getPageContext() == null) {
			return (String) formatStringNoContext.get(key);
		}
		return getTableProperties().geResourceProvider().getResource(key, "", null,
				pageContext);
	}

	public Locale getCurrentLocale() {
		if (getPageContext() == null) {
			return defaultLocaleNoContext;
		}
		return TableProperties.getLocaleResolverInstance().resolveLocale(
				(HttpServletRequest) pageContext.getRequest());
	}

	public TableProperties getTableProperties() {
		return TableProperties.getInstance((HttpServletRequest) getPageContext()
				.getRequest());
	}

	public abstract String decorate(Object value);
}
