package com.sifang.modules.utils.img;

import cn.hutool.core.io.IoUtil;
import com.sifang.modules.alioss.AliyunOSSClientUtil;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


public class DownImg {

    public String downImg(String bankCode) {


        String fileInfo = "";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = this.getClass().getResource("/../../plug-in/static/img/bankCode.png").openStream();

            Image src = ImageIO.read(inputStream);
            // 获取图片的高和宽
            int wideth = 1000;
            int height = 600;

            // 新增一个图片缓冲
            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
            // 设置圆角
            BufferedImage bufferedImageWithRadius = setRadius(image);
            Graphics2D g = bufferedImageWithRadius.createGraphics();
            g.drawImage(src, 0, 0, wideth, height, null);
            // 设置字体颜色（颜色也可以直接new定义rgba，例如new Color(17, 16, 44)）
            g.setColor(Color.BLACK);
            // size字体大小，Font.BOLD字体加粗
            g.setFont(new Font("宋体", Font.BOLD, 50));
            // 写入成果名称，由宽度减去我们测的宽度，就等于要开始写的位置

            String str = partition(bankCode);

            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(str, wideth - 865, height - 290);
            // 释放资源
            g.dispose();


            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            ImageIO.write(bufferedImageWithRadius, "png", byteArrayOutputStream);

            byteArrayOutputStream.flush();

            byte[] byteArray = byteArrayOutputStream.toByteArray();

            fileInfo = AliyunOSSClientUtil.uploadImage(byteArray, "png", "");
            fileInfo = StringUtil.trasferBackslash(fileInfo);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(inputStream);
            IoUtil.close(outputStream);
        }
        return fileInfo;

    }

    /**
     * 图片设置圆角
     *
     * @param srcImage
     * @return
     * @throws
     */
    public static BufferedImage setRadius(BufferedImage srcImage) {
        //int radius = (srcImage.getWidth() + srcImage.getHeight()) / 6;
        int radius = 75;
        return setRadius(srcImage, radius, 0, 0);
    }

    /**
     * 图片设置圆角
     *
     * @param srcImage
     * @param radius
     * @param border
     * @param padding
     * @return
     * @throws
     */
    public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) {
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        int canvasWidth = width + padding * 2;
        int canvasHeight = height + padding * 2;

        BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();
        gs.setComposite(AlphaComposite.Src);
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setColor(Color.WHITE);
        gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
        gs.setComposite(AlphaComposite.SrcAtop);
        gs.drawImage(setClip(srcImage, radius), padding, padding, null);
        if (border != 0) {
            gs.setColor(Color.GRAY);
            gs.setStroke(new BasicStroke(border));
            gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
        }
        gs.dispose();
        return image;

    }

    /**
     * 图片切圆角
     *
     * @param srcImage
     * @param radius
     * @return
     */
    public static BufferedImage setClip(BufferedImage srcImage, int radius) {
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();

        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
        gs.drawImage(srcImage, 0, 0, null);
        gs.dispose();
        return image;
    }

    public String partition(String num) {
        StringBuilder str = new StringBuilder(num);
        for (int i = 1; i <= str.length(); i++) {
            if (i <= 4 && i % 4 == 0) {
                str.insert(i, "  ");

            }

        }
        for (int i = 4; i <= str.length(); i++) {
            if (i > 4 && i % 6 == 0) {
                str.insert(i, "  ");
            }
        }
        str.deleteCharAt(4);
        str.deleteCharAt(5);
        return str.toString();
    }

}
