package com.czg.aspect;

import cn.hutool.core.util.StrUtil;
import com.czg.annotation.Debounce;
import com.czg.resp.CzgResult;
import com.czg.utils.SpELUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;


/**
 * 防抖切面
 * @author ww
 */
@Aspect
@Component
public class DebounceAspect {

	@Pointcut("@annotation(com.czg.annotation.Debounce)")
	public void debouncePointCut() {
		
	}

	// 用于存储基于方法和入参情况的上次执行时间，结构为：方法签名 -> (入参值 -> 上次执行时间)
	private static final ConcurrentHashMap<String, ConcurrentHashMap<Object, Long>> EXECUTION_TIME_MAP = new ConcurrentHashMap<>();
	private static final ReentrantLock LOCK = new ReentrantLock();

	@Around("debouncePointCut()")
	public Object aroundDebounce(ProceedingJoinPoint joinPoint) throws Throwable {
		cleanExpiredRecords();
		String methodSignature = joinPoint.getSignature().toLongString();
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Debounce annotation =  signature.getMethod().getAnnotation(Debounce.class);
		long interval = annotation.interval();
		TimeUnit timeUnit = annotation.timeUnit();
		String value = annotation.value();
		if (StrUtil.isBlank(value)) {
			// 没有指定入参表达式，按照整个方法进行防抖
			return debounceForWholeMethod(joinPoint, methodSignature, interval, timeUnit);
		}
		String[] split = value.split(",");
		StringBuilder values = new StringBuilder();
		for (String str : split) {
			values.append(SpELUtil.getByEl(str, joinPoint));
		}
		// 解析入参表达式，获取对应入参的值

		if (StrUtil.isBlank(values.toString())) {
			// 如果解析失败或值为null，按照整个方法进行防抖
			return debounceForWholeMethod(joinPoint, methodSignature, interval, timeUnit);
		}

		// 根据方法签名和入参值进行防抖判断
		return debounceForSpecificValue(joinPoint, methodSignature, interval, timeUnit, values.toString());
	}

	private Object debounceForWholeMethod(ProceedingJoinPoint joinPoint, String methodSignature, long interval, TimeUnit timeUnit) throws Throwable {
		ConcurrentHashMap<Object, Long> methodExecutionTimeMap = EXECUTION_TIME_MAP.computeIfAbsent(methodSignature, k -> new ConcurrentHashMap<>());
		long currentTime = System.currentTimeMillis();
		Long lastTime = methodExecutionTimeMap.get(methodSignature);
		if (lastTime == null) {
			// 如果不存在对应键值对，设置初始值，这里设置为一个表示很久之前的时间戳，比如0
			lastTime = 0L;
		}
		if (currentTime - timeUnit.toMillis(interval) >= lastTime) {
			// 满足防抖间隔，更新上次执行时间，并执行目标方法
			methodExecutionTimeMap.put(methodSignature, currentTime);
			return joinPoint.proceed();
		}
		// 在防抖间隔内，不执行目标方法，直接返回
		return CzgResult.failure("请求频繁，请重试");
	}

	private Object debounceForSpecificValue(ProceedingJoinPoint joinPoint, String methodSignature, long interval, TimeUnit timeUnit, Object targetValue) throws Throwable {
		ConcurrentHashMap<Object, Long> methodExecutionTimeMap = EXECUTION_TIME_MAP.computeIfAbsent(methodSignature, k -> new ConcurrentHashMap<>());
		long currentTime = System.currentTimeMillis();
		Long lastTime = methodExecutionTimeMap.get(targetValue);
		if (lastTime == null || currentTime - timeUnit.toMillis(interval) >= lastTime) {
			// 满足防抖间隔，更新上次执行时间，并执行目标方法
			methodExecutionTimeMap.put(targetValue, currentTime);
			return joinPoint.proceed();
		}
		// 在防抖间隔内，不执行目标方法，直接返回
		return CzgResult.failure("请求频繁，请重试");
	}

	public void cleanExpiredRecords() {
		long expirationTime = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(10);
		LOCK.lock();
		try {
			for (Entry<String, ConcurrentHashMap<Object, Long>> outerEntry : EXECUTION_TIME_MAP.entrySet()) {
				String methodSignature = outerEntry.getKey();
				ConcurrentHashMap<Object, Long> innerMap = outerEntry.getValue();
				ConcurrentHashMap<Object, Long> keysToRemove = new ConcurrentHashMap<>();
				for (Entry<Object, Long> innerEntry : innerMap.entrySet()) {
					if (innerEntry.getValue() < expirationTime) {
						keysToRemove.put(innerEntry.getKey(), innerEntry.getValue());
					}
				}
				innerMap.keySet().removeAll(keysToRemove.keySet());
				if (innerMap.isEmpty()) {
					EXECUTION_TIME_MAP.remove(methodSignature);
				}
			}
		} finally {
			LOCK.unlock();
		}
	}
}
