package com.inspect.partrolresult.util;

import cn.hutool.core.date.DateUtil;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.ServerException;
import javax.imageio.ImageIO;

import com.alibaba.nacos.common.utils.StringUtils;
import sun.font.FontDesignMetrics;

public class ImageUtil {
   public static void main(String[] args) throws Exception {
      System.out.println("1/2023/12/02/120/FIR/3730_1_20231202214009.jpg".substring(0, "1/2023/12/02/120/FIR/3730_1_20231202214009.jpg".lastIndexOf("/")));
      System.out.println("1/2023/12/02/120/FIR/3730_1_20231202214009.jpg".substring("1/2023/12/02/120/FIR/3730_1_20231202214009.jpg".lastIndexOf("/") + 1));
   }

   public static InputStream overlaying(InputStream inputStream, String coors, String temp) throws IOException {
      BufferedImage image = ImageIO.read(inputStream);
      if(image == null) {
         return null;
      } else {
         Graphics2D g2 = image.createGraphics();
         g2.setColor(Color.RED);
         BasicStroke stokeLine = new BasicStroke(1.0F);
         g2.setStroke(stokeLine);
         if(StringUtils.isNotEmpty(coors)) {
            coors = coors.replaceAll(";", StringUtils.COMMA);
            String[] splits = coors.split(StringUtils.COMMA);
            if(splits != null && splits.length >= 8) {
               int[] cs = new int[splits.length];

               for(int i = 0; i < cs.length; ++i) {
                  cs[i] = Integer.parseInt(splits[i]);
               }

               System.out.println("x:" + cs[0]);
               System.out.println("x:" + cs[1]);
               System.out.println("x:" + (cs[2] - cs[0]));
               System.out.println("x:" + (cs[5] - cs[3]));
               g2.drawRect(cs[0], cs[1], cs[2] - cs[0], cs[5] - cs[3]);
               g2.dispose();
               Graphics2D g3 = image.createGraphics();
               g3.setColor(Color.WHITE);
               Font font = new Font("微软雅黑", 1, 24);
               String content = DateUtil.now();
               FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
               g3.setFont(font);
               g3.drawString(content, 0, metrics.getAscent());
               g3.dispose();
               font = new Font("微软雅黑", 1, 14);
               Graphics2D g4 = image.createGraphics();
               g4.setColor(Color.RED);
               g4.setFont(font);
               g4.drawString(temp, cs[0], cs[1] + 14);
               g4.dispose();
               ByteArrayOutputStream outStream = new ByteArrayOutputStream();
               ImageIO.write(image, "jpg", outStream);
               InputStream input = new ByteArrayInputStream(outStream.toByteArray());
               return input;
            } else {
               throw new ServerException("坐标数量有误：" + coors);
            }
         } else {
            throw new ServerException("坐标数量上报值为空：" + coors);
         }
      }
   }
}
