package com.yunfeiyun.agmp.common.utils.img;

import com.yunfeiyun.agmp.common.utils.img.entity.GraphicsCanvasItem;
import com.yunfeiyun.agmp.common.utils.img.enums.MediaType;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.List;

public class GraphicsCanvas {
    private  BufferedImage bufferedImage;

    private List<GraphicsCanvasItem> graphicsCanvasItemList;


    /**
     * 初始化画布
     * @param bufferedImage
     * */
    public GraphicsCanvas(BufferedImage bufferedImage){
        this.bufferedImage = bufferedImage;
    }

    /**
     * 初始化画布
     * @param width 宽度
     * @param height 高度
     * @param imgType 图片类型
     * */
    public GraphicsCanvas(int width,int height,int imgType,List<GraphicsCanvasItem> graphicsCanvasItemList){
        this.bufferedImage = new BufferedImage(width,height,imgType);
        this.graphicsCanvasItemList = graphicsCanvasItemList;
    }

    public void setGraphicsCanvasItemList(List<GraphicsCanvasItem> graphicsCanvasItemList){
        this.graphicsCanvasItemList = graphicsCanvasItemList;
    }
    public boolean write(String formatName, ImageOutputStream output) throws IOException{
      return  ImageIO.write(this.bufferedImage,formatName,output);
    }
    public  boolean write(String formatName, File output) throws IOException {
        return ImageIO.write(this.bufferedImage,formatName,output);
    }
    public  boolean write(String formatName, OutputStream output) throws IOException {
        return ImageIO.write(this.bufferedImage,formatName,output);
    }

    public String toBase64() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(this.bufferedImage, "png", baos);
        byte[] imageBytes = baos.toByteArray();
        // 对字节数组进行 Base64 编码
        return ImageReadUtil.BASE64_HEAD+ Base64.getEncoder().encodeToString(imageBytes);
    }
    public  void appendRectangle(int positionX,int positionY,int width,int height,Color backgroundColor){
        Graphics2D g2d = this.bufferedImage.createGraphics();
        g2d.setBackground(backgroundColor); // 设置背景色为白色
        g2d.clearRect(positionX, positionY, width, height); // 清空绘制区域
    }
    public  void appendRectangle(int positionX,int positionY,int width,int height,Image image){
        Graphics2D g2d = this.bufferedImage.createGraphics();
        g2d.drawImage(image,positionX,positionY,width,height, null);
    }
    public void appendText(String text,Font font,Color fontColor,int x, int y){
        Graphics2D g2d = this.bufferedImage.createGraphics();
        // 设置字体和颜色
        g2d.setFont(font);
        g2d.setColor(fontColor);

        x = x - (g2d.getFontMetrics().stringWidth(text) / 2 );
        y = y + g2d.getFontMetrics().getAscent() / 2;

        g2d.drawString(text, x, y);
    }

    public void appendByGraphicsCanvasItems() throws Exception{
        for(GraphicsCanvasItem item : graphicsCanvasItemList){
            if(item.getMediaType().equals(MediaType.SHAPE)){
                appendRectangle(item.getPositionX(),item.getPositionY(),item.getWidth(),item.getHeight(),item.getBackGroundColor());
            }else if(item.getMediaType().equals(MediaType.PATH_IMAGE)){
                appendRectangle(item.getPositionX(),item.getPositionY(),item.getWidth(),item.getHeight(),ImageReadUtil.readImageToBufferImage(item.getData()));
            }else if (item.getMediaType().equals(MediaType.BASE64_IMAGE)){
                appendRectangle(item.getPositionX(),item.getPositionY(),item.getWidth(),item.getHeight(),ImageReadUtil.base64ConvertImage(item.getData()));
            }else if(item.getMediaType().equals(MediaType.TEXT)) {
                 appendText(item.getData(),item.getFont(),item.getBackGroundColor(),item.getPositionX(),item.getPositionY());
            }else if(item.getMediaType().equals(MediaType.BARCODE)){
                appendRectangle(item.getPositionX(),item.getPositionY(),item.getWidth(),item.getHeight(),BitMatrixUtil.createBarcode(item.getData(),item.getWidth(),item.getHeight(),item.getLineWidth()));
            }else if(item.getMediaType().equals(MediaType.BIT_MATRIX)){
                appendRectangle(item.getPositionX(),item.getPositionY(),item.getWidth(),item.getHeight(),BitMatrixUtil.createBitMatrixUtil(item.getData(),item.getWidth(),item.getHeight(),item.getMargin()));
            }
        }
    }


}
