package com.wzj.soopin.order.utils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.QRCodeReader;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.Comment;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.io.File;

@Component
@Slf4j
public class QrCodeGenerator {

    /**
     * 生成二维码图片并保存到指定路径
     * @param content 二维码内容
     * @param width 图片宽度
     * @param height 图片高度
     * @param filePath 保存路径
     */


    public String generateQrCode(String content, int width, int height, String filePath) {
        try {
            // 检查并创建目录
            File dir = new File(filePath).getParentFile();
            if (!dir.exists()) {
                dir.mkdirs();
            }
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            Path path = Paths.get(filePath);
            MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
            return filePath;
        } catch (WriterException | IOException e) {
            // 记录异常信息
            log.error("生成二维码时出现异常: {}", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成二维码图片字节数组
     * @param content 二维码内容
     * @param width 图片宽度
     * @param height 图片高度
     * @return 二维码图片字节数组
     */
    public byte[] generateQrCodeImage(String content, int width, int height) {
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
            javax.imageio.ImageIO.write(bufferedImage, "png", bos);
            return bos.toByteArray();
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解析二维码图片
     * @param filePath 二维码图片路径
     * @return 二维码内容
     */
    public String decodeQrCode(String filePath) {
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(filePath));
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            int[] pixels = new int[width * height];
            bufferedImage.getRGB(0, 0, width, height, pixels, 0, width);
            LuminanceSource source = new RGBLuminanceSource(width, height, pixels);
            HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
            MultiFormatReader reader = new MultiFormatReader();
            Result result = reader.decode(binaryBitmap);
            return result.getText();
        } catch (IOException | com.google.zxing.NotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
