package xyz.wbsite.config;

import xyz.wbsite.frame.base.Control;
import xyz.wbsite.frame.base.Screen;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * 请求处理器配置类，Screen及Control
 * <p>
 * 注册扫描Screen处理器 {@link ActionConfig#registryScreen}
 * 注册扫描Control处理器 {@link ActionConfig#registryControl}
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Configuration
public class ActionConfig implements BeanDefinitionRegistryPostProcessor {
    public static final String SCREEN_PREFIX = "screen";
    public static final String CONTROL_PREFIX = "control";

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        String aPackage = this.getClass().getPackage().getName();
        int i = registryScreen("xyz.wbsite.action.screen", beanDefinitionRegistry);
        int i1 = registryControl("xyz.wbsite.action.control", beanDefinitionRegistry);
        System.out.println();
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    private int registryScreen(String basePackage, BeanDefinitionRegistry beanDefinitionRegistry) {
        ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
        classPathBeanDefinitionScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                if (metadataReader.getClassMetadata().getSuperClassName().equals(Screen.class.getName())) {
                    return true;
                }
                return false;
            }
        });
        classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() {
            @Override
            public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
                String beanClassName = beanDefinition.getBeanClassName();
                String s = beanClassName.replaceAll(basePackage, SCREEN_PREFIX);
                return s.toLowerCase();
            }
        });
        return classPathBeanDefinitionScanner.scan(basePackage);
    }

    private int registryControl(String basePackage, BeanDefinitionRegistry beanDefinitionRegistry) {
        ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
        classPathBeanDefinitionScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                if (metadataReader.getClassMetadata().getSuperClassName().equals(Control.class.getName())) {
                    return true;
                }
                return false;
            }
        });
        classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() {
            @Override
            public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
                String beanClassName = beanDefinition.getBeanClassName();
                String s = beanClassName.replaceAll(basePackage, CONTROL_PREFIX);
                return s.toLowerCase();
            }
        });
        return classPathBeanDefinitionScanner.scan(basePackage);
    }
}
