package com.izouma.nineth.utils;

import com.izouma.nineth.config.RedisKeys;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Service
@AllArgsConstructor
public class ValidateCodeUtils {
    private static final long serialVersionUID = -1409007752285164213L;
    private static int width = 90;
    private static int height = 35;
    private static char[] ch = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    private static String[] ch1 = {"+", "-", "×"};//,"÷"
    // 生成随机类
    private static final Random random = new Random();


    public static String processRequest(HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 在内存中创建图象
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文
        Graphics2D g = (Graphics2D)image.getGraphics();
        // 设定背景色
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        // 设定字体
        g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        // 画边框
        // g.setColor(new Color());
        // g.drawRect(0,0,width-1,height-1);
        // 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到
        int count = 15+ random.nextInt(15);
        //粗线
        g.setStroke(new BasicStroke(2.0F,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
        for (int i = 0; i < count; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(200);
            int yl = random.nextInt(200);
            g.setColor(getRandColor(100, 255));
            g.drawLine(x, y, x + xl, y + yl);
        }
        //还原
        g.setStroke(new BasicStroke());

        // 取随机产生的认证码(4位数字)
        int len = ch.length, len1 = ch1.length;
        int sum = ch[random.nextInt(len)];
        int sum1 = ch[random.nextInt(len)];
        String sum2 = ch1[random.nextInt(len1)];
        position(g, String.valueOf(sum), 0);
        position(g, String.valueOf(sum2), 1);
        position(g, String.valueOf(sum1), 2);
        position(g, "=", 3);
        position(g, "?", 4);

        //计算结果
        String r = String.valueOf(compute(sum2, sum, sum1));
        // 图象生效
        g.dispose();
        ServletOutputStream responseOutputStream = response.getOutputStream();
        // 输出图象到页面
        ImageIO.write(image, "JPEG", responseOutputStream);
        // 以下关闭输入流！
        responseOutputStream.flush();
        responseOutputStream.close();
        return r;
    }

    /**
     * 每个数字、符号的位置
     *
     * @param g
     * @param text
     * @param row
     */
    private static void position(Graphics g, String text, int row) {
        g.setColor(new Color(random.nextInt(88), random.nextInt(188), random.nextInt(255)));
        g.setFont(new Font("Arial", Font.ITALIC, 15 + random.nextInt(13)));// 输出的字体和大小
        g.drawString(text, row * 15 + 6, 20 + random.nextInt(6));// 写什么数字，在图片的什么位置画
    }

    private static int compute(String symbol, int sum, int sum1) {
        switch (symbol) {
            case "+":
                return sum + sum1;
            case "-":
                return sum - sum1;
            case "×":
                return sum * sum1;
//            case "÷":return sum/sum1;
        }
        return 0;
    }

    private static Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
        Random random = new Random();
        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);
    }
}
