package com.huimv.admin.session.config;

import com.huimv.admin.exception.PlatformException;
import com.huimv.admin.exception.constant.ExceptionConstant;
import com.huimv.admin.session.HmSession;
import com.huimv.admin.session.SessionHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

@Slf4j
public class WebApiInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HttpSession session = request.getSession();
        String sessionId = session.getId();
        System.out.println("admin拦截器 sessionId>>"+sessionId);
//        if (sessionId == null) {
//            System.out.println("0000000000000");
//            log.info("session:{}",session);
//            log.info("session id is null");
//            throw new PlatformException(ExceptionConstant.REQUEST_NOT_AUTHORIZE, "未找到授权信息00000");
//        }

        final HandlerMethod handlerMethod = (HandlerMethod) handler;
        final Class<?> clazz = handlerMethod.getBeanType();
        final Method method = handlerMethod.getMethod();
        if (clazz.isAnnotationPresent(ApiToken.class) || method.isAnnotationPresent(ApiToken.class)) {
            log.info("BeanType:{}",clazz.getName());
            HmSession hmSession = (HmSession) session.getAttribute("hmSession");
            if(hmSession == null){
                throw new PlatformException(ExceptionConstant.REQUEST_NOT_AUTHORIZE, "未找到授权信息");
            }
//            if(hmSession.getMemberAccount() == null){
//                throw new PlatformException(ExceptionConstant.ACCOUNT_NOT_AUTHORIZE, "未找到账户信息");
//            }
//            if(hmSession.getOrganizeInfo() == null){
//                throw new PlatformException(ExceptionConstant.ORG_NOT_AUTHORIZE, "未找到组织信息");
//            }

            //设置请求参数，api只要接受就好，也不用做二次校验：
            //HashMap newParam = new HashMap(request.getParameterMap());
            //newParam.put("memberAccount", hmSession.getMemberAccount());
            //newParam.put("organizeInfo", hmSession.getOrganizeInfo());
            //request = new ParameterRequestWrapper(request, newParam);

            //Map<String, String[]> parameterMap = new HashMap(request.getParameterMap());

//            request.setAttribute("memberAccount", hmSession.getMemberAccount());
//            request.setAttribute("organizeInfo", hmSession.getOrganizeInfo());
//            SessionHolder.setMemberAccount(hmSession.getMemberAccount());
//            SessionHolder.setOrganizeInfo(hmSession.getOrganizeInfo());
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
    }

    //方法执行之后拦截
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    	//log.info("释放一下，防止线程复用",SessionHolder.getOrganizeInfo());
//    	SessionHolder.removeAll();
    }
}