package com.kxs.transfer.api.util;
import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Spring 上下文实用程序
 *
 * @author 没秃顶的码农
 * @date 2024-01-25
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    //获取applicationContext
    @Getter
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
 
    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
 
    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    /**
     * 通过注解调用方法
     * 通过方法注解代理执行方法
     *
     * @param annotation 注解
     * @param args       参数
     */
    public static void invokeMethodByAnnotation(Class<? extends Annotation> annotation,
                                                   Object... args) {
        List<Object> beans = getBeansByMethodAnnotation(annotation);
 
        for (Object bean : beans) {
            List<Method> methods = getMethodsByAnnotation(bean, annotation);
            for (Method method : methods) {
                try {
                    method.invoke(bean,args);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 通过方法注解获取 Bean
     *
     * @param annotation 注解
     * @return {@link List}<{@link Object}>
     */
    public static List<Object> getBeansByMethodAnnotation(Class<? extends Annotation> annotation) {
        List<Object> list = new ArrayList<>();
        String[] beanNames = getApplicationContext().getBeanDefinitionNames();
 
        for (String beanName : beanNames) {
            Object bean = getBean(beanName);
            List<Method> methodsByAnnotation = getMethodsByAnnotation(bean, annotation);
 
            if (methodsByAnnotation.isEmpty()){
                //方法未匹配则跳过
                continue;
            }
 
            list.add(bean);
        }
 
        return list;
    }

    /**
     * 通过注解获取方法
     *
     * @param bean       豆
     * @param annotation 注解
     * @return {@link List}<{@link Method}>
     */
    private static  List<Method> getMethodsByAnnotation(Object bean, Class<? extends Annotation> annotation) {
        List<Method> methodList = new ArrayList<>();
        Method[] methods = bean.getClass().getMethods();

        for (Method item : methods) {
            Annotation annotation1 = item.getAnnotation(annotation);
            if (annotation1!=null) {
                methodList.add(item);
            }
        }
 
        return methodList;
    }
 
}