package cn.newfeifan.mall.module.distri.dal.dataobject.shopsettlement.file;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 实现MultipartFile接口，用于将BufferedImage转换为MultipartFile对象。
 * 这个类允许将图像数据封装为MultipartFile，以便在Spring框架中进行文件上传操作。
 */
public class BufferedImageMultipartFile implements MultipartFile {

    private final BufferedImage bufferedImage; // 存储BufferedImage实例
    private final String fileName; // 图像文件名
    private final String contentType; // 图像的内容类型

    /**
     * 构造函数初始化BufferedImageMultipartFile实例。
     *
     * @param bufferedImage 要封装的BufferedImage实例
     * @param fileName      图像文件名
     * @param contentType   图像的内容类型
     */
    public BufferedImageMultipartFile(BufferedImage bufferedImage, String fileName, String contentType) {
        this.bufferedImage = bufferedImage;
        this.fileName = fileName;
        this.contentType = contentType;
    }

    @NotNull
    @Override
    public String getName() {
        return this.fileName; // 返回文件名
    }

    @Override
    public String getOriginalFilename() {
        return this.fileName; // 返回原始文件名，与getName相同
    }

    @Override
    public String getContentType() {
        return this.contentType; // 返回内容类型
    }

    @Override
    public boolean isEmpty() {
        return this.bufferedImage == null; // 如果BufferedImage为空，则返回true
    }

    @Override
    public long getSize() {
        // 计算图像数据的大小
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            ImageIO.write(bufferedImage, "png", bos);
            return bos.size();
        } catch (IOException e) {
            throw new RuntimeException("Failed to calculate size of the image.", e);
        }
    }

    @NotNull
    @Override
    public byte[] getBytes() throws IOException {
        // 将图像数据转换为字节数组
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            ImageIO.write(bufferedImage, "png", bos);
            return bos.toByteArray();
        }
    }

    @NotNull
    @Override
    public InputStream getInputStream() throws IOException {
        // 创建一个输入流，用于读取图像数据
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            ImageIO.write(bufferedImage, "png", bos);
            return new ByteArrayInputStream(bos.toByteArray());
        }
    }

    @NotNull
    @Override
    public Resource getResource() {
        // 不支持此方法，因为BufferedImage不能直接转换为Resource
        throw new UnsupportedOperationException("getResource not supported.");
    }

    @Override
    public void transferTo(@NotNull File dest) throws IOException {
        // 将图像数据写入指定的File对象
        try (OutputStream os = Files.newOutputStream(dest.toPath())) {
            ImageIO.write(bufferedImage, "png", os);
        }
    }

    @Override
    public void transferTo(@NotNull Path dest) throws IOException {
        // 将图像数据写入指定的Path对象
        try (OutputStream os = Files.newOutputStream(dest)) {
            ImageIO.write(bufferedImage, "png", os);
        }
    }

    // 其他MultipartFile方法的实现...
}
