package com.hzs.common.security.interceptor;

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

import com.hzs.common.core.enums.ELoginType;
import com.hzs.common.security.auth.AuthMemberUtil;
import com.hzs.member.base.dto.LoginMember;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import com.hzs.common.core.constant.SecurityConstants;
import com.hzs.common.core.context.SecurityContextHolder;
import com.hzs.common.core.utils.ServletUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.security.auth.AuthUserUtil;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.dto.LoginUser;

/**
 * 自定义请求头拦截器，将Header数据封装到线程变量中方便获取
 * 注意：此拦截器会同时验证当前用户有效期自动刷新有效期
 *
 * @author hzs
 */
public class HeaderInterceptor implements AsyncHandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
        SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
        SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
        SecurityContextHolder.setUserType(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_TYPE));
        SecurityContextHolder.setUserCountry(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_COUNTRY));

        String token = SecurityUtils.getToken();

        if (StringUtils.isNotEmpty(token)) {
            if (ELoginType.USER.getValue().equals(SecurityContextHolder.getUserType())
                    || ELoginType.SUPPLIER.getValue().equals(SecurityContextHolder.getUserType())
            ) {
                // 后台 管理员、供应商 处理
                LoginUser loginUser = AuthUserUtil.getLoginUser(token);
                if (StringUtils.isNotNull(loginUser)) {
                    AuthUserUtil.verifyLoginUserExpire(loginUser);
                    SecurityContextHolder.set(SecurityConstants.LOGIN_USER, loginUser);
                    SecurityContextHolder.setUserCountry(loginUser.getDataCountry().toString());
                }
            } else {
                // 后台 会员、分享会员 处理
                LoginMember loginMember = AuthMemberUtil.getLoginMember(token);
                if (StringUtils.isNotNull(loginMember)) {
                    AuthMemberUtil.verifyLoginMemberExpire(loginMember);
                    SecurityContextHolder.set(SecurityConstants.LOGIN_MEMBER, loginMember);
                }
            }
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        SecurityContextHolder.remove();
    }

}
