package com.yidianling.ydlcommon.custom.aop;

import android.util.Log;
import com.umeng.analytics.dplus.UMADplus;
import com.yidianling.ydlcommon.core.base.BaseApplication;
import com.yidianling.common.tools.LogUtil;
import com.yidianling.ydlcommon.BuildConfig;
import com.yidianling.ydlcommon.custom.YdlBuryPointUtil;
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 java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jiucheng
 * @描述:埋点的实际执行处，aop思想实现埋点功能
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/9/26
 */
@Aspect
public class PointAspect {
    @Pointcut("execution(@com.yidianling.ydlcommon.aop.PointTrace * *(..))")
    public void pointcut() {
    }


    @Around("pointcut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();// 方法
        if (BuildConfig.DEBUG) {
            String name = signature.getName(); // 方法名
            Class returnType = signature.getReturnType(); // 返回值类型
            Class declaringType = signature.getDeclaringType(); // 方法所在类名
            String[] parameterNames = signature.getParameterNames(); // 参数名
            Class[] parameterTypes = signature.getParameterTypes(); // 参数类型

            LogUtil.e("AOP----ClassName:" + declaringType.getName());
            LogUtil.e("AOP----MethodName:" + name);
            LogUtil.e("AOP----ReturnType:" + returnType.getName());

            StringBuilder builder = new StringBuilder();
            for (String parameterName : parameterNames) {
                builder.append(parameterName + "；");
            }
            LogUtil.e("AOP----ParameterNames:" + builder.toString());

            StringBuilder stringBuilder = new StringBuilder();
            for (Class parameterType : parameterTypes) {
                stringBuilder.append(parameterType.getName() + "；");
            }
            LogUtil.e("AOP----ParameterTypes:" + stringBuilder.toString());
        }
//        PointTrace annotation = method.getAnnotation(PointTrace.class);
        //事件名称
        String eventName = null;
        //属性名称
        String propertyName = null;
        //属性值
        String propertyValue = null;
        //执行方法
        joinPoint.proceed();
        //获取方法执行时参数：三个参数： 事件名String、属性名String、属性值String
        Object[] objects = joinPoint.getArgs();
        if (objects != null && objects.length == 3) {
            if (objects[0] instanceof String) {
                eventName = (String) objects[0];
            }
            if (objects[1] instanceof String) {
                propertyName = (String) objects[1];
            }
            if (objects[2] instanceof String) {
                propertyValue = (String) objects[2];
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put(propertyName, propertyValue);

        UMADplus.track(BaseApplication.Companion.getApp(), eventName, map);
        if (BuildConfig.DEBUG) {
            LogUtil.e("AOP----eventName:" + eventName);
            LogUtil.e("AOP----propertyName:" + propertyName);
            LogUtil.e("AOP----propertyValue:" + propertyValue);
        }
    }

    // =========分割线===========分割线============分割线===============分割线==============分割线===========

    /**
     * 新版埋点（长链接jar包实现埋点）
     */
    @Pointcut("execution(@com.yidianling.ydlcommon.aop.CustomPointTrace * *(..))")
    public void customPointCut() {

    }

    /**
     * 新版埋点（长链接jar包实现埋点）
     */
    @Around("customPointCut()")
    public void customAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();// 方法
        if (BuildConfig.DEBUG) {
            String name = signature.getName(); // 方法名
            Class returnType = signature.getReturnType(); // 返回值类型
            Class declaringType = signature.getDeclaringType(); // 方法所在类名
            String[] parameterNames = signature.getParameterNames(); // 参数名
            Class[] parameterTypes = signature.getParameterTypes(); // 参数类型

            LogUtil.e("AOP----ClassName:" + declaringType.getName());
            LogUtil.e("AOP----MethodName:" + name);
            LogUtil.e("AOP----ReturnType:" + returnType.getName());

            StringBuilder builder = new StringBuilder();
            for (String parameterName : parameterNames) {
                builder.append(parameterName + "；");
            }
            LogUtil.e("AOP----ParameterNames:" + builder.toString());

            StringBuilder stringBuilder = new StringBuilder();
            for (Class parameterType : parameterTypes) {
                stringBuilder.append(parameterType.getName() + "；");
            }
            LogUtil.e("AOP----ParameterTypes:" + stringBuilder.toString());
        }
        //执行方法
        joinPoint.proceed();
        Object[] objects = joinPoint.getArgs();
        if (objects != null && objects.length == 1) {
            if (objects[0] instanceof String) {
                String name = (String) objects[0];
                YdlBuryPointUtil.sendClick(name);
                Log.e("http---------------","sendClick="+name);
            }
        }
        Log.e("数据:" ,objects.toString());
    }
}
