package cc.smtweb.system.bpm.util;

import lombok.Getter;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.resizers.configurations.Antialiasing;
import net.sf.image4j.codec.ico.ICODecoder;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 缩略图生成工具
 *
 * @author xkliu
 */
@Getter
public class ThumbImage {
    // 图片处理方式
    public static final int TYPE_DEFAULT = 1;
    public static final int TYPE_THUMB = 2;
    public static final int TYPE_AVATAR = 3;

    private int imageWidth;
    private int imageHeight;

    public static int type(String thumb) {
        // 解决历史遗留boolean类型
        if (StringUtils.isBlank(thumb) || "false".equalsIgnoreCase(thumb)) {
            return TYPE_DEFAULT;
        }

        if ("true".equalsIgnoreCase(thumb)) {
            return TYPE_THUMB;
        }

        return Integer.parseInt(thumb);
    }

    public void makeThumb(boolean isThumb, File file, int size) throws IOException {
        makeThumb(file, size, size, isThumb);
    }

    // 后台等比压缩后大小最好控制在20k以内
    public void makeThumb(File file, int w, int h, boolean keepAspectRatio) throws IOException {
        String fileName = file.getName().toLowerCase();

        BufferedImage image;

        if (fileName.endsWith(".ico")) {
            List<BufferedImage> images = ICODecoder.read(file);
            image = images.get(images.size() - 1);
        } else {
            image = ImageIO.read(file);
        }

        imageWidth = image.getWidth();
        imageHeight = image.getHeight();

        if (fileName.endsWith(".png") || fileName.endsWith(".gif")) {
            // 把透明的图填充白色背景
            BufferedImage newBufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
            image = newBufferedImage;
        }

        Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(image);

        if (keepAspectRatio) {
            if (h > 0) {
                // 高度为基准调整宽度到达原图缩放比例
                int imageR = imageWidth * 1000 / imageHeight;
                w = h * imageR / 1000;
            } else {
                // 宽度为基准调整宽度到达原图缩放比例
                int imageR = imageHeight * 1000 / imageWidth;
                h = w * imageR / 1000;
            }

//			int r = w * 1000 / h;
//			int imageR = imageWidth * 1000 / imageHeight;
//			if (r != imageR) {
//				w = imageHeight * r / 1000;
//			}
        } else {
            int r = w * 1000 / h;
            int imageR = imageWidth * 1000 / imageHeight;

            if (r != imageR) {
                int width = imageWidth;
                int height = imageHeight;
                if (r > imageR) {
                    width = imageHeight * r / 1000;
                } else {
                    height = imageWidth * 1000 / r;
                }

                builder.sourceRegion(Positions.CENTER, width, height);
            }
        }

        builder.size(w, h).antialiasing(Antialiasing.ON).outputFormat("jpg").outputQuality(0.9)
                .toFile(file.getAbsolutePath() + FilePathGenerator.THUMB_FILE_EXT);
    }
}
