package cn.qinys.platform.base.security.filter;


import cn.qinys.platform.base.redis.RedisKeyEnum;
import cn.qinys.platform.base.security.properties.JwtProperties;
import cn.qinys.platform.base.security.utils.CurrentUtils;
import cn.qinys.platform.base.utils.RedisUtils;
import cn.qinys.platform.base.vo.LoginUserInfo;
import jakarta.annotation.Resource;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Description:
 *
 * @author tanlie
 * Date: 2023/12/22 16:50
 */
@Component
@Slf4j
public class AuthenticationTokenFilter extends OncePerRequestFilter {
    private static final PathMatcher PATH_MATCHER = new AntPathMatcher();
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private RedisUtils redisUtils;


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        String path = request.getRequestURI();

        boolean ignore = false;
        for (String ignoreUrl : jwtProperties.getIgnoreUrls()) {
            if (PATH_MATCHER.match(ignoreUrl, path)) {
                ignore = true;
                break;
            }
        }
        if (ignore) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    "", "", new ArrayList<>());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authentication);
            chain.doFilter(request, response);
            return;
        }

        String token = CurrentUtils.getToken();
        if (StringUtils.hasLength(token)) {
            String key = RedisKeyEnum.CURRENT_USER.getCode() + token;
            LoginUserInfo userInfo = (LoginUserInfo) redisUtils.get(key);
            if (userInfo != null && SecurityContextHolder.getContext().getAuthentication() == null) {
                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                        userInfo.getUsername(), token, new ArrayList<>());
                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authentication);
            }
        }
        chain.doFilter(request, response);
    }
}
