package com.gic.haoban.manage.service.util;

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

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author MUSI
 * @Date 2023/3/15 9:48 PM
 * @Description
 * @Version
 **/
public class DrawImageUtils {

    private static final Logger log = LoggerFactory.getLogger(DrawImageUtils.class);

    /**
     * 绘画素材月报海报图
     * @param fileName
     * @param materialNum
     * @param amountText
     * @throws IOException
     */
    public static void drawImageMonth(String fileName, String materialNum, String amountText) {
        try {
            long startTime = System.currentTimeMillis();
            InputStream inputStream = DrawImageUtils.class.getClassLoader().getResourceAsStream("template/month_bkg.png");
            if (inputStream == null) {
                return;
            }

            BufferedImage image = ImageIO.read(inputStream);
            Graphics2D pen = image.createGraphics();
            pen.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            pen.setColor(Color.WHITE);
            Font font = new Font("微软雅黑", Font.BOLD, 56);
            pen.setFont(font);
            FontMetrics metrics = pen.getFontMetrics(font);
            int startX = 40 + (260 - metrics.stringWidth(materialNum)) / 2;
            pen.drawString(materialNum, startX, 250);
            int amountX = 320 + (353 - metrics.stringWidth(amountText)) / 2;
            pen.drawString(amountText, amountX, 250);
            File file = new File(fileName);
            FileOutputStream fos = new FileOutputStream(file);
            ImageIO.write(image, "png", fos);
            pen.dispose();
            long endTime = System.currentTimeMillis();
            log.info("合成图片耗时 {} ms", (endTime - startTime));
        }catch (Exception ex){
            log.info("合成月报图片异常", ex);
        }
    }

    public static void main(String[] args) throws IOException {

        DrawImageUtils.drawImageMonth("test2.png", "104", "13,433.33");
        DrawImageUtils.drawImageMonth("test3.png", "1", "1.43万");
    }
}
