package com.crm.manager.interceptor;

import com.crm.manager.service.UserRoleService;
import com.crm.rely.backend.aspect.ControllerAop;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.constant.RoleConstants;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.exception.LoginException;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.RedisService;
import com.crm.rely.backend.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


public class RoleInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisService redisService;
    @Autowired
    private UserRoleService roleInfoService;

    @Autowired
    private ControllerAop controllerAop;
    @Value("${role.googleVerified:false}")
    private boolean googleVerified;

    public RoleInterceptor() {

    }

    public RoleInterceptor(RedisService redisService, UserRoleService roleInfoService, ControllerAop controllerAop,
                           boolean googleVerified) {
        this.redisService = redisService;

        this.roleInfoService = roleInfoService;

        this.controllerAop = controllerAop;
        this.googleVerified = googleVerified;
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                             Object o) throws Exception {

        //如果为OPTIONS直接返回成功
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            return true;
        }
        InfoEntity infoEntity = HttpServletRequestUtil.getLoginInfo(InfoEntity.class, httpServletRequest, redisService
                , true);

        if (infoEntity == null) {
            throw new LoginException(Constants.INVALID);
        }
        if (googleVerified && (infoEntity.getVerified() == null || !infoEntity.getVerified().equals(1))) {
            throw ServiceException.exception(Constants.GOOGLE_NOT_BIND_FAILED_CODE,
                    Constants.GOOGLE_SECRET_NOT_BIND_ERROR);
        }

        if (PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            return true;
        }

        String key = null;
        if (infoEntity.getIbInfo().getRoleId() == null || infoEntity.getIbInfo().getRoleId() == 0) {
            key = RoleConstants.ROLE_USER_ACTION + RoleConstants.ROLE_ADMIN;
        } else {
            key = RoleConstants.ROLE_USER_ACTION + infoEntity.getIbInfo().getRoleId().toString();
        }
        String requestPath = httpServletRequest.getRequestURI();
        /*
         * 验证权限
         */
        if (!redisService.hasKey(key)) {
            roleInfoService.getUserNodeDisplay(infoEntity.getIbInfo().getRoleId());
        }
        List<String> actionList = redisService.getObject(key);
        if (actionList == null || actionList.size() == 0) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
        for (String action : actionList) {
            if (requestPath.startsWith(action)) {
                return true;
            }
        }
        throw new ServiceException(Constants.NOT_PERMIT);
    }

    @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 {

    }
}