package com.elab.spring.componts;

import java.util.*;

/**
 * bean的依赖结构图
 *
 * @author ： liukx
 * @time ： 2020/9/21 - 11:18
 */
public class BeanDependOnModel {

    /**
     * 类的名称
     */
    private Class clazz;

    /**
     * 字段对应的名称
     * K -> 接口
     * V -> 具体实现
     */
    private Map<Class, Map<Class, Class>> fieldMapList = new LinkedHashMap<>();

    /**
     * 注解对应的名称
     */
    private Map<Class, Set<String>> annotationValueList = new HashMap<>();

    /**
     * 被排除的包名对应的类名
     */
    private Map<String, Set<Class>> excludeClassMap = new LinkedHashMap<>();

    public Map<String, Set<Class>> getExcludeClassMap() {
        return excludeClassMap;
    }

    public void setExcludeClassMap(Map<String, Set<Class>> excludeClassMap) {
        this.excludeClassMap = excludeClassMap;
    }

    public Class getClazz() {
        return clazz;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    public Map<Class, Map<Class, Class>> getFieldMapList() {
        return fieldMapList;
    }

    public void setFieldMapList(Map<Class, Map<Class, Class>> fieldMapList) {
        this.fieldMapList = fieldMapList;
    }

    public Map<Class, Set<String>> getAnnotationValueList() {
        return annotationValueList;
    }

    public void setAnnotationValueList(Map<Class, Set<String>> annotationValueList) {
        this.annotationValueList = annotationValueList;
    }

    public Set<Class> getDependOnClassList() {
        return getDependOnClassList(null);
    }

    private boolean isExcludePackages(String[] excludePackages, Class clazz) {
        if (excludePackages == null || excludePackages.length == 0) {
            return false;
        }
        for (int i = 0; i < excludePackages.length; i++) {
            String name = clazz.getName();
            String excludePackage = excludePackages[i];
            if (name.startsWith(excludePackage)) {
                return true;
            }
        }
        return false;
    }

    public Set<Class> getDependOnClassList(String[] excludePackages) {

        Set<Class> clazzList = new HashSet<>();
        Map<Class, Map<Class, Class>> fieldMapList = getFieldMapList();
        fieldMapList.keySet().forEach((key) -> {
            Map<Class, Class> classClassMap = fieldMapList.get(key);
            classClassMap.forEach((K, V) -> {
                if (!isExcludePackages(excludePackages, V)) {
                    clazzList.add(V);
                }
            });
        });
        return clazzList;
    }
}
