package top.imwork.window.silos.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * 〈功能简述〉
 * 〈图片验证码生成器〉
 *
 * @author 江晓伟
 * @create 2021/8/8
 * @since 1.0.0
 */
public class ImageCodeUtils {
    /**
     * 默认验证码宽度
     */
    private int width = 160;
    /**
     * 默认高度
     */
    private int height = 40;
    /**
     * 默认长度（验证码字符个数）
     */
    private int codeCount = 4;
    /**
     * 默认过期时间(秒)
     */
    private int expireIn = 60;

    /**
     * 默认过期时间
     */
    private LocalDateTime expireTime = LocalDateTime.now().plusSeconds(expireIn);


    //图片加载到内存
    private BufferedImage bufferedImage;

    // 验证码干扰线数
    private int lineCount = 40;

    // 验证码
    private String code = null;

    private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public ImageCodeUtils() {
        createCodeImage();
    }

    public ImageCodeUtils(int width, int height) {
        this.width = width;
        this.height = height;
        createCodeImage();
    }

    public ImageCodeUtils(int width, int height, int codeCount, int lineCount) {
        this(width, height);
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        createCodeImage();
    }

    public ImageCodeUtils(int width, int height, int codeCount, int lineCount, LocalDateTime expireTime) {
        this(width, height);
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        this.expireTime = expireTime;
        createCodeImage();
    }

    public ImageCodeUtils(int width, int height, int codeCount, int lineCount, int expireIn) {
        this(width, height);
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
        createCodeImage();
    }

    private void createCodeImage() {
        //字符所在x坐标
        int x = 0;
        //字体高度
        int fontHeight = 0;
        //字符所在y坐标
        int codeY = 0;
        int red = 0;
        int green = 0;
        int blue = 0;
        x = width / (codeCount + 2);
        fontHeight = height - 2;
        codeY = height - 4;
        bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        Random random = new Random();
        graphics2D.setColor(Color.WHITE);
        graphics2D.fillRect(0, 0, width, height);
        Font font = new Font("Fixedays", Font.PLAIN, fontHeight);
        graphics2D.setFont(font);

        for (int i = 0; i < lineCount; i++) {
            //x轴第一个点的位置
            int x1 = random.nextInt(width);
            //y轴第一个点的位置
            int y1 = random.nextInt(height);
            //x轴第二个点的位置
            int x2 = x1 + random.nextInt(width >> 2);
            //y轴第二个点的位置
            int y2 = y1 + random.nextInt(height >> 2);

            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);

            graphics2D.setColor(new Color(red, green, blue));
            graphics2D.drawLine(x1, y1, x2, y2);
        }

        StringBuilder randomCode = new StringBuilder(codeCount);
        for (int i = 0; i < codeCount; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            graphics2D.setColor(new Color(red, green, blue));
            graphics2D.drawString(strRand, (i + 1) * x, codeY);
            randomCode.append(strRand);
        }
        code = randomCode.toString();
    }

    public void write(String path) throws IOException {
        OutputStream outputStream = new FileOutputStream(path);
        this.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    public void write(OutputStream outputStream) throws IOException {
        ImageIO.write(bufferedImage, "png", outputStream);
    }

    public BufferedImage getBufferedImage() {
        return bufferedImage;
    }

    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

    public LocalDateTime getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(LocalDateTime expireTime) {
        this.expireTime = expireTime;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getCodeCount() {
        return codeCount;
    }

    public void setCodeCount(int codeCount) {
        this.codeCount = codeCount;
    }

    public int getLineCount() {
        return lineCount;
    }

    public void setLineCount(int lineCount) {
        this.lineCount = lineCount;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public char[] getCodeSequence() {
        return codeSequence;
    }

    public void setCodeSequence(char[] codeSequence) {
        this.codeSequence = codeSequence;
    }

    public boolean isExpried() {
        //如果 过期时间 在 当前日期 之前，则验证码过期
        return LocalDateTime.now().isAfter(expireTime);
    }
}