package com.zhangmeng.admin.manager.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.zhangmeng.admin.manager.config.verificationCode.VerificationCodeConfig;
import com.zhangmeng.admin.manager.service.VerificationCodeService;
import com.zhangmeng.admin.manager.utils.VerificationCodeUtil;
import com.zhangmeng.api.service.admin_manager.VerificationCodeControllerApi;
import com.zhangmeng.model.base.baseController.BaseController;
import com.zhangmeng.model.base.baseUtil.CommonUtil;
import com.zhangmeng.model.entity.VerificationCode;
import com.zhangmeng.model.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author zhangmeng
 * @version 1.0
 * @date 2021/1/21 21:41
 * <p>
 * 验证码控制器
 */

@RestController
@RequestMapping("/verificationCode")
public class VerificationCodeController extends BaseController implements VerificationCodeControllerApi {

    /**
     * 验证码 参数
     */
    private static final String SESSION_KEY = "captcha";

    private static final int DEFAULT_LEN = 4;  // 默认长度

    private static final int DEFAULT_WIDTH = 130;  // 默认宽度

    private static final int DEFAULT_HEIGHT = 48;  // 默认高度

    @Autowired
    private VerificationCodeConfig verificationCodeConfig;

    @Autowired
    private VerificationCodeService verificationCodeService;


    /**
     * 验证码生成
     *  @param request  请求报文 校验
     * @param response 响应报文
     * @return
     */
    @Override
    @RequestMapping("/generate")
    public void generate(HttpServletRequest request, HttpServletResponse response) {
        SpecCaptcha specCaptcha = new SpecCaptcha(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LEN);
        VerificationCodeConfig.Type type = this.verificationCodeConfig.getType();
        switch (type) {
            case MYSQL:
                this.verificationCodeService.mysql_save(specCaptcha);
                break;
            case REDIS:
                this.verificationCodeService.redis_save(specCaptcha);
                break;
        }
        try {
            //输出验证码到页面
            specCaptcha.out(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 异步验证
     *
     * @param request 请求报文
     * @param captcha 验证码
     * @return 验证结果
     */
    @Override
    @RequestMapping("/verify")
    public Result verify(HttpServletRequest request, String captcha) {
        if (com.wf.captcha.utils.CaptchaUtil.ver(captcha, request)) {
            return success("验证成功");
        }
        return failure("验证失败");
    }


    /**
     * 生成验证码
     *
     * @param httpServletRequest  imageIo
     * @param httpServletResponse imageIo
     */
    @Override
    @GetMapping("/generateVerificationCode/v1")
    public void generateVerificationCodeV1(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                           @RequestParam(name = "vl", defaultValue = "4") Integer vcodeLength,//vcodeLength,验证码长度
                                           @RequestParam(name = "fs", defaultValue = "21") Integer fontSize,//fontSize,验证码字体大小
                                           @RequestParam(name = "w", defaultValue = "98") Integer width,//width,图片宽度
                                           @RequestParam(name = "h", defaultValue = "33") Integer height//height,图片高度
    ) {
        VerificationCodeUtil verificationCodeUtil = new VerificationCodeUtil(vcodeLength, fontSize, width, height);
        String generatorVerificationCode = verificationCodeUtil.generatorVCode();
        this.verificationCodeService.cacheVerificationCode(generatorVerificationCode);
        try {
            ImageIO.write(verificationCodeUtil.generatorVCodeImage(generatorVerificationCode, true), "gif", httpServletResponse.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成验证码
     */
    @Override
    @GetMapping("/generateVerificationCode/v2")
    public String generateVerificationCodeV2() {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(111, 36, 6, 6);
        // 验证码对应的字符串
        String code = captcha.getCode();
        this.verificationCodeService.cacheVerificationCode(code);
        return captcha.getImageBase64();
    }


    /**
     * 模拟手机验证码
     */
    @Override
    @GetMapping("/generateVerificationCode/v3")
    public Result generateVerificationCodeV3(String telephone) {
        String verifierCode = CommonUtil.mobileVerifierCode(6);
        //保存至数据库
        VerificationCode verificationCode = new VerificationCode();
        verificationCode.setAddTime(new Date());
        verificationCode.setDeleteStatus(false);
        verificationCode.setType(VerificationCode.Type.pc);
        verificationCode.setStatus(VerificationCode.Status.register);
        verificationCode.setTelephone(telephone);
        verificationCode.setCode(verifierCode);
        this.verificationCodeService.save(verificationCode);
        return new Result(true, 0, "发送成功", verifierCode);
    }
}
