/*
 */
package com.agricraft.agricore.json;

import com.agricraft.agricore.core.AgriCore;
import com.agricraft.agricore.registry.AgriLoadableRegistry;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

/**
 *
 * @author Ryan
 */
public class AgriFileWalker extends SimpleFileVisitor<Path> {

    public static final String MOD_PREFIX = "mod_";

    private final AgriLoadableRegistry[] registries;
    private final Path root;

    public AgriFileWalker(Path root, AgriLoadableRegistry... registries) {
        this.root = Objects.requireNonNull(root);
        this.registries = Objects.requireNonNull(registries);
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        // Filter null files.
        Objects.requireNonNull(dir);
        Objects.requireNonNull(attrs);

        // Get Localized Directory
        final String loc = root.relativize(dir).toString();

        // Get Directory Name
        final String dirname = dir.getFileName().toString();

        if (!dirname.startsWith(MOD_PREFIX)) {
            if (!loc.isEmpty() && !loc.contains("\\")) {
                AgriCore.getCoreLogger().debug("Loading: \"json:{0}\"!", loc);
            }
            return FileVisitResult.CONTINUE;
        }

        // Get Mod Name
        final String modid = dirname.substring(4);

        if (AgriCore.getValidator().isValidMod(modid)) {
            AgriCore.getCoreLogger().debug("Loading: \"json:{0}\" for mod: \"{1}\"!", loc, modid);
            return FileVisitResult.CONTINUE;
        } else {
            AgriCore.getCoreLogger().debug("Skipping: \"json:{0}\" for missing mod: \"{1}\"!", loc, modid);
            return FileVisitResult.SKIP_SUBTREE;
        }
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        // Filter Null
        Objects.requireNonNull(file);
        Objects.requireNonNull(attrs);

        // Load Registries
        for (AgriLoadableRegistry r : registries) {
            if (r.acceptsElement(file.getFileName().toString())) {
                AgriLoader.loadElement(root, file, r);
            }
        }

        return FileVisitResult.CONTINUE;
    }

}

@@ -2112,32 +2112,126 @@
 onNull(attrs);%0A%0A
+        // Boolean Marker to Detect Skipped JSON Files.%0A        boolean wasAccepted = false;%0A%0A
         // Load 
@@ -2241,16 +2241,16 @@
 istries%0A
-
         
@@ -2419,33 +2419,436 @@
 r);%0A            
-%7D
+    wasAccepted = true;%0A            %7D%0A        %7D%0A%0A        // If the file was not accepted, give the user a warning.%0A        if (!wasAccepted && file.getFileName().toString().toLowerCase().endsWith(%22.json%22)) %7B%0A            AgriCore.getCoreLogger().info(%0A                    %22Found a JSON file that was not accepted by any loader! Perhaps it has the wrong name?%5Cn%7B0%7D%22,%0A                    file%0A            );
 %0A        %7D%0A%0A    

Add JSON Loader Skipped File Hint