package com.yonge.toolset.utils.freemarker;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class FreemarkerTemplateEngine {
	private static final Logger LOGGER = LoggerFactory.getLogger(FreemarkerTemplateEngine.class);

	private final static FreemarkerTemplateEngine templateEngine = new FreemarkerTemplateEngine();
	
	private static Configuration cfg;

	private FreemarkerTemplateEngine() {
		cfg = new Configuration();
		cfg.setOutputEncoding("UTF-8");
		cfg.setClassForTemplateLoading(FreemarkerTemplateEngine.class, "/template/");
		cfg.setObjectWrapper(new DefaultObjectWrapper());
	}

	public static FreemarkerTemplateEngine getInstance() {
		return templateEngine;
	}

	public void setClassForTemplateLoading(Class<?> clazz, String pathPrefix) {
		cfg.setClassForTemplateLoading(clazz, pathPrefix);
	}

	public void render(Map<String, Object> data, String templateFile, File outputFile) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			Template temp = cfg.getTemplate(templateFile);
			fw = new FileWriter(outputFile);
			bw = new BufferedWriter(fw);
			temp.process(data, bw);
			bw.flush();
		} catch (Exception e) {
			LOGGER.warn("模板文件{}渲染失败", templateFile, e);
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					LOGGER.warn("BufferedWriter关闭异常");
				}
			}
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					LOGGER.warn("FileWriter关闭异常");
				}
			}
		}
	}

	public void render(Map<String, Object> data, String templateFile, String outputFile) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			Template temp = cfg.getTemplate(templateFile);
			File file = new File(outputFile);
			fw = new FileWriter(file);
			bw = new BufferedWriter(fw);
			temp.process(data, bw);
			bw.flush();
		} catch (Exception e) {
			LOGGER.warn("模板文件{}渲染失败", templateFile, e);
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					LOGGER.warn("BufferedWriter关闭异常");
				}
			}
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					LOGGER.warn("FileWriter关闭异常");
				}
			}
		}
	}

	public void renderByString(Map<String, Object> data, String templateStr, File outputFile) {

		FileWriter fw = null;
		BufferedWriter bw = null;
		String templateFileName = outputFile.getName();
		try {
			StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
			stringTemplateLoader.putTemplate(templateFileName, templateStr);
			cfg.setTemplateLoader(stringTemplateLoader);
			Template temp = cfg.getTemplate(templateFileName);
			fw = new FileWriter(outputFile);
			bw = new BufferedWriter(fw);
			temp.process(data, bw);
			bw.flush();
		} catch (Exception e) {
			LOGGER.warn("模板渲染失败", e);
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					LOGGER.warn("BufferedWriter关闭异常");
				}
			}
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					LOGGER.warn("FileWriter关闭异常");
				}
			}
		}
	}

}
