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.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
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;
        }

        final File file = dir.toFile();
        String oldName = file.getName();
        if (filenameChecker.isInvalid(oldName, file.isDirectory())) {
            String cleaned = filenameCleaner.clean(oldName, file.isDirectory());

            Set<Character> removedChars = removedCharsUtil.getRemovedChars(oldName, cleaned);

            if (LOGGER.isInfoEnabled() && !removedChars.isEmpty()) {
                LOGGER.info("REMOVEDCHARS {} has removed >> {} << {}",
                            oldName,
                            StringUtils.join(removedChars),
                            cleaned);
            }

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


}

@@ -190,29 +190,8 @@
 t;%0A%0A
-import java.io.File;%0A
 impo
@@ -252,16 +252,44 @@
 Result;%0A
+import java.nio.file.Files;%0A
 import j
@@ -1219,30 +1219,29 @@
     
-final File fil
+String oldNam
 e = dir.
 toFi
@@ -1240,59 +1240,89 @@
 dir.
+ge
 t
-o
 File
-();%0A        String oldName = file.getName(
+Name().toString();%0A        boolean isDirectory = Files.isDirectory(dir
 );%0A
+%0A
     
@@ -1356,37 +1356,32 @@
 nvalid(oldName, 
-file.
 isDirectory())) 
@@ -1375,18 +1375,16 @@
 irectory
-()
 )) %7B%0A   
@@ -1440,21 +1440,16 @@
 ldName, 
-file.
 isDirect
@@ -1451,18 +1451,16 @@
 irectory
-()
 );%0A%0A    
@@ -1706,36 +1706,32 @@
                 
-    
 oldName,%0A       
@@ -1715,36 +1715,32 @@
        oldName,%0A
-    
                 
@@ -1779,20 +1779,16 @@
 Chars),%0A
-    
         
@@ -1892,36 +1892,32 @@
                 
-    
 oldName,%0A       
@@ -1901,36 +1901,32 @@
        oldName,%0A
-    
                 
@@ -1962,16 +1962,11 @@
     
-    file
+dir
 .get
@@ -1975,18 +1975,13 @@
 rent
-File().get
+().to
 Abso
@@ -1990,16 +1990,29 @@
 tePath()
+%0A            
 );%0A     

use nio