package com.ywt.outpatient.interceptors;//package com.ywt.wapapi.web.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
///**
// * Created by huangguoping.
// */
//@Service("authInterceptor")
public class AuthInterceptor implements HandlerInterceptor {
//
////    @Resource(name = "gatewayConfigFactory")
////    private GatewayConfigFactory gatewayConfigFactory;
//
//    private void toResponse(BaseResponse response, HttpServletResponse httpServletResponse) throws IOException {
//        httpServletResponse.addHeader("Content-Type", "application/json;charset=UTF-8");
//        String responseData = JsonSerializer.toJson(response);
//        httpServletResponse.setStatus(200);
//        byte[] responseDataBytes = responseData.getBytes("utf-8");
//        httpServletResponse.setContentLength(responseDataBytes.length);
//        httpServletResponse.getOutputStream().write(responseDataBytes);
//    }
//
//    @Override
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//        //System.out.println(String.format("%s 验证签名... http %s", httpServletRequest.getRequestURI(), httpServletRequest.getMethod()));
//        try {
////            String token = httpServletRequest.getParameter("token");
//            String token = Checker.getStringValue(httpServletRequest.getHeader("token"));
//            WebAppContext.current().setTokenID(token);
//
////            Enumeration<String> paramterNames = httpServletRequest.getParameterNames();
////            if (paramterNames != null && paramterNames.hasMoreElements()) {
////                Set<String> paramterNamesSoredSet = new TreeSet<>();
////                while (paramterNames.hasMoreElements()) {
////                    String keyName = paramterNames.nextElement();
////                    paramterNamesSoredSet.add(keyName);
////                }
////                if (paramterNamesSoredSet.contains("sign") && paramterNamesSoredSet.contains("appid")) {
////                    String sign_str = Checker.nullToEmptyString(httpServletRequest.getParameter("sign"));
////                    paramterNamesSoredSet.remove("sign");
////
////                    String appid=Checker.nullToEmptyString(httpServletRequest.getParameter("appid"));
////                    //TODO 根据appid获取相关信息
////                    GatewayConfig appConfig = gatewayConfigFactory.get(appid);
////
////                    if (appConfig == null) {
////                        BaseResponse response = new BaseResponse();
////                        response.setCode(BaseResponse.NO_RIGHT);
////                        response.setInfo("不合法的appid");
////                        this.toResponse(response, httpServletResponse);
////                        return false;
////                    }
////                    String appSecret = appConfig.getSecret(); // temp setting
////
////                    String beSignedStr = "";
////                    for (String pName : paramterNamesSoredSet) {
////                        String temp = pName + "=" + Checker.nullToEmptyString(httpServletRequest.getParameter(pName));
////                        beSignedStr = beSignedStr.equals("") ? temp : beSignedStr + "&" + temp;
////                    }
////                    beSignedStr += appSecret;
////                    String genSignStr = MD5CheckSum.md5(beSignedStr);
////
////                    if (!genSignStr.equals(sign_str)) { // 验证签名
////                        BaseResponse response = new BaseResponse();
////                        response.setCode(response.SIGN_EEROR);
////                        response.setInfo("验证签名不通过");
////                        this.toResponse(response, httpServletResponse);
////                        return false;
////                    }
////
////                    String appVersion = Checker.getStringValue(httpServletRequest.getHeader("app-version"));
////                    WebAppContext.current().setAppVersion(appVersion);  //设置App版本号
////
////                    WebAppContext.current().setAppID(appid); //设置请求上下文的 appid
////                    WebAppContext.current().setPlatform(appConfig.getPlatform());//设置平台
////                    WebAppContext.current().setClientType(appConfig.getType());
////                } else {
////                    //System.out.println("参数中必须包含sign和appid !!");
////                    BaseResponse response = new BaseResponse();
////                    response.setCode(response.PARAMETER_ERROR);
////                    response.setInfo("不正确的请求参数");
////                    this.toResponse(response, httpServletResponse);
////                    return false;
////                }
////            } else {
////                BaseResponse response = new BaseResponse();
////                response.setCode(response.PARAMETER_ERROR);
////                response.setInfo("不正确的请求参数");
////                this.toResponse(response, httpServletResponse);
////                return false;
////            }
//        }catch (Exception e){
//            BaseResponse response = new BaseResponse();
//            response.setCode(BaseResponse.APP_EXCEPTION);
//            response.setInfo(e.getMessage());
//            this.toResponse(response, httpServletResponse);
//            return false;
//        }
//        return true;
//    }
//
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
