package com.blueland.core.util;

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

import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FreemarkerUtil {

	static FreeMarkerConfigurer result = new FreeMarkerConfigurer();
	static {
		initFreemarkerConfig();
	}
	public static void initFreemarkerConfig() {
		try {
			FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
		    factory.setTemplateLoaderPaths("classpath:/ftl/");
		    factory.setDefaultEncoding("UTF-8");
		    freemarker.template.Configuration configuration = factory.createConfiguration();
		    configuration.setClassicCompatible(true);
		    result.setConfiguration(configuration);
		    Properties settings = new Properties();
		    settings.put("template_update_delay", "0");
		    settings.put("default_encoding", "UTF-8");
		    settings.put("number_format", "0.##########");
		    settings.put("datetime_format", "yyyy-MM-dd HH:mm:ss");
		    settings.put("classic_compatible", true);
		    settings.put("template_exception_handler", "ignore");
		    result.setFreemarkerSettings(settings);
		}
		catch(Exception ex) {
//			log.error("initFreemarkerConfig====",ex);
		}
	    
	}
	
	
	
	public static String processTemplateString(String templateName,Map model) {
		try {
			Template tpl = result.getConfiguration().getTemplate(templateName);
			return FreeMarkerTemplateUtils.processTemplateIntoString(tpl, model);
		}catch(Exception e) {
//			log.error("processTemplateString====",e);
		}
		return "";
	}
}
