package kotowari.scaffold.util;

import java.io.File;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

/**
 * @author kawasima
 */
public class BasePackageDetector {

    public static Set<String> scan(File sourceDirectory) {
        Set<String> packages = new HashSet<>();
        File[] directories = sourceDirectory.listFiles(f -> f != null && f.isDirectory());
        if (directories != null) {
            for (File dir : directories) {
                scanPackageInner(dir, null, packages);
            }
        }
        return packages;
    }

    private static void scanPackageInner(File dir, String pkg, Set<String> packages) {
        if (!dir.isDirectory()) {
            return;
        }
        File[] files = dir.listFiles();
        if (files == null)
            return;

        Boolean isAllDirectory = true;
        for (File f : files) {
            isAllDirectory &= (f != null && f.isDirectory());
        }
        if (isAllDirectory) {
            for (File f : files) {
                String prefix = (pkg == null) ? "" : pkg + ".";
                scanPackageInner(f, prefix + dir.getName(), packages);
            }
        } else {

            packages.add(pkg == null ? dir.getName() : pkg + "." + dir.getName());
        }
    }

    private static int packageDepth(String pkgName) {
        int cnt = 0, i = 0;
        while ((i = pkgName.indexOf(".", i)) == -1) {
            cnt++;
        }
        return cnt;

    }

    protected static String detectInMavenProject(File sourceDirectory) {
        return scan(sourceDirectory).stream()
                .filter(pkg -> !pkg.equals("db.migration"))
                .sorted(Comparator.comparing(BasePackageDetector::packageDepth).reversed())
                .findFirst()
                .orElse("");
    }

    public static String detect(String sourcePath) {
        File sourceDirectory = new File(sourcePath);
        if (sourceDirectory.exists()) {
            String pkg = detectInMavenProject(sourceDirectory);
            return pkg.isEmpty() ? pkg : pkg + ".";
        } else {
            // Unknown package
            return "";
        }
    }

    public static String detect() {
        return detect("src/main/java");
    }
}

@@ -921,32 +921,33 @@
 = (f != null && 
+(
 f.isDirectory())
@@ -937,32 +937,49 @@
 (f.isDirectory()
+ %7C%7C f.isHidden())
 );%0A        %7D%0A   

Modify BasePackageDetector to ignore hidden files something like dotfiles in *nix.