package cd.casic.plugin.core.register;

import cd.casic.plugin.core.register.filter.impl.BasicBeanFilter;
import cd.casic.plugin.core.register.filter.impl.ControllerFilter;
import cd.casic.plugin.core.register.filter.impl.MapperFilter;
import cd.casic.plugin.dataobject.pojo.PluginInfo;
import cd.casic.plugin.function.IPluginProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO
 * @author: mianbin
 * @date: 2025/5/22 20:17
 * @version: 1.0
 */
@Slf4j
@Component
public class SpringBeanRegisterProcessor implements IPluginProcessor {
    @Override
    public void initialize() throws Exception {

    }

    @Override
    public void registry(PluginInfo plugin) throws Exception {
        List<Class<?>> basicBeanClassList = new ArrayList<>();
        basicBeanClassList.addAll(plugin.getGroupClass(BasicBeanFilter.GROUP_NAME));
        basicBeanClassList.addAll(plugin.getGroupClass(MapperFilter.GROUP_NAME));
        basicBeanClassList.addAll(plugin.getGroupClass(ControllerFilter.GROUP_NAME));
//        if(!basicBeanClassList.isEmpty()){
//            plugin.getPluginApplicationContext().register(basicBeanClassList.toArray(new Class[0]));
//        }
        basicBeanClassList.forEach(clazz -> {
            AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(clazz);
            beanDefinition.setBeanClass(clazz);
            BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
            String beanName = beanNameGenerator.generateBeanName(beanDefinition, plugin.getPluginApplicationContext());
            plugin.getPluginApplicationContext().registerBeanDefinition(beanName, beanDefinition);
        });
    }

    @Override
    public void unRegistry(PluginInfo plugin) throws Exception {

    }
}
