package com.chaozhanggui.admin.system.interceptor;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.admin.system.annotation.OpLog;
import com.chaozhanggui.admin.system.util.RedisCst;
import com.chaozhanggui.admin.system.util.RedisUtil;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.common.system.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.lang.reflect.Method;


public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger log= LoggerFactory.getLogger(LoginInterceptor.class);


    private RedisUtil redisUtil;

    public LoginInterceptor(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//      获取进过拦截器的路径
        String requestURI = request.getRequestURI();
        String loginName=request.getHeader("loginName");
        String token=request.getHeader("token");
        String userId=request.getHeader("userId");

        OutputStream outputStream=response.getOutputStream();
        String userInfo=  redisUtil.getMessage(RedisCst.ONLINE_USER.concat(":").concat(loginName));
        if(ObjectUtil.isEmpty(userInfo)){
            log.error("token 不存在或已经过期");
            outputStream.write(JSONUtil.toJsonStr(new RespBody("999999")).getBytes("UTF-8"));
            return false;
        }
        JSONObject object=JSONObject.parseObject(userInfo);
        if(!object.containsKey("token")||!token.equals(object.getString("token"))){
            log.error("token 不存在或已经过期");
            outputStream.write(JSONUtil.toJsonStr(new RespBody("999999")).getBytes("UTF-8"));
            return false;
        }

        if(!object.containsKey("user")||!object.getJSONObject("user").containsKey("loginName")||!loginName.equals(object.getJSONObject("user").getString("loginName"))
            ||!object.getJSONObject("user").containsKey("userId")||!userId.equals(object.getJSONObject("user").get("userId").toString())
        ){
            log.error("用户信息错误");
            outputStream.write(JSONUtil.toJsonStr(new RespBody("999999")).getBytes("UTF-8"));
            return false;
        }

        return true;
    }

    /**
     * 目标方法执行完成以后
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 页面渲染以后
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
