package com.hotent.auth.server.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.UUID;
import com.hotent.auth.server.service.AuthenticationThirdPartyService;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "验证码接口")
@ApiGroup(group = {ApiGroupConsts.GROUP_AUTH})
public class AuthenticationCaptchaController {
    private static final int captchaWeight = 145;
    private static final int captchaHeight = 55;
    @Resource
    AuthenticationThirdPartyService authenticationThirdPartyService;

    @RequestMapping(value = "/sso/captcha/image", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "生成图形验证码", httpMethod = "GET", notes = "生成图形验证码")
    public void identifyImage(HttpServletResponse response,
                              @ApiParam(value = "图形验证码id,无值：生成验证码，有值:刷新验证码")
                              @RequestParam(name = "randImageId", required = false) String randImageId) throws AuthenticationException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(captchaWeight, captchaHeight, 4, 100);
        // 验证码值
        String code = lineCaptcha.getCode();
        // 把验证码的值存储到缓存
        if (randImageId == null) {
            randImageId = UUID.randomUUID().toString();
            // 保存图形码值
            authenticationThirdPartyService.saveCaptchaCode(randImageId, code);
        } else {
            // 更新图形码值，此时此刻 图形码可能已经过期删除，那就相对于保存一个新的
            authenticationThirdPartyService.saveCaptchaCode(randImageId, code);
        }
//        System.out.println("randImageCode===>" + code);
//        System.out.println("randImageId===>" + randImageId);
        // 图形验证码对应的UUID,id和验证码一一对应，用于刷新、验证
        response.setHeader("randImageId", randImageId);
        // 输出到客户端
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            // 图形验证码写出，写出到流
            lineCaptcha.write(outputStream);
        } catch (Exception e) {
            System.out.println("图形验证码输出错误");
        }
    }

    @RequestMapping(value = "/sso/captcha/imagebase64", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "生成图形验证码", httpMethod = "GET", notes = "生成图形验证码")
    public CommonResult<?> identifyImageBase64(@ApiParam(value = "图形验证码id,无值：生成验证码，有值:刷新验证码")
                                               @RequestParam(name = "randImageId", required = false) String randImageId) throws AuthenticationException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(captchaWeight, captchaHeight, 4, 100);
        // 验证码值
        String code = lineCaptcha.getCode();
        // 把验证码的值存储到缓存
        if (randImageId == null) {
            randImageId = UUID.randomUUID().toString();
            // 保存图形码值
            authenticationThirdPartyService.saveCaptchaCode(randImageId, code);
        } else {
            // 更新图形码值，此时此刻 图形码可能已经过期删除，那就相对于保存一个新的
            authenticationThirdPartyService.saveCaptchaCode(randImageId, code);
        }
        String imageBase64Data = lineCaptcha.getImageBase64Data();
        Map<String, String> ajax = new HashMap<>();
        ajax.put("randImageId", randImageId);
        ajax.put("imageBase64Data", imageBase64Data);
        return new CommonResult<>(true, "获取shortCode成功！", ajax);

    }
}
