package com.xy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.anji.captcha.model.common.RepCodeEnum;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.util.StringUtils;
import com.xy.service.CaptchaService;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;

/***
 * 验证码服务类
 * @author 谭斌
 * @date 2022/12/15 14:10
 */
@Api(tags = "滑块验证码操作")
@Service
@RequiredArgsConstructor
public class CaptchaServiceImpl implements CaptchaService {

    private final com.anji.captcha.service.CaptchaService captchaService;

    @Override
    public R<String> reVerification(String captchaVerification) {
        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification(captchaVerification);
        ResponseModel response = captchaService.verification(captchaVO);
        if (response.isSuccess() == false) {
            String repCode = response.getRepCode();
            //验证码校验失败，返回信息告诉前端
            return R.fail(getRepDesc(repCode));
        }
        return R.ok();

    }

    /**
     * 获取验证码
     *
     * @param data
     * @param request
     * @return
     */
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @PostMapping("/get")
    public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) {
        assert request.getRemoteHost() != null;
        data.setBrowserInfo(getRemoteId(request));
        return captchaService.get(data);
    }

    /**
     * 验证码校验
     *
     * @param data
     * @param request
     * @return
     */
    @ApiOperation(value = "验证码校验", notes = "验证码校验")
    @PostMapping("/check")
    public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(getRemoteId(request));
        return captchaService.check(data);
    }


    /**
     * 根据code获取描述
     *
     * @param code
     * @return
     */

    public static String getRepDesc(String code) {
        RepCodeEnum[] repCodeEnums = RepCodeEnum.values();
        for (RepCodeEnum repCodeEnum : repCodeEnums) {
            if (repCodeEnum.getCode().equals(code)) {
                return repCodeEnum.getDesc();
            }
        }
        return StrUtil.EMPTY;
    }

    public static final String getRemoteId(HttpServletRequest request) {
        String xfwd = request.getHeader("X-Forwarded-For");
        String ip = getRemoteIpFromXfwd(xfwd);
        String ua = request.getHeader("user-agent");
        if (StringUtils.isNotBlank(ip)) {
            return ip + ua;
        }
        return request.getRemoteAddr() + ua;
    }

    private static String getRemoteIpFromXfwd(String xfwd) {
        if (StringUtils.isNotBlank(xfwd)) {
            String[] ipList = xfwd.split(",");
            return StringUtils.trim(ipList[0]);
        }
        return null;
    }
}
