package org.springblade.modules.app.manager;

import com.alibaba.fastjson.JSONObject;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.perfmonitor.annotation.ServiceTimeLog;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.dto.InnerCaptchaReqDTO;
import org.springblade.modules.app.dto.InnerCaptchaResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.app.constant.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CaptchaManager {

    @Autowired
    private CaptchaService captchaService;

    /**
     * 获取滑动验证码
     *
     * @param innerCaptchaReqDTO
     * @return com.wn.sspwidget.dto.captcha.InnerCaptchaResultDTO
     * @throws
     * @描述 :获取滑动验证码
     * @创建人:WangPanPan
     * @创建时间:2021-06-21 1:55 下午
     * @修改人:
     * @修改时间:
     * @修改描述:
     * @date 2021-06-21 1:55 下午
     */
    public InnerCaptchaResultDTO getCaptcha(InnerCaptchaReqDTO innerCaptchaReqDTO) {
        Validator.notNull(innerCaptchaReqDTO, ErrorCode.PARAM_DATA_NULL);
        Validator.notBlank(innerCaptchaReqDTO.getCaptchaType(), ErrorCode.CAPTCHA_TYPE_NULL);
        log.info("获取验证码,入参:{}", JSONObject.toJSONString(innerCaptchaReqDTO));
        CaptchaVO captchaVO = ModelConvertUtil.copyTo(innerCaptchaReqDTO, CaptchaVO.class);
        ResponseModel responseModel = captchaService.get(captchaVO);
        if (responseModel == null) {
            log.info("获取验证码,出参为空");
        } else {
            log.info("获取验证码,出参code:{}", responseModel.getRepCode());
        }
        return responseModel == null ? null : ModelConvertUtil.copyTo(responseModel, InnerCaptchaResultDTO.class);
    }

    /**
     * 验证滑动验证码
     *
     * @param innerCaptchaReqDTO
     * @return com.wn.sspwidget.dto.captcha.InnerCaptchaResultDTO
     * @throws
     * @描述 :验证滑动验证码
     * @创建人:WangPanPan
     * @创建时间:2021-06-21 1:55 下午
     * @修改人:
     * @修改时间:
     * @修改描述:
     * @date 2021-06-21 1:55 下午
     */
    @ServiceTimeLog
    public InnerCaptchaResultDTO checkCaptcha(InnerCaptchaReqDTO innerCaptchaReqDTO) {
        Validator.notNull(innerCaptchaReqDTO, ErrorCode.PARAM_DATA_NULL);
        Validator.notBlank(innerCaptchaReqDTO.getCaptchaType(), ErrorCode.CAPTCHA_TYPE_NULL);
        Validator.notBlank(innerCaptchaReqDTO.getPointJson(), ErrorCode.CAPTCHA_POINT_JSON_NULL);
        Validator.notBlank(innerCaptchaReqDTO.getToken(), ErrorCode.CAPTCHA_TOKEN_NULL);
        CaptchaVO captchaVO = ModelConvertUtil.copyTo(innerCaptchaReqDTO, CaptchaVO.class);
        log.info("校验验证码,入参:{}", JSONObject.toJSONString(innerCaptchaReqDTO));
        ResponseModel responseModel = captchaService.check(captchaVO);
        if (responseModel == null) {
            log.info("获取验证码,出参为空");
        } else {
            log.info("校验验证码,出参:{}", JSONObject.toJSONString(responseModel));
        }
        return responseModel == null ? null : ModelConvertUtil.copyTo(responseModel, InnerCaptchaResultDTO.class);
    }

    /**
     * 二次验证滑动验证码
     *
     * @param innerCaptchaReqDTO
     * @return
     */
    @ServiceTimeLog
    public InnerCaptchaResultDTO verificationCaptcha(InnerCaptchaReqDTO innerCaptchaReqDTO) {
        Validator.notNull(innerCaptchaReqDTO, ErrorCode.PARAM_DATA_NULL);
        Validator.notBlank(innerCaptchaReqDTO.getCaptchaVerification(), ErrorCode.CAPTCHA_VERIFICATION_NULL);
        CaptchaVO captchaVO = ModelConvertUtil.copyTo(innerCaptchaReqDTO, CaptchaVO.class);
        log.info("二次验证滑动验证码,入参:{}", JSONObject.toJSONString(innerCaptchaReqDTO));
        ResponseModel responseModel = captchaService.verification(captchaVO);
        if (responseModel == null) {
            log.info("二次验证滑动验证码,出参为空");
        } else {
            log.info("二次验证滑动验证码,出参:{}", JSONObject.toJSONString(responseModel));
        }
        return responseModel == null ? null : ModelConvertUtil.copyTo(responseModel, InnerCaptchaResultDTO.class);
    }
}

