package com.jeequan.jeepay.core.utils;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.URLUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;

/**
 * 图片处理工具类
 *
 * @author ruoyi
 */
public class ImageUtils {
    private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);

    public static ByteArrayOutputStream compressNetPic(String url, int size) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        float scale;
        try {
            long contentLength = URLUtil.getContentLength(new URL(url));
            scale = ((float) size) / contentLength;

            if (scale > 1f) {
                byte[] bytes = IoUtil.readBytes(URLUtil.getStream(new URL(url)));
                baos.write(bytes);
                return baos;
            }

            ImgUtil.scale(URLUtil.getStream(new URL(url)), baos, scale);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }

        return baos;
    }

    public static void compressNetPic(String url, int size, File file) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        float scale;

        try {
            URL url2 = new URL(url);
            long contentLength = URLUtil.getContentLength(url2);
            scale = ((float) size) / contentLength;

            if (scale > 1f) {
                scale = 1f;
            }

            ImgUtil.scale(URLUtil.getStream(url2), fos, scale);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }

    /**
     * 图片等比例处理
     *
     * @param file
     * @return
     */
    public static InputStream compressScalePic(MultipartFile file) {
//        float scale = scale(file.getSize());
        try {
            InputStream inputStream = file.getInputStream();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(file.getBytes().length);
            ImgUtil.scale(inputStream, outputStream, 0.8f);
            return inputStream;
        } catch (IOException e) {
            e.printStackTrace();
            log.error("图片压缩失败");
        }
        return null;
    }

    public static float scale(long srcSize) {
        float scale = 0f;
        if (srcSize < 200 * 1000) {
            scale = 1.00f;
        } else if (srcSize < 500 * 1000) {
            scale = 0.57f;
        } else if (srcSize < 700 * 1000) {
            scale = 0.47f;
        } else if (srcSize < 1 * 1000 * 1000) {
            scale = 0.37f;
        } else if (srcSize < 2 * 1000 * 1000) {
            scale = 0.25f;
        } else if (srcSize < 4 * 1000 * 1000) {
            scale = 0.17f;
        } else if (srcSize < 5 * 1000 * 1000) {
            scale = 0.13f;
        } else if (srcSize < 10 * 1000 * 1000) {
            scale = 0.10f;
        } else {
            scale = 0.05f;
        }
        return scale;
    }
}
