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.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(11)
@Slf4j
public class MailCodeFilter 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())) {
                // 是否需要邮箱口令验证
                if (true) {
                    // 验证码验证
                    String username = request.getParameter("username");
                    Object mailCode = LocalCache.get(CacheConstants.MAIL_CODE_KEY + username);

                    // 校验服务端验证码
                    if (mailCode == null || "".equals(mailCode)) {
                        // 记录用户失败日志
                        LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "邮箱口令错误");
                        ResponseInfo.doResponse(response, "邮箱口令无效,需要重新获取！", 401);
                        return;
                    }
                    // 页面录入的邮箱口令
                    String mailbox = request.getParameter("mailbox");

                    // 校验页面验证码
                    if (StringUtils.isEmpty(mailbox)) {
                        // 记录用户失败日志
                        LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "邮箱口令错误");
                        ResponseInfo.doResponse(response, "非法访问,邮箱口令错误！", 401);
                        return;
                    }
                    if (mailbox.length() != 6) {
                        // 记录用户失败日志
                        LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "邮箱口令错误");
                        ResponseInfo.doResponse(response, "需要6位邮箱口令！", 401);
                        return;
                    }
                    if (!String.valueOf(mailCode).toLowerCase().equals(mailbox.toLowerCase())) {
                        // 记录用户失败日志
                        LoginFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "邮箱口令错误");
                        // 删除缓存邮箱口令
                        LocalCache.remove(CacheConstants.MAIL_CODE_KEY + username);
                        ResponseInfo.doResponse(response, "邮箱口令错误！", 401);
                        return;
                    }
                }
            }
            filterChain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseInfo.doResponse(response, "邮箱口令错误！", 401);
            return;
        }
    }
}
