package com.crm.rely.backend.interceptor;

import com.crm.rely.backend.aspect.ControllerAop;
import com.crm.rely.backend.configuration.LocaleMessage;
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.dto.base.BaseResultDto;
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.GetIpAndMac;
import com.crm.rely.backend.util.HttpServletRequestUtil;
import com.crm.rely.backend.util.InteceptorResultUtil;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

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

public class NoRepeatSubmitInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisService redisService;

    @Autowired
    private LocaleMessage localeMessage;

    private static final int EXPIRE_TIME_NOT_TOKEN = 500;

    private static final int EXPIRE_TIME = 1 * 1000;

    public NoRepeatSubmitInterceptor() {

    }

    public NoRepeatSubmitInterceptor(RedisService redisService,LocaleMessage localeMessage) {
        this.redisService = redisService;
        this.localeMessage = localeMessage;
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //如果为OPTIONS直接返回成功
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            return true;
        }
        String uri = httpServletRequest.getRequestURI();
        String token = httpServletRequest.getHeader(Constants.ACCESS_TOKEN);
        String ip = GetIpAndMac.getIp(httpServletRequest);
        String clientIp = GetIpAndMac.getClientIp(httpServletRequest);
        if (Strings.isNullOrEmpty(token) && Strings.isNullOrEmpty(clientIp)) {
            return true;
        }
        String key = token + ip + clientIp + uri;
        String uuid = UUID.randomUUID().toString();
        int expireTime;
        if (Strings.isNullOrEmpty(token)) {
            expireTime = EXPIRE_TIME_NOT_TOKEN;
        } else {
            expireTime = EXPIRE_TIME;
        }
        if (!redisService.tryLock(key, uuid, expireTime)) {
            InteceptorResultUtil.returnResult(httpServletResponse, BaseResultDto.result(Constants.FAILED_CODE,
                    localeMessage.getMessage("no_repeat_submit_error","no_repeat_submit_error")));
            httpServletResponse.setStatus(Constants.FAILED_CODE);
            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 {

    }
}
