package com.weiqi.mis.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.weiqi.mis.annotation.LoginType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yyy on 2024/5/9 weiqi Co.,Ltd.
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LoginType anno = null;
        //判断是否有注解
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;

            if (handlerMethod.getMethod().isAnnotationPresent(LoginType.class)) {
                anno = handlerMethod.getMethod().getAnnotation(LoginType.class);
            } else if (handlerMethod.getBeanType().isAnnotationPresent(LoginType.class)) {
                anno = handlerMethod.getBeanType().getAnnotation(LoginType.class);
            }
        }

        if (anno == null) {
            return true;

        }else{//因为现在域名没有edit，所以获取不到cookie 暂时放开所有校验，之后将else注释掉即可
//            logger.info("String getServerName={}", request.getServerName());

            HttpSession session = request.getSession(false);

            if (session != null && session.getAttribute("USER") != null) {
                return true;
            } else {
                request.getRequestDispatcher("/mis/login").forward(request, response);
                //response.sendRedirect("/metrics/sms/login");
                return false;
            }
//
//            String token = CookieUtil.getCookieValueByName(request, "DXToken");
//
//            if (token .equalsIgnoreCase("E4A6869ACE81760AC226D82376807528")){
//                return true;
//            } else {
//                request.getRequestDispatcher("/metrics/sms/login").forward(request, response);
//                //response.sendRedirect("/metrics/sms/login");
//                return false;
//            }

        }


//        HttpServletRequest ra = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        Cookie cookie = CookieUtil.getCookieByName(ra, "_Admin");
//        if(cookie==null){
//            this.response(response, ReturnCode.INFO_ERROR.getCode(), ReturnCode.INFO_ERROR.getDesc());
//            logger.error("cookie为空cookie==null====={}",cookie);
//            return false;
//        }
//
//        MisUserinfo misUserinfo =misUserService.decryptUserinfo(cookie.getValue());
//
//        if ( misUserinfo==null) {
//
//                this.response(response, ReturnCode.INFO_ERROR.getCode(), ReturnCode.INFO_ERROR.getDesc());
//                logger.error("解析报错{}",misUserinfo);
//                return false;
//        }
//        return true;
    }

    private void response(HttpServletResponse response, int code, String message) throws IOException {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(requiredAppidResponse(code, message)));
    }

    public Map requiredAppidResponse(int code, String message) {
        HashMap<String, Object> result = new HashMap();
        result.put("returncode", code);
        result.put("message", message);
        return result;
    }
}
