package com.inspect.gateway.service.impl;

import com.inspect.base.core.exception.CaptchaException;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.utils.sign.Base64;
import com.inspect.base.core.utils.uuid.IdUtils;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.redis.service.RedisService;
import com.inspect.gateway.config.properties.CaptchaProperties;
import com.inspect.gateway.service.ValidateCodeService;
import com.google.code.kaptcha.Producer;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Resource(
            name = "captchaProducer"
    )
    private Producer captchaProducer;
    @Resource(
            name = "captchaProducerMath"
    )
    private Producer captchaProducerMath;
    private final RedisService redisService;
    private final CaptchaProperties captchaProperties;

    public ValidateCodeServiceImpl(RedisService redisService, CaptchaProperties captchaProperties) {
        this.redisService = redisService;
        this.captchaProperties = captchaProperties;
    }

    public AjaxResult createCaptcha() throws IOException, CaptchaException {
        AjaxResult ajax = AjaxResult.success();
        boolean captchaOnOff = this.captchaProperties.getEnabled();
        ajax.put("captchaOnOff", captchaOnOff);
        if (captchaOnOff) {
            String uuid = IdUtils.simpleUUID();
            String verifyKey = "captcha_codes:" + uuid;
            String capStr = null;
            String code = null;
            BufferedImage image = null;
            String captchaType = this.captchaProperties.getType();
            if ("math".equals(captchaType)) {
                String capText = this.captchaProducerMath.createText();
                capStr = capText.substring(0, capText.lastIndexOf(StringUtils.AT));
                code = capText.substring(capText.lastIndexOf(StringUtils.AT) + 1);
                image = this.captchaProducerMath.createImage(capStr);
            } else if ("char".equals(captchaType)) {
                capStr = code = this.captchaProducer.createText();
                image = this.captchaProducer.createImage(capStr);
            }

            this.redisService.setCacheObject(verifyKey, code, 2L, TimeUnit.MINUTES);
            FastByteArrayOutputStream os = new FastByteArrayOutputStream();

            try {
                ImageIO.write(image, "jpg", os);
            } catch (IOException var11) {
                return AjaxResult.error(var11.getMessage());
            }

            ajax.put("uuid", uuid);
            ajax.put("img", Base64.encode(os.toByteArray()));
        }
        return ajax;
    }

    public void checkCaptcha(String code, String uuid) throws CaptchaException {
        if (StringUtils.isEmpty(code)) {
            throw new CaptchaException("验证码不能为空");
        } else if (StringUtils.isEmpty(uuid)) {
            throw new CaptchaException("验证码已失效");
        } else {
            String verifyKey = "captcha_codes:" + uuid;
            String captcha = (String) this.redisService.getCacheObject(verifyKey);
            this.redisService.deleteObject(verifyKey);
            if (!code.equalsIgnoreCase(captcha)) {
                throw new CaptchaException("验证码错误");
            }
        }
    }
}
