package xyz.wbsite.dbtool.web.config;

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 xyz.wbsite.dbtool.web.frame.base.Control;
import xyz.wbsite.dbtool.web.frame.base.Screen;
import xyz.wbsite.dbtool.web.frame.utils.LogUtil;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 请求处理器配置类，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 String SCREEN_PREFIX = "/screen/";
    public static String CONTROL_PREFIX = "/control/";
    public static String AJAX_PREFIX = "/ajax/";

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        String aPackage = this.getClass().getPackage().getName();
        Pattern compile = Pattern.compile("(.*)\\.config");
        Matcher matcher = compile.matcher(aPackage);
        if (matcher.find()) {
            String basePackage = matcher.group(1);
            registryScreen(basePackage + ".action.screen", beanDefinitionRegistry);
            registryControl(basePackage + ".action.control", beanDefinitionRegistry);
            registryAjax(basePackage + ".action.ajax", beanDefinitionRegistry);
        }
    }

    @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 beamName = beanClassName.replaceAll(basePackage + ".", SCREEN_PREFIX).replaceAll("\\.", "/").toLowerCase();
                LogUtil.i("registry screen " + beamName);
                return beamName;
            }
        });
        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 beamName = beanClassName.replaceAll(basePackage + ".", CONTROL_PREFIX).replaceAll("\\.", "/").toLowerCase();
                LogUtil.i("registry control " + beamName);
                return beamName;
            }
        });
        return classPathBeanDefinitionScanner.scan(basePackage);
    }

    private int registryAjax(String basePackage, BeanDefinitionRegistry beanDefinitionRegistry) {
        ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
        classPathBeanDefinitionScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                return true;
            }
        });
        classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() {
            @Override
            public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
                String beanClassName = beanDefinition.getBeanClassName();
                if (beanClassName != null && beanClassName.endsWith("Ajax")) {
                    beanClassName = beanClassName.substring(0, beanClassName.length() - 4);
                }
                String beamName = beanClassName.replaceAll(basePackage + ".", AJAX_PREFIX).replaceAll("\\.", "/").toLowerCase();
                LogUtil.i("registry ajax " + beamName);
                return beamName;
            }
        });
        return classPathBeanDefinitionScanner.scan(basePackage);
    }
}
