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

import com.alibaba.fastjson.JSON;
import com.gic.thirdparty.cloudfile.CloudFileUtil;
import com.gic.thirdparty.cloudfile.enums.CloudFileBusinessOptEnum;
import com.gic.thirdparty.cloudfile.enums.CloudFileTypeEnum;
import com.gic.thirdparty.cloudfile.pojo.CloudFileInfo;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Random;

/**
 * @Author MUSI
 * @Date 2023/3/15 9:48 PM
 * @Description
 * @Version
 **/
@Slf4j
public class DrawImageUtils {
    /**
     * 绘画素材周报/月报 海报图
     *
     * @param factoryCode
     * @param materialNum
     * @param amountText
     */
    public static String drawImage(String factoryCode, String materialNum, String amountText, String templatePath) {
        try {
            long startTime = System.currentTimeMillis();
            InputStream inputStream = DrawImageUtils.class.getClassLoader().getResourceAsStream(templatePath);
            if (inputStream == null) {
                log.info("模板文件不存在 path:{}", templatePath);
                return null;
            }

            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);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", bos);
            pen.dispose();
            CloudFileInfo cloudFileInfo = CloudFileUtil.uploadFile(bos.toByteArray(), "png", CloudFileTypeEnum.IMAGE, factoryCode, CloudFileBusinessOptEnum.HAOBAN_COMMON);
            log.info("上传图片结果:{}", JSON.toJSONString(cloudFileInfo));
            long endTime = System.currentTimeMillis();
            log.info("合成图片耗时 {} ms", (endTime - startTime));
            if (cloudFileInfo != null) {
                return cloudFileInfo.getOrgFileUrl();
            }
        } catch (Exception ex) {
            log.info("合成月报图片异常", ex);
        }
        return null;
    }

    public static void main(String[] args) {
        System.setProperty("gic.module.name", "haoban-manage3-service");
        Random random = new Random();
        for (int i = 0; i < 50; i++) {
            String url = DrawImageUtils.drawImage("jhdm", Math.abs(random.nextInt(1000)) + "", Math.abs(random.nextInt(1000)) + "万", "template/week_bkg.png");
            System.out.println(url);
        }
    }
}
