package org.texttechnologylab.project.gruppe_05_1.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class FileUtils {




    /**
     * Creates a (possibly nested) directory
     * @param dir (e.g. "generated" , "level1/level2/level3" etc.
     */
    public static boolean createDirectoryIFNotExists(String dir) {
        File directory = new File(dir);
        if (!directory.exists()){
            return directory.mkdirs();
        }
        return true;
    }

    /**
     * Write a list of Strings to file
     * @param fileName the filename
     * @param stringsList the list of strings to be written to the file
     */
    public static void writeStringsToFile(String fileName, List<String> stringsList) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(fileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (String str : stringsList) {
            try {
                fileWriter.write(str + System.lineSeparator());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @param fileName the filename
     * @param string the string to be written to the file
     */
    public static void writeStringToFile(String fileName, String string) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(fileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            fileWriter.write(string + System.lineSeparator());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @param fileName the filename
     * @return the filewriter object
     * @throws IOException if the file cannot be created
     */
    public static FileWriter createFileWriter(String fileName) throws IOException{
        FileWriter fileWriter = null;
        fileWriter = new FileWriter(fileName);
        return fileWriter;
    }


    /**
     *
     * @param fileWriter the filewriter object
     * @param stringsList the list of strings to be written to the file
     */

    public static void writeStringsToFile(FileWriter fileWriter, List<String> stringsList) {
        for (String str : stringsList) {
            try {
                fileWriter.write(str + System.lineSeparator());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     *
     * @param dir the directory
     * @return a set of filenames in the directory
     */
    public static Set<String> listFilesInDirectory(String dir) {
        try (Stream<Path> stream = Files.list(Paths.get(dir))) {
            return stream
                    .filter(file -> !Files.isDirectory(file))
                    .map(Path::getFileName)
                    .map(Path::toString)
                    .collect(Collectors.toSet());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
