// @topic T11925 Recursive dir traversal with Files.walkFileTree and DirectoryStream
// @brief class DirectoryParser implements recursive traversal algorithm

package week13;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import static week13.AppMain.printFileInfo;

public class DirectoryParser {
    //---------------------------------------
    // data attributes
    //---------------------------------------
    private Path directoryPath;
    private Path largestFilePath;
    private long largestFileSize;
    
    //---------------------------------------
    // constructors
    //---------------------------------------
    public DirectoryParser( Path directoryPath )
    {
        this.directoryPath = directoryPath;
        largestFileSize = 0;
    }

    //---------------------------------------
    // operations
    //---------------------------------------
    public void traverseDirectory() {
        traverseDirectory(directoryPath);
    }

    public void traverseDirectory(Path directoryPath) {
        try {
            DirectoryStream<Path> dirStream
                    = Files.newDirectoryStream(directoryPath);

            for (Path path : dirStream) {
                if (Files.isRegularFile(path)) {
                    AppMain.printFileInfo(path);
                    // TODO: check file size instead
                    // update data as needed
                    System.out.println();
                } else {
                    System.out.println("DIR " + path.getFileName());
                    traverseDirectory(path);
                }
            }
        } catch (IOException ex) {
            System.out.println(
                    "*** ERROR, ["
                    + directoryPath.getFileName()
                    + "] is not a directory.");

        }
    }//traverseDirectory
    
}//class DirectoryParser