package com.fs.framework.security.filter;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.StringUtils;
import com.fs.framework.security.LoginAdminUser;
import com.fs.framework.security.LoginCompanyUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.framework.service.TokenService;
import com.fs.framework.utils.LoginUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token过滤器 验证token有效性
 *

 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisCache redisCache;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException
    {
        Object user = tokenService.getLoginUser(request);
//        LoginCompanyUser loginCompanyUser = tokenService.getLoginUser(request);
//        if (StringUtils.isNotNull(user) && StringUtils.isNull(SecurityUtils.getAuthentication()))
        if (StringUtils.isNotNull(user))
        {
            //获取不同对象
            String type = request.getHeader("X-Frontend-Type");
            if ("admin".equals(type)){
                LoginUser loginUser = (LoginUser)user;
                tokenService.verifyToken(loginUser);
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            } else if("company".equals(type)){
//                LoginCompanyUser loginUser =JSON.parseObject(JSON.toJSONString(user), LoginCompanyUser.class);
                com.fs.framework.security.LoginUser loginUser = (com.fs.framework.security.LoginUser) user;
                tokenService.verifyToken(loginUser);
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }

        }
        chain.doFilter(request, response);
    }
}
