package com.hc.web.aspect;

import com.hc.core.utils.exception.ResponseException;
import com.hc.core.utils.redis.RedisUtil;
import com.hc.core.utils.tool.Verification;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author JIA
 */

@Aspect
@Component
@Slf4j
public class FrontUserAspect {

    private final RedisUtil redisUtil;

    public FrontUserAspect(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }


    @Pointcut("execution (public * com.hc.web.controller.browser.front.*.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontUserController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontNewsTagController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontUserTeacherController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontClassConfigController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontBannerController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontUserController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontCourseController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontCourseTypeController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontNewsController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontAliPayController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontAreaDistrictController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontClassConfigController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontClassConfigSponsorController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontClassCourseController.*(..))" +
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontFloatWindowController.*(..))"+
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontNewsAttachmentController.*(..))"+
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontLiveController.*(..))"+
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontLiveClassController.*(..))"+
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontWxPayJsApiController.*(..))"+
            "&& ! execution (public * com.hc.web.controller.browser.front.FrontToolsController.*(..))"
    )

    public void verify() {

    }

    @Before("verify()")
    public void doVerify() {

//        //获取请求信息

        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        //从请求头中获取token
        String token = request.getHeader("token");

        if (!Objects.isNull(token)) {

            //身份识别 已取消

            //获取redis中的token 查看token是否过期
            if (Verification.verificationString((String) redisUtil.get(token))) {
                log.info("front 请求-token正常");
            } else {
                log.info("front 请求-redis中的token已过期，请重新登录");
                throw new ResponseException(-1, "front token is expired");
            }

        } else {
            log.info("front request-token is null");
            throw new ResponseException(-1, "front request-token is null");
        }
    }

}
