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.AllArgsConstructor;
import lombok.Getter;
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.HashMap;
import java.util.Random;

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

    @Getter
    @AllArgsConstructor
    public enum DrawBkgImg {

        CONTENT_AREA_WEEK_BKG("template/content_area_week_bkg.png", "素材区经周报背景图", 352, 416, 320),
        CONTENT_AREA_MONTH_BKG("template/content_area_month_bkg.png", "素材区经月报背景图", 352, 416, 320),
        CONTENT_CLERK_WEEK_BKG("template/content_week_bkg.png", "素材导购/店长周报背景图", 260, 320, 353),
        CONTENT_CLERK_MONTH_BKG("template/content_month_bkg.png", "素材导购/店长月报背景图", 260, 320, 353),

        POTENTIAL_AREA_WEEK_BKG("template/potential_week_bkg.png", "潜客雷达区经周报背景图", 260, 320, 353),
        POTENTIAL_AREA_MONTH_BKG("template/potential_month_bkg.png", "潜客雷达区经月报背景图", 260, 320, 353),
        POTENTIAL_CLERK_WEEK_BKG("template/potential_week_bkg.png", "潜客雷达导购/店长周报背景图", 260, 320, 353),
        POTENTIAL_CLERK_MONTH_BKG("template/potential_month_bkg.png", "潜客雷达导购/店长月报背景图", 260, 320, 353),

        AREA_WEEK_BKG("template/week_bkg.png", "门店增长区经周报背景图", 352, 416, 320),
        AREA_MONTH_BKG("template/month_bkg.png", "门店增长区经月报背景图", 352, 416, 320),
        CLERK_WEEK_BKG("template/week_bkg.png", "门店增长导购/店长周报背景图", 352, 416, 320),
        CLERK_MONTH_BKG("template/month_bkg.png", "门店增长导购/店长月报背景图", 352, 416, 320),
        ;
        private String path;
        private String desc;
        /**
         * 第一个位置宽度
         */
        private int numWidth;

        /**
         * 金额开始位置的左上顶点坐标
         */
        private int amountStart;
        /**
         * 金额宽度
         */
        private int amountWidth;

    }

    public static final HashMap<String, DrawImageUtils.DrawBkgImg> templateMap = new HashMap<>();

    static {
        // 区域+周/月
        templateMap.put("AREA_WEEK", DrawImageUtils.DrawBkgImg.AREA_WEEK_BKG);
        templateMap.put("AREA_MONTH", DrawImageUtils.DrawBkgImg.AREA_MONTH_BKG);
        // 导购+周/月
        templateMap.put("CLERK_WEEK", DrawImageUtils.DrawBkgImg.CLERK_WEEK_BKG);
        templateMap.put("CLERK_MONTH", DrawImageUtils.DrawBkgImg.CLERK_MONTH_BKG);
        // 预约 + 区域/导购 + 周/月
        templateMap.put("POTENTIAL_AREA_WEEK", DrawImageUtils.DrawBkgImg.POTENTIAL_AREA_WEEK_BKG);
        templateMap.put("POTENTIAL_CLERK_WEEK", DrawImageUtils.DrawBkgImg.POTENTIAL_CLERK_WEEK_BKG);
        templateMap.put("POTENTIAL_AREA_MONTH", DrawImageUtils.DrawBkgImg.POTENTIAL_AREA_MONTH_BKG);
        templateMap.put("POTENTIAL_CLERK_MONTH", DrawImageUtils.DrawBkgImg.POTENTIAL_CLERK_MONTH_BKG);
        // 内容 + 区域/导购 + 周/月
        templateMap.put("CONTENT_AREA_WEEK", DrawImageUtils.DrawBkgImg.CONTENT_AREA_WEEK_BKG);
        templateMap.put("CONTENT_CLERK_WEEK", DrawImageUtils.DrawBkgImg.CONTENT_CLERK_WEEK_BKG);
        templateMap.put("CONTENT_AREA_MONTH", DrawImageUtils.DrawBkgImg.CONTENT_AREA_MONTH_BKG);
        templateMap.put("CONTENT_CLERK_MONTH", DrawImageUtils.DrawBkgImg.CONTENT_CLERK_MONTH_BKG);
    }

    /**
     * 绘画素材周报/月报 海报图
     *
     * @param factoryCode
     * @param firstValue
     * @param amountText
     */
    public static String drawImage(String factoryCode, String firstValue, String amountText, DrawBkgImg drawBkgImg) {
        if (drawBkgImg == null) {
            log.info("合成图片的背景模板为空 factoryCode:{}", factoryCode);
            return null;
        }
        try {
            long startTime = System.currentTimeMillis();
            InputStream inputStream = DrawImageUtils.class.getClassLoader().getResourceAsStream(drawBkgImg.getPath());
            if (inputStream == null) {
                log.info("模板文件不存在 path:{}", drawBkgImg.getPath());
                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 + (drawBkgImg.numWidth - metrics.stringWidth(firstValue)) / 2;
            pen.drawString(firstValue, startX, 250);
            int amountX = drawBkgImg.amountStart + (drawBkgImg.amountWidth - 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(), "jpg", 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();
        String url = DrawImageUtils.drawImage("jhdm", Math.abs(random.nextInt(1000)) + "",
                Math.abs(random.nextInt(1000)) + "万", DrawBkgImg.AREA_MONTH_BKG);
        System.out.println(url);
//        String temp = DrawImageUtils.drawImage("jhdm", Math.abs(random.nextInt(1000)) + "",
//                Math.abs(random.nextInt(1000)) + "万", DrawBkgImg.CLERK_MONTH_BKG);
    }
}
