package com.sf.vertx.handle;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import com.sf.vertx.api.pojo.GatewayInterface;
import com.sf.vertx.api.pojo.SacService;
import com.sf.vertx.api.pojo.Strategy;
import com.sf.vertx.service.impl.AppConfigServiceImpl;
import com.sf.vertx.utils.ProxyTool;

import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.HttpException;
import lombok.extern.slf4j.Slf4j;

/***
 * redis存储
 * 
 * @author xy
 *
 */
@Slf4j
public class RateLimitHandlerRedisImpl implements RateLimitHandler {
	private RedisRateLimiter rateLimiter;
	// private int pattern;// 1:app,2:接口默认,3:服务接口配置限流策略

	public RateLimitHandlerRedisImpl(RedisRateLimiter rateLimiter) {
		this.rateLimiter = rateLimiter;
	}

	@Override
	public void handle(RoutingContext rc) {
		String sacAppHeaderKey = rc.request().headers().get(AppConfigServiceImpl.getSacAppHeaderKey());
		String appHeaderServiceName = rc.request().getHeader(AppConfigServiceImpl.getAppHeaderServiceName());
		if (StringUtils.isBlank(sacAppHeaderKey) || StringUtils.isBlank(appHeaderServiceName)) { // 参数传递错误
			rc.fail(new HttpException(10003));
			return;
		}
		// 查询模式
		SacService sacService = AppConfigServiceImpl.getSacService(sacAppHeaderKey, appHeaderServiceName);
		if (sacService == null) { // 参数传递错误
			rc.fail(new HttpException(10003));
			return;
		}
		boolean matchUri = false;
		Strategy strategyInterface = null; // 接口
		if (sacService.getUriList() != null) {
			for (GatewayInterface uri : sacService.getUriList()) {
				if (uri.isUriRegular()) {
					if (ProxyTool.regexMatch(uri.getUri(), rc.request().uri())) {
						matchUri = true;
					}
				} else {
					if (StringUtils.equals(uri.getUri(), rc.request().uri())) {
						matchUri = true;
					}
				}
				if (matchUri) {
					for (Strategy strategy : uri.getStrategy()) {
						if (StringUtils.equals(strategy.getType(), "CURRENT_LIMITING")) {
							strategyInterface = strategy;
						}
					}
					break;
				}
			}
		}

		strategyInterface = strategyInterface != null ? strategyInterface
				: (AppConfigServiceImpl.getAppConfig(sacAppHeaderKey) != null
						&& AppConfigServiceImpl.getAppConfig(sacAppHeaderKey).getApiCurrentLimitingConfig() != null
								? AppConfigServiceImpl.getAppConfig(sacAppHeaderKey).getApiCurrentLimitingConfig()
								: null);

		Strategy strategyApp = AppConfigServiceImpl.getAppConfig(sacAppHeaderKey) != null
				&& AppConfigServiceImpl.getAppConfig(sacAppHeaderKey).getAppCurrentLimitingConfig() != null
						? AppConfigServiceImpl.getAppConfig(sacAppHeaderKey).getAppCurrentLimitingConfig()
						: null;
		if (strategyInterface != null || strategyApp != null) {
			Map<Integer, Boolean> retMap = rateLimiter.acquire(rc, strategyInterface, strategyApp);

			if (strategyInterface != null && retMap.get(1) == false) {
				rc.fail(new HttpException(10101, strategyInterface.getDefaultResponse()));
				return;
			}

			if (strategyApp != null && retMap.get(2) == false) {
				rc.fail(new HttpException(10102, strategyApp.getDefaultResponse()));
				return;
			}
		}

		log.info("rateLimiter.acquire true");
		rc.next();
		return;
	}
}
