package com.muchinfo.mtp3century.interceptor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.muchinfo.mtp3century.utils.RedisServiceUtil;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3common.utils.EncryptionUtils;
import com.muchinfo.mtp3century.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Objects;

@Slf4j
@Component
public class SignAuthInterceptor implements HandlerInterceptor {
    @Resource
    private RedisServiceUtil redisUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String timestampStr = request.getHeader("timestamp");
        if (StringUtils.isBlank(timestampStr)) {
            log.debug("timestamp不能为空...........");
            ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM008.getCode(), request.getHeader("Accept-Language"));
            return false;
        }
        //重复请求判断
//        Object timeCache = redisUtils.getCacheObject(request.getHeader("Sign-Id")+"-timestamp");
//        if(Objects.isNull(timeCache)){
//            redisUtils.setCacheObject(request.getHeader("Sign-Id")+"-timestamp", timestampStr, RedisConstants.CACHE_NULL_TTL, TimeUnit.MINUTES);
//        }else{
//            if(timestampStr.equals(timeCache)){
//                log.debug("不可多次重复请求...........");
//                ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "系统异常,请联系管理员！", false);
//                return false;
//            }
//            redisUtils.setCacheObject(request.getHeader("Sign-Id")+"-timestamp", timestampStr, RedisConstants.CACHE_NULL_TTL, TimeUnit.MINUTES);
//        }
        String sign = request.getHeader("sign");
        if (StringUtils.isBlank(sign)) {
            log.debug("sign不能为空...........");
            ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM008.getCode(), request.getHeader("Accept-Language"));
            return false;
        }
        //获取redis存入的sign
        Object cacheObject = redisUtils.getCacheObject(request.getHeader("Sign-Id"));
        if (Objects.isNull(cacheObject)) {
            log.debug("sign签名失效...........");
            ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM008.getCode(), request.getHeader("Accept-Language"));
            return false;
        }
        char[] encryptedChars = String.valueOf(cacheObject).toCharArray();
        // 对字符数组进行排序
        Arrays.sort(encryptedChars);
        // 将排序后的字符数组转换回字符串
        String sortedText = new String(encryptedChars);
        //通过后台重新签名校验与前端签名sign值比对，确认当前请求数据是否被篡改
        if (!(sign.equalsIgnoreCase(EncryptionUtils.encryptSHA(sortedText + timestampStr)))) {
            log.debug("sign签名校验失败...........");
            ResponseUtils.writeFiled(response, HttpServletResponse.SC_BAD_GATEWAY, MessageType.MESSAGE_ERROR_CODE_SYSTEM008.getCode(), request.getHeader("Accept-Language"));
            return false;
        }
        log.debug("签名校验通过，放行...........");
        //放行
        return true;
    }
}
