package com.ycwl.basic.image.watermark.operator;

import com.ycwl.basic.image.watermark.entity.WatermarkInfo;
import com.ycwl.basic.image.watermark.exception.ImageWatermarkException;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class DefaultImageWatermarkOperator implements IOperator {
    @Override
    public File process(WatermarkInfo info) throws ImageWatermarkException {
        BufferedImage baseImage;
        BufferedImage watermarkImage;
        InputStream logoInputStream = getClass().getResourceAsStream("/watermark.png");
        if (logoInputStream == null) {
            throw new ImageWatermarkException("无法找到 watermark.png 资源文件");
        }
        try {
            baseImage = ImageIO.read(info.getOriginalFile());
            watermarkImage = ImageIO.read(logoInputStream);
        } catch (IOException e) {
            throw new ImageWatermarkException("图片打开失败");
        }
        // 新图像画布
        BufferedImage newImage = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = newImage.createGraphics();
        g2d.drawImage(baseImage, 0, 0, null);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
        g2d.drawImage(watermarkImage, 0, 0,  baseImage.getWidth(), baseImage.getHeight(), null);
        String fileName = info.getWatermarkedFile().getName();
        String formatName = "jpg"; // 默认格式为 jpg
        if (fileName.endsWith(".png")) {
            formatName = "png";
        } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
            formatName = "jpg";
        }
        ImageWriter writer = ImageIO.getImageWritersByFormatName(formatName).next();
        ImageOutputStream ios;
        try {
            ios = ImageIO.createImageOutputStream(info.getWatermarkedFile());
        } catch (IOException e) {
            throw new ImageWatermarkException("图片保存失败，目标文件无法写入");
        }
        writer.setOutput(ios);
        try {
            // 使用 ImageWriter 设置写入质量
            ImageWriteParam writeParam = writer.getDefaultWriteParam();
            if (writeParam.canWriteCompressed()) {
                writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                writeParam.setCompressionQuality(0.8f); // 设置写入质量为 80%
            }
            writer.write(null, new javax.imageio.IIOImage(newImage, null, null), writeParam);
        } catch (IOException e) {
            throw new ImageWatermarkException("图片保存失败");
        }
        finally {
            g2d.dispose();
            try {
                ios.close();
            } catch (IOException ignore) {
            }
            writer.dispose();
        }
        return info.getWatermarkedFile();
    }
}
