package com.chaozhanggui.system.lkl.util;

import cn.hutool.core.io.FileUtil;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
@Slf4j
public class HtmlUtil {

    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;
   static Template template =null;


   public  void initHtml(){
        try {
            template = freeMarkerConfigurer.getConfiguration().getTemplate("indexHTKWECHAT_PAY.html");

            System.out.println(template.getName());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //html 替换占位符
    public static File writeHtml(Map<String, Object> data) throws IOException, TemplateException {
        String fileName = UUID.randomUUID().toString();
        File htmlFile = new File(FileUtil.getTmpDir(), fileName + ".html");
        FileWriter sw = new FileWriter(htmlFile);
        log.info("生成协议html, 地址:{}, 参数:{} ", htmlFile.getAbsolutePath(), data);
        template.process(data, sw);
        return htmlFile;
    }


    //html 转png 示例代码
    public static File createAgreementPicture(File htmlFile) throws IOException {
        File outputFile = new File(FileUtil.getTmpDir(), htmlFile.getName() + ".jpg");
        log.info("生成图片开始, HTML地址 {}, 图片地址:{}", htmlFile.getAbsolutePath(), outputFile.getAbsolutePath());
        String commandProcess = "wkhtmltoimage  --width  400  --quality  94  " + htmlFile.getPath() + "  " + outputFile.getPath();
        log.info("协议执行procommand:{}", commandProcess);
        long startTime = System.currentTimeMillis();   //获取开始时间
        Process process = Runtime.getRuntime().exec(commandProcess);
        try {
            int exitVal = process.waitFor();
            log.info("协议html转换png结果:{}", exitVal);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.info("协议html转换png错误:{}", e.getMessage());
            throw new IOException(e);
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        log.info("程序运行时间： " + (endTime - startTime) + "ms");
        log.info("生成图片结束,地址: {}", outputFile.getPath());
        Thumbnails.of(outputFile).scale(1).outputQuality(0.9).toFile(outputFile);
        return outputFile;
    }


   // 合并两张图片示例代码
    public BufferedImage mergeImage(BufferedImage img1, BufferedImage img2, boolean isHorizontal) throws IOException {
        int w1 = img1.getWidth();
        int h1 = img1.getHeight();
        int w2 = img2.getWidth();
        int h2 = img2.getHeight();
        // 从图片中读取RGB
        int[] ImageArrayOne = new int[w1 * h1];
        ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
        int[] ImageArrayTwo = new int[w2 * h2];
        ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
        // 生成新图片
        BufferedImage DestImage = null;
        if (isHorizontal) { // 水平方向合并
            // DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = null;
            if (h1 >= h2) {
                DestImage = new BufferedImage(w1 + w2, h1, BufferedImage.TYPE_INT_RGB);
                g2d = DestImage.createGraphics();
                g2d.setPaint(Color.WHITE);
                g2d.fillRect(0, 0, w1 + w2, h1);
                g2d.dispose();
            } else {
                DestImage = new BufferedImage(w2, h1, BufferedImage.TYPE_INT_RGB);// TYPE_INT_RGB
                g2d = DestImage.createGraphics();
                g2d.setPaint(Color.WHITE);
                g2d.fillRect(0, 0, w2 + w1, h1);
                g2d.dispose();
            }
            DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
        } else { // 垂直方向合并
            Graphics2D g2d = null;
            if (w1 >= w2) {
                DestImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_RGB);// TYPE_INT_RGB
                g2d = DestImage.createGraphics();
                g2d.setPaint(Color.WHITE);
                g2d.fillRect(0, 0, w1 + w2, h1 + h2);
                g2d.dispose();
            } else {
                DestImage = new BufferedImage(w2, h1 + h2, BufferedImage.TYPE_INT_RGB);// TYPE_INT_RGB
                g2d = DestImage.createGraphics();
                g2d.setPaint(Color.WHITE);
                g2d.fillRect(0, 0, w2 + w1, h1 + h2);
                g2d.dispose();
            }
            DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
        }
        return DestImage;
    }

}
