package com.ssj.framework.core.common.util;

import org.apache.commons.lang3.RandomStringUtils;

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


public class ImageCaptchaUtil {

    private int width = 160;// 图片的宽度。

    private int height = 40;// 图片的高度。

    private int codeCount = 5;// 验证码字符个数

    private String code = null;// 验证码

    private BufferedImage buffImg = null;// 验证码图片Buffer
    Random random = new Random();

    public ImageCaptchaUtil() {
        creatImage();
    }

    public ImageCaptchaUtil(int width, int height) {
        this.width = width;
        this.height = height;
        creatImage();
    }

    public ImageCaptchaUtil(int width, int height, int codeCount) {
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        creatImage();
    }

    /**
     * 生成图片
     */
    private void creatImage() {
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) buffImg.getGraphics();

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        for (int i = 0; i < 10; i++) {
            g.setColor(getRandColor(150, 250));
            g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
        }
        g.setFont(getFont(28));
        g.setColor(getRandColor(10, 240));
        g.drawString(randomStr(), 10, 30);
    }

    /**
     * 得到随机字符
     * @param n
     * @return
     */
    private String randomStr() {
        String code = RandomStringUtils.randomAlphanumeric(this.codeCount);
        code = code.replace('0', 'W');
        code = code.replace('o', 'R');
        code = code.replace('I', 'E');
        code = code.replace('1', 'T');
        this.code = code;
        return code;
    }

    /**
     *  得到随机颜色
     * @param fc
     * @param bc
     * @return
     */
    private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    /**
     * 产生随机字体
     */
    private Font getFont(int size) {
        Random random = new Random();
        Font font[] = new Font[10];
        font[0] = new Font("Time NewsRoman", Font.PLAIN, size);
        font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
        font[2] = new Font("Fixedsys", Font.PLAIN, size);
        font[3] = new Font("Georgia", Font.PLAIN, size);
        font[4] = new Font("Arial", Font.PLAIN, size);
        font[5] = new Font("Verdana", Font.PLAIN, size);
        font[6] = new Font("Tahoma", Font.PLAIN, size);
        font[7] = new Font("Courier New", Font.PLAIN, size);
        font[8] = new Font("Arial Black", Font.PLAIN, size);
        font[9] = new Font("Quantzite", Font.PLAIN, size);
        return font[random.nextInt(10)];
    }

    /**
     * 写图片
     * @param sos
     * @throws IOException
     */
    public void write(OutputStream sos) throws IOException {
        ImageIO.write(buffImg, "png", sos);
        sos.close();
    }

    /**
     * 验证码图片Buff
     * @return
     */
    public BufferedImage getBuffImg() {
        return buffImg;
    }

    /**
     * 验证码字符
     * @return
     */
    public String getCode() {
        return code.toLowerCase();
    }


}
