package org.matrix.testNg.web.report;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.matrix.exception.GlobalException;
import org.matrix.testNg.web.entity.DataBean;
import org.matrix.testNg.web.entity.DataBeanMove;
import org.matrix.testNg.web.entity.DataBeans;
import org.matrix.testNg.web.entity.ReportMessage;
import org.matrix.util.ReporterUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MRY
 */
public class GenerateReporterJob {

    private static final LocalDateTime TIME = LocalDateTime.now();
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
    private static final String STRING = DATE_TIME_FORMATTER.format(TIME);
    private static final String OUTPUT_FOLDER = System.getProperty("user.dir") + "/";
    private static final String FILE_NAME = STRING + "-testNg.html";

    public ReportMessage generateReport() {
        try {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
            cfg.setClassForTemplateLoading(this.getClass(), "/templates");
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            Template temp = cfg.getTemplate("overviews.ftl");
            Map context = new HashMap();
            ReporterDataJob reporterDatajob = new ReporterDataJob();
            DataBean dataBean = reporterDatajob.testDataBean();
            List<DataBeanMove> dataBeanMoves = reporterDatajob.testDataBeanMove();
            List<DataBeans> dataBeansList = reporterDatajob.testDataBeans();
            // 这里是公共的信息.
            for (DataBeans bean : dataBeansList) {
                context.put("overView", dataBean);
                for (DataBeanMove dataBeanMove : dataBeanMoves) {
                    context.put("move", dataBeanMoves);
                    if (bean.getResult()) {
                        context.put("pass", bean);
                    } else {
                        context.put("fail", bean);
                    }
                }
            }
            //文件夹不存在的话进行创建
            File reportDir = new File(OUTPUT_FOLDER);
            if (!reportDir.exists() && !reportDir.isDirectory()) {
                reportDir.mkdir();
            }
            // 输出流
            FileOutputStream out = new FileOutputStream(OUTPUT_FOLDER + "/" + FILE_NAME);
            Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
            // 转换输出
            temp.process(context, writer);
            writer.flush();
            ReportMessage reportMessage = new ReportMessage();
            reportMessage.setUrl(OUTPUT_FOLDER + "/" + FILE_NAME);
            return reportMessage;
        } catch (Exception e) {
            throw new GlobalException(e.getMessage());
        } finally {
            ReporterUtils.map = new HashMap<>();
        }
    }
}
