package de.dergrafiker.filenamecleaner;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.util.Set;

import static java.nio.file.FileVisitResult.CONTINUE;

@Component
public class FileVisitor extends SimpleFileVisitor<Path> {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileVisitor.class);
    private final RemovedCharsUtil removedCharsUtil;
    private final FilenameChecker filenameChecker;
    private final FilenameCleaner filenameCleaner;

    public FileVisitor(RemovedCharsUtil removedCharsUtil, FilenameChecker filenameChecker,
                       FilenameCleaner filenameCleaner) {
        this.removedCharsUtil = removedCharsUtil;
        this.filenameChecker = filenameChecker;
        this.filenameCleaner = filenameCleaner;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        if (exc != null) {
            throw exc;
        }

        String oldName = dir.getFileName().toString();
        boolean isDirectory = Files.isDirectory(dir);

        if (filenameChecker.isInvalid(oldName, isDirectory)) {
            String cleaned = filenameCleaner.clean(oldName, isDirectory);

            if (filenameChecker.isInvalid(cleaned, isDirectory)) {
                throw new IllegalArgumentException(
                        String.format("Name is still invalid after clean '%s' => '%s' [%s]",
                                      oldName,
                                      cleaned,
                                      dir.getParent().toAbsolutePath())
                );
            }

            reportRemovedChars(oldName, cleaned);

            LOGGER.info("RENAME '{}' => '{}' [{}]",
                        oldName,
                        cleaned,
                        dir.getParent().toAbsolutePath()
            );
        }
        LOGGER.trace("WALKED dir {}", dir);
        return CONTINUE;
    }

    private void reportRemovedChars(String oldName, String cleaned) {
        if (LOGGER.isInfoEnabled()) {
            Set<Character> removedChars = removedCharsUtil.getRemovedChars(oldName, cleaned);
            if (!removedChars.isEmpty()) {
                LOGGER.info("REMOVED_CHARS {} has removed >> {} << {}",
                            oldName,
                            StringUtils.join(removedChars),
                            cleaned);
            }
        }
    }
}

@@ -347,16 +347,68 @@
 isitor;%0A
+import java.nio.file.attribute.BasicFileAttributes;%0A
 import j
@@ -1251,32 +1251,333 @@
 exc;%0A        %7D%0A%0A
+        fixName(dir);%0A        LOGGER.trace(%22WALKED dir %7B%7D%22, dir);%0A        return CONTINUE;%0A    %7D%0A%0A    @Override%0A    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException %7B%0A        fixName(file);%0A        return CONTINUE;%0A    %7D%0A%0A    private void fixName(Path path) %7B%0A
         String o
@@ -1585,19 +1585,20 @@
 dName = 
-dir
+path
 .getFile
@@ -1664,19 +1664,20 @@
 rectory(
-dir
+path
 );%0A%0A    
@@ -2146,35 +2146,36 @@
                 
-dir
+path
 .getParent().toA
@@ -2416,19 +2416,20 @@
         
-dir
+path
 .getPare
@@ -2479,77 +2479,8 @@
   %7D%0A
-        LOGGER.trace(%22WALKED dir %7B%7D%22, dir);%0A        return CONTINUE;%0A
     

add fixName to files as well