package com.jiayue.ssi.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jiayue.ssi.constant.Constants;
import com.jiayue.ssi.factory.LoginFactory;
import com.jiayue.ssi.util.IPUtils;
import com.jiayue.ssi.util.ResponseInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import com.jiayue.ssi.constant.CacheConstants;
import com.jiayue.ssi.util.LocalCache;

import lombok.RequiredArgsConstructor;

/**
 * 验证码过滤器
 *
 * @author xsl
 * @since 2023/02/20
 */
@RequiredArgsConstructor
@Order(10)
@Slf4j
public class VerifyCodeFilter extends OncePerRequestFilter {
    private String defaultFilterProcessUrl = "/user/login";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            if ("POST".equalsIgnoreCase(request.getMethod()) && defaultFilterProcessUrl.equals(request.getServletPath())) {
                // 验证码验证
                String username = request.getParameter("username");
                String requestCaptcha = request.getParameter("code");
                String verifyuuid = request.getParameter("verifyuuid");
                String murmur = request.getParameter("murmur");
                Object uuidObj = LocalCache.get(CacheConstants.CAPTCHA_CODE_KEY + verifyuuid + murmur);
                // 校验服务端验证码
                if (uuidObj == null || "".equals(uuidObj)) {
                    // 记录验证码失败日志
                    LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "验证码错误");
                    ResponseInfo.doResponse(response, "验证码错误！", 401);
                    return;
                }
                // 校验页面验证码
                if (StringUtils.isEmpty(requestCaptcha)) {
                    // 记录验证码失败日志
                    LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "验证码错误");
                    ResponseInfo.doResponse(response, "验证码错误！", 401);
                    return;
                }
                if (requestCaptcha.length() != 4) {
                    // 记录验证码失败日志
                    LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "验证码错误");
                    ResponseInfo.doResponse(response, "验证码错误！", 401);
                    return;
                }
                if (!String.valueOf(uuidObj).toLowerCase().equals(requestCaptcha.toLowerCase())) {
                    // 记录验证码失败日志
                    LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "验证码错误");
                    // 删除缓存验证码
                    LocalCache.remove(CacheConstants.CAPTCHA_CODE_KEY + verifyuuid + murmur);
                    ResponseInfo.doResponse(response, "验证码错误！", 401);
                    return;
                }
            }

        } catch (Exception e) {
            ResponseInfo.doResponse(response, "验证码错误！", 401);
            return;
        }
        filterChain.doFilter(request, response);
    }
}
