package com.greenorange.promotion.utils;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

public class QRCodeUtil {


    // 将InputStream转换为BufferedImage
    public static BufferedImage inputStreamToBufferedImage(InputStream inputStream) throws IOException {
        return ImageIO.read(inputStream);
    }

    // 获取用户头像并缩放到指定大小
    public static BufferedImage getScaledAvatar(String url, int targetWidth, int targetHeight) throws IOException {
        BufferedImage avatarImage = ImageIO.read(new URL(url));
        int width = avatarImage.getWidth();
        int height = avatarImage.getHeight();

        // 如果头像大于目标大小，进行缩放
        if (width > targetWidth && height > targetHeight) {
            width = targetWidth;
            height = targetHeight;
        }

        return toBufferedImage(avatarImage.getScaledInstance(width, height, Image.SCALE_DEFAULT));
    }

    // 将Image转换为BufferedImage
    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }
        BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = bimage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bimage;
    }

//    // 将图片转为圆形
//    public static BufferedImage getCircularImage(BufferedImage img) {
//        int width = img.getWidth();
//        int height = img.getHeight();
//        BufferedImage circularImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//        Graphics2D g = circularImage.createGraphics();
//        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, width, height);
//        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        g.setClip(shape);
//        g.drawImage(img, 0, 0, null);
//        g.dispose();
//        return circularImage;
//    }


    /**
     * 将图片裁剪成圆形，并确保背景透明
     * @param img 原始图片
     * @return 裁剪后的圆形图片，背景为透明
     */
    public static BufferedImage getCircularImage(BufferedImage img) {
        int width = img.getWidth();
        int height = img.getHeight();

        // 创建一个透明背景的 BufferedImage
        BufferedImage circularImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = circularImage.createGraphics();
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, width, height);

        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.WHITE);
        g.fill(shape);
        g.setComposite(AlphaComposite.SrcAtop);
        g.drawImage(img, 0, 0, null);
        g.dispose();

        return circularImage;
    }




    // 合并二维码和头像
    public static BufferedImage addImages(BufferedImage qrImage, BufferedImage avatarImage, int x, int y) {
        Graphics2D g = qrImage.createGraphics();
        g.drawImage(avatarImage, x, y, avatarImage.getWidth(), avatarImage.getHeight(), null);
        g.dispose();
        return qrImage;
    }

    // 合并二维码和空白图片
    public static BufferedImage addBlankImage(BufferedImage qrImage, BufferedImage blankImage, int x, int y) {
        Graphics2D g = qrImage.createGraphics();
        g.drawImage(blankImage, x, y, blankImage.getWidth(), blankImage.getHeight(), null);
        g.dispose();
        return qrImage;
    }

    // 将BufferedImage转换为InputStream
    public static InputStream bufferedImageToInputStream(BufferedImage image) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        return new ByteArrayInputStream(os.toByteArray());
    }

    // 添加文字到图片上
    public static BufferedImage addTextToImage(BufferedImage image, String text, Color color, Font font, int x, int y) {
        Graphics2D g = image.createGraphics();
        g.setColor(color);
        g.setFont(font);
        g.drawString(text, x, y);
        g.dispose();
        return image;
    }

    // 保存图片到文件
    public static void saveImageToFile(BufferedImage image, String path) throws IOException {
        File outputfile = new File(path);
        ImageIO.write(image, "png", outputfile);
    }


}
