package com.gingersoft.gsa.cloud.common.function.click;

import android.util.Log;
import android.view.View;

import com.gingersoft.gsa.cloud.common.R;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Calendar;

/**
 * 全局BaseQuickAdapter item點擊事件AOP
 */
@Aspect
public class BaseQueicItemClickAspect {
    static int TIME_TAG = R.id.click_time;

    /**
     * 方法切入点
     */
    @Pointcut("execution(* com.chad.library.adapter.base.listener.OnItemClickListener.onItemClick(..))")
    public void onItemSingleClickMethod() {
    }

    /**
     * 在连接点进行方法替换
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Around("onItemSingleClickMethod()")
    public void doItemSingleClickMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        View view = null;
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof View) {
                view = (View) arg;
            }
            if (view != null) {
                Object tag = view.getTag(TIME_TAG);
                long lastClickTime = ((tag != null) ? (long) tag : 0);
                Log.d("SingleClickAspect", "lastClickTime:" + lastClickTime);
                long currentTime = Calendar.getInstance().getTimeInMillis();

                if (currentTime - lastClickTime > 500) {//过滤掉500毫秒内的连续点击
                    view.setTag(TIME_TAG, currentTime);
                    Log.d("SingleClickAspect", "currentTime:" + currentTime);
                    joinPoint.proceed();//执行原方法
                } else {
                    Log.d("SingleClickAspect", "連點了");
                }
            }
        }
    }
}
