package com.ads.common.interceptor;

import cn.hutool.core.util.StrUtil;
import com.ads.business.service.TokenService;
import com.ads.business.service.data.GetLoginOutData;
import com.ads.common.data.JsonResult;
import com.ads.common.exception.BusinessException;
import com.ads.common.exception.LoginException;
import com.ads.common.redis.RedisKeyConstant;
import com.ads.common.redis.RedisManager;
import com.ads.common.util.OnlineUserUtils;
import com.ads.common.util.UtilConst;
import com.ads.common.util.UtilMessage;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuqiang
 * @Description 权限拦截
 * @Date 2020/3/18 15:33
 */
@Component
public class SecurityInterceptor implements HandlerInterceptor {

    @Resource
    private TokenService tokenService;

    @Resource
    private RedisManager redisManager;

    private static final List<String> allowUrls = new ArrayList<>();

    static {
        allowUrls.add("/api/auth/config");
        allowUrls.add("/login");
        allowUrls.add("/api/file/*");
        allowUrls.add("/file/upload/*");
    }

    /**
     * 在调用controller具体方法前拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        String path = request.getServletPath();
//        if (true){
//            return true;
//        }
        if (path.contains("upload") ||path.contains("swagger") || path.contains("acutator") || path.contains("scheduler")) {
            return true;
        }
        if (path.contains(UtilConst.URL_ERROR)) {
            throw new BusinessException(UtilMessage.HTTP_ERROR);
        }
        if (allowUrls.contains(path)) {
            return true;
        }
        String method = request.getMethod();
        if (UtilConst.REQUEST_METHOD_OPTIONS.equalsIgnoreCase(method)) {
            return true;
        }
        String token = request.getHeader(UtilConst.LOGIN_TOKEN);

        if (StrUtil.isBlank(token)) {
            throw new LoginException();
        }
        GetLoginOutData data = null;
        if (path.contains("/api")){
            JsonResult<GetLoginOutData> userInfo = tokenService.getUserInfo(token);
            data = userInfo.getData();
        }else {
            String dataStr = (String) redisManager.get(RedisKeyConstant.PREFIX + token);
            if (StringUtils.isNotBlank(dataStr)){
                data = JSONObject.parseObject(dataStr,GetLoginOutData.class);
            }
        }
        if (data == null) {
            throw new LoginException();
        }
        /*GetLoginOutData data = new GetLoginOutData();
        data.setUserId(266437676720445783L);
        data.setName("侯昌维");
        data.setIsAdmin(true);
        data.setMobile("15236541545");*/
        OnlineUserUtils.set(data);
        return true;
    }

    /**
     * 完成页面的render后调用
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {
    }

    /**
     * 在调用controller具体方法后拦截
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
    }
}
