package com.ssm.filter;

import com.google.gson.Gson;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.StaticBaseStaff;
import com.ssm.manage.response.MemberResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.service.SsmSysRolePowerService;
import com.ssm.manage.utils.Constant;
import com.ssm.util.RedisClient;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Objects;
import java.util.Set;

@Slf4j
//@Component
public class MemberLoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisClient<MemberResponse> redisClient;

    @Autowired
    private RedisClient<StaffResponse> staffRedisClient;

    @Autowired
    private SsmSysRolePowerService ssmSysRolePowerService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        StringBuffer requestURL = request.getRequestURL();
        if (requestURL.toString().contains("error") || requestURL.toString().contains("swagger")) {
            return true;
        }

//        HttpSession session = request.getSession();
        String token = request.getHeader("token");

        //StaffResponse staffResponse = new StaffResponse();
        //staffResponse.setToken(request.getHeader("token"));
        //staffResponse.setStoreId(Long.valueOf(request.getHeader("storeId")));
        //StaticBaseStaff.setBaseStaff(staffResponse);
        //StaticBaseStaff.setBaseStaff( Long.valueOf(request.getHeader("storeId")));

       /* SessionHelp.setToken(session,staffResponse.getToken());
        SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
        SessionHelp.setStoreId(session,staffResponse.getStoreId());
        SessionHelp.setStoreName(session,staffResponse.getStoreName());*/





        MemberResponse memberResponse = new MemberResponse();

        if (StringUtils.isBlank(token) ){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = null;
            GenericResponse genericResponse = new GenericResponse();
            genericResponse.setCodeAndMsg(ResponseCode.TOKEN_IS_EXPIRED);
            Gson gson = new Gson();
            try {
                out = response.getWriter();
                out.append(gson.toJson(genericResponse));
            } catch (Exception e) {
                log.error("MemberLoginInterceptor.preHandle", e);
            } finally {
                if (out != null) {
                    out.close();
                }
                return false;
            }
        }else {
//            String account = SessionHelp.getLoginAccount(session);
            //根据token去做验证
            MemberResponse memberResponse1 = redisClient.get(Constant.memberKey + token);
//            Set<String> keys = redisClient.keys("*" + token);
//            Set<String> staffKeys = staffRedisClient.keys("*" + token);
            //redis存在相关key 已登录 返回true
//            if (keys.size() > 0 || staffKeys.size() > 0){
            if (memberResponse1!=null && memberResponse1.getId()!=null){
                return true;
            }else {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json;charset=utf-8");
                PrintWriter out = null;
                GenericResponse genericResponse = new GenericResponse();
                genericResponse.setCodeAndMsg(ResponseCode.NOT_LOGIN);
                Gson gson = new Gson();
                try {
                    out = response.getWriter();
                    out.append(gson.toJson(genericResponse));
                } catch (Exception e) {
                    log.error("MemberLoginInterceptor.preHandle", e);
                } finally {
                    if (out != null) {
                        out.close();
                    }
                    return false;
                }
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
