package com.zcxk.controller;


import com.zcxk.entity.ImageCode;
import com.zcxk.entity.ValidateCode;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.ResultStatus;
import com.zcxk.util.ByteArrayUtils;
import com.zcxk.util.RedisUtil;
import com.zcxk.util.VerifyCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@Api(tags = "验证码")
public class ValidateCodeController {

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/validateCode/image")
    @ApiOperation(value = "获取验证码")
    public void createCode(
            @ApiParam(value = "随机数 uuid", required = true, defaultValue = "1234") @RequestParam String random,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        ImageCode imageCode = createImageCode();
        ValidateCode validateCode = new ValidateCode(imageCode.getCode(), imageCode.getExpireTime());
        //保存redis
        String key = "validateCode:"+random;
        redisUtil.setExpire(key.getBytes(), ByteArrayUtils.objectToBytes(validateCode).get(), 300);//5分钟过期
        OutputStream out = response.getOutputStream();

        //设置响应头通知浏览器以图片的形式打开
        response.setContentType("image/png");
        //设置响应头控制浏览器不要缓存
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        ImageIO.write(imageCode.getImage(), "PNG", out);
        out.close();
    }

    private ImageCode createImageCode() throws IOException {
        String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_ONLY, 4, null);
        BufferedImage image = VerifyCodeUtil.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, new Color(0,6,11), null, null);
        ImageCode imageCode = new ImageCode(image, verifyCode, 300);//5分钟过期
        return imageCode;
    }

    @GetMapping("/validateCode/test")
    @ResponseBody
    @ApiOperation(value = "测试")
    public AjaxMessage getCode(
            @ApiParam(value = "随机数 uuid", required = true) @RequestParam String random
    ){
        AjaxMessage ajaxMessage = new AjaxMessage();
        String key = "validateCode:"+random;
        byte[] redisValidateCodeByte = redisUtil.get(key.getBytes());
        if (redisValidateCodeByte == null) {
            ajaxMessage.setMsg(ResultStatus.ERROR);
            return ajaxMessage;
        }
        ValidateCode validateCode = (ValidateCode) ByteArrayUtils.bytesToObject(redisValidateCodeByte).get();
        System.out.println(validateCode);
        ajaxMessage.setMsg(ResultStatus.OK,validateCode);
        return ajaxMessage;
    }
}
