package utils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Base64;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Class used for methods to read from- save to- or manipulate File Objects and related tasks.
 */
public final class FileUtils {


    /**
     * Retrieves the extension of a file given its filename.
     *
     * @param filename Filename of the file.
     * @return The extension of the file.
     */
    public static String getExtension(String filename) {
        if (!filename.contains(".")) {
            return filename;
        }
        return getExtension(filename.substring(filename.indexOf(".") + 1));
    }

    /**
     * Retrieves a file from the Resources folder of Graphter Effects.
     *
     * @param path Path that indicates the location of the file in question from the resource folder.
     * @return The file requested.
     * @throws IOException Thrown when the file could not be loaded.
     */
    public static File fromResources(String path) throws IOException {
        URL url = FileUtils.class.getClassLoader().getResource(path);
        if (url == null) {
            throw new IOException("Resource file " + path + " not found.");
        }
        return new File(url.getFile());
    }

    /**
     * Returns a list of ALL files in a directory and all its subdirectories, recursively.
     *
     * @param dir Directory to search files in.
     * @return All files within this directory.
     */
    public static List<File> recursiveInDirectory(File dir) throws IOException {
        List<File> res = new LinkedList<>();
        if (dir.isFile()) {
            res.add(dir);
            return res;
        }
        File[] directoryListing = dir.listFiles();
        if (directoryListing == null) {
            throw new IOException();
        }
        for (File child : directoryListing) {
            if (child.isDirectory()) {
                res.addAll(recursiveInDirectory(child));
            } else {
                res.add(child);
            }
        }
        return res;
    }

    /**
     * Reads a file and returns its Base64 String representation.
     *
     * @param file The file to be read.
     * @return The Base64 String representation.
     * @throws IOException Thrown when the file could not be read.
     */
    public static String ImageToBase64(File file) throws IOException {
        return Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
    }

    public static String readFromFile(File file) throws IOException {
        return Files.readAllLines(file.toPath()).stream().collect(Collectors.joining());
    }


    /**
     * Reads a file and returns a String containing its Base64 String representation that can be used in an SVG image.
     *
     * @param file The File to be read.
     * @return The String representation that can be used in SVG generation.
     * @throws IOException Thrown when the File could not be read.
     */
    //TODO: Refactor to somewhere else
    public static String getImageSVG(File file) throws IOException {
        String extension = FileUtils.getExtension(file.getName()).toLowerCase();
        switch (extension) {
            case "jpg":
            case "jpeg":
            case "jpe":
            case "jif":
            case "jfif":
            case "jfi":
                return "data:image/jpg;base64," + ImageToBase64(file);
            case "png":
                return "data:image/png;base64," + ImageToBase64(file);
            default:
                System.err.println("WARNING: Unknown image format: ." + extension);
                return "data:image/false;base64," + ImageToBase64(file);
        }
    }


}

@@ -123,19 +123,21 @@
 ava.
-util.Base64
+nio.file.Path
 ;%0Aim
@@ -151,26 +151,22 @@
 va.util.
-LinkedList
+Base64
 ;%0Aimport
@@ -595,106 +595,15 @@
     
-if (!filename.contains(%22.%22)) %7B%0A            return filename;%0A        %7D%0A        return getExtension(
+return 
 file
@@ -626,17 +626,21 @@
 ilename.
-i
+lastI
 ndexOf(%22
@@ -647,16 +647,29 @@
 .%22) + 1)
+.toLowerCase(
 );%0A    %7D
@@ -1574,508 +1574,86 @@
     
-List%3CFile%3E res = new LinkedList%3C%3E();%0A        if (dir.isFile()) %7B%0A            res.add(dir);%0A            return res;%0A        %7D%0A        File%5B%5D directoryListing = dir.listFiles();%0A        if (directoryListing == null) %7B%0A            throw new IOException();%0A        %7D%0A        for (File child : directoryListing) %7B%0A            if (child.isDirectory()) %7B%0A                res.addAll(recursiveInDirectory(child));%0A            %7D else %7B%0A                res.add(child);%0A            %7D%0A        %7D%0A        return res
+return Files.walk(dir.toPath()).map(Path::toFile).collect(Collectors.toList())
 ;%0A  
@@ -2232,17 +2232,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -2562,47 +2562,8 @@
  */%0A
-    //TODO: Refactor to somewhere else%0A
     
@@ -2696,248 +2696,33 @@
 e())
-.toLowerCase();%0A        switch (extension) %7B%0A            case %22jpg%22:%0A            case %22jpeg%22:%0A            case %22jpe%22:%0A            case %22jif%22:%0A            case %22jfif%22:%0A            case %22jfi%22:%0A                return %22data:image/jpg;
+;%0A        String 
 base64
-,%22 +
+ =
  Ima
@@ -2751,289 +2751,68 @@
     
-    case %22png%22:%0A                return %22data:image/png;base64,%22 + ImageToBase64(file);%0A            default:%0A                System.err.println(%22WARNING: Unknown image format: .%22 + extension);%0A                return %22data:image/false;base64,%22 + ImageToBase64(file);%0A        %7D%0A    %7D%0A%0A
+return %22data:image/%22 + extension + %22;base64,%22 + base64;%0A    %7D
 %0A%7D%0A

Use nio