package railo.runtime.functions.displayFormatting;

import java.util.Locale;
import java.util.TimeZone;

import railo.commons.date.TimeZoneUtil;
import railo.runtime.PageContext;
import railo.runtime.engine.ThreadLocalPageContext;
import railo.runtime.exp.ExpressionException;
import railo.runtime.ext.function.Function;
import railo.runtime.op.Caster;
import railo.runtime.type.dt.DateTime;

/**
 * Implements the CFML Function dateformat
 */
public final class DateTimeFormatClassic implements Function {

	private static final long serialVersionUID = 134840879454373440L;

	/**
	 * @param pc
	 * @param object
	 * @return Formated Time Object as String
	 * @throws ExpressionException
	 */
	public static String call(PageContext pc , Object object) throws ExpressionException {
		return _call(pc,object,"dd-mmm-yy hh:nn tt",ThreadLocalPageContext.getTimeZone(pc));
	}
	
	/**
	 * @param pc
	 * @param object
	 * @param mask Characters that show how CFML displays a date:
	 * @return Formated Time Object as String
	 * @throws ExpressionException
	 */
	public static String call(PageContext pc , Object object, String mask) throws ExpressionException {
		return _call(pc,object,mask,ThreadLocalPageContext.getTimeZone(pc));
	}

	public static String call(PageContext pc , Object object, String mask,String strTimezone) throws ExpressionException {
		return _call(pc,object,mask, strTimezone==null?ThreadLocalPageContext.getTimeZone(pc):TimeZoneUtil.toTimeZone(strTimezone));
	}
	
	private static String _call(PageContext pc , Object object, String mask,TimeZone tz) throws ExpressionException {
		Locale locale=Locale.US;//:pc.getConfig().getLocale();
		DateTime datetime = Caster.toDate(object,true,tz,null);
		if(datetime==null) {
		    if(object.toString().trim().length()==0) return "";
		    throw new ExpressionException("can't convert value "+object+" to a datetime value");
		}
		
		
		return new railo.runtime.format.DateTimeFormat(locale).format(datetime,mask,tz);
		//return new railo.runtime.text.TimeFormat(locale).format(datetime,mask);
	}
}