/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.auth.core;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.codvision.auth.enums.CaptchaEnum;
import com.codvision.base.entity.SysConfig;
import com.codvision.base.service.ISysConfigService;
import com.codvision.cachecore.utils.RedisUtil;
import com.codvision.commoncore.exception.AuthException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.codvision.auth.support.CacheContants.CUSTOM_CAPTCHA_CODE;

/**
 * 验证码服务
 *
 * @author lingee
 * @date 2023/11/29
 */
@Component
public class AuthCaptchaService {

    @Resource
    private ISysConfigService configService;

    @Resource
    private CaptchaService captchaService;

    /**
     * 校验验证码
     *
     * @param request http请求
     * @return 校验结果
     */
    public boolean check(String code, String reqNo, String captchaVerification) {
        // 判断当前验证码类型CAPTCHA_TYPE，默认CLOSED 无验证码功能，SIMPLE 普通验证码，BLOCK_PUZZLE 滑块验证码
        SysConfig config = configService.getByKey("CAPTCHA_TYPE");
        if (config == null || StringUtils.isBlank(config.getV())) {
            throw new IllegalArgumentException("验证码配置项CAPTCHA_TYPE不存在或者数值为空");
        }

        CaptchaEnum captchaEnum = null;
        try {
            captchaEnum = CaptchaEnum.valueOf(config.getV());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("不支持的验证码类型：" + config.getV());
        }

        switch (captchaEnum) {
            case CLOSED:
                return true;
            case SIMPLE:
                return simple(code, reqNo);
            case BLOCK_PUZZLE:
                return blockPuzzle(captchaVerification);
        }

        return true;
    }

    private boolean simple(String code, String reqNo) {
        if (StringUtils.isBlank(code) || StringUtils.isBlank(reqNo)) {
            throw new IllegalArgumentException("缺少验证码或请求号！");
        }

        String verifyCode = RedisUtil.get(CUSTOM_CAPTCHA_CODE + reqNo);
        if (StringUtils.isBlank(verifyCode)) {
            throw new AuthException(10002, "验证码已失效，请重新获取");
        }

        if (!code.equalsIgnoreCase(verifyCode)) {
            throw new AuthException(30001, "验证码错误");
        }

        RedisUtil.del(CUSTOM_CAPTCHA_CODE + reqNo);
        return true;
    }

    private boolean blockPuzzle(String captchaVerification) {
        if (StringUtils.isBlank(captchaVerification)) {
            throw new IllegalArgumentException("缺少验证码！");
        }

        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification(captchaVerification);
        ResponseModel responseModel = captchaService.verification(captchaVO);
        if (!responseModel.isSuccess()) {
            throw new AuthException(10002, responseModel.getRepMsg());
        }

        return true;
    }
}
