/**
 * *****************************************************************************
 * Copyright (c) 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************
 */
package com.exalttech.trex.util.files;

import com.exalttech.trex.util.PreferencesManager;
import com.exalttech.trex.util.Util;
import java.io.File;
import java.io.IOException;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 * File manager utility class
 *
 * @author GeorgeKh
 */
public class FileManager {

    private static final Logger LOG = Logger.getLogger(FileManager.class.getName());
    private static final String APP_DATA_PATH = File.separator + "TRex" + File.separator + "trex" + File.separator;
    private static final String PROFILES_PATH = "traffic-profiles" + File.separator;

    /**
     * Return local file path
     *
     * @return
     */
    public static String getLocalFilePath() {

        if (Util.isWindows()) {
            if (!Util.isNullOrEmpty(System.getenv("LOCALAPPDATA"))) {
                String systemPath = System.getenv("LOCALAPPDATA") + APP_DATA_PATH;
                LOG.info("Loading System Path =" + systemPath);
                return systemPath;
            }
            return "";
        }
        return "";
    }

    /**
     * Return The profiles file path
     *
     * @return
     */
    public static String getProfilesFilePath() {
        if (Util.isWindows()) {
            createDirectoryIfNotExists(getLocalFilePath() + PROFILES_PATH);
            return getLocalFilePath() + PROFILES_PATH;
        }
        return "generatedFiles/";
    }

    /**
     * Copy the select file to the local directory
     *
     * @param srcFile
     * @return
     * @throws IOException
     */
    public static File copyFile(File srcFile) throws IOException {
        File destFile;
        String srcFileDirectory = "";
        if (srcFile.getParentFile() != null) {
            srcFileDirectory = srcFile.getParentFile().getName();
        }
        destFile = new File(createDirectoryIfNotExists(getLocalFilePath() + srcFileDirectory) + File.separator + srcFile.getName());
        FileUtils.copyFile(srcFile, destFile);
        return destFile;
    }

    /**
     * Create directory if not exists
     *
     * @param directoryPath
     * @return
     */
    public static String createDirectoryIfNotExists(String directoryPath) {
        File dir = new File(directoryPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return directoryPath;
    }

    /**
     *
     * @param path
     * @return
     */
    public static boolean isExists(String path) {
        File file = new File(path);
        return file.exists();
    }

    /**
     * Delete file from local directory
     *
     * @param fileToDelete
     */
    public static void deleteFile(File fileToDelete) {
        if (fileToDelete != null) {
            FileUtils.deleteQuietly(fileToDelete);
        }
    }

    /**
     * Create new file
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static File createNewFile(String fileName) throws IOException {
        File newFile = new File(getProfilesFilePath() + fileName);
        newFile.createNewFile();
        return newFile;
    }

    /**
     * Write String to file
     *
     * @param data
     * @param file
     * @throws IOException
     */
    public static void writeStringToFile(String data, File file) throws IOException {
        FileUtils.writeStringToFile(file, data);
    }

    /**
     * Export file
     *
     * @param windowTitle
     * @param fileName
     * @param fileContent
     * @param window
     * @param type
     * @throws IOException
     */
    public static void exportFile(String windowTitle, String fileName, Object fileContent, Window window, FileType type) throws IOException {
        File savedFile = getSelectedFile(windowTitle, fileName, window, type, PreferencesManager.getInstance().getSaveLocation(), true);
        if (savedFile != null) {
            String filePath = savedFile.getAbsolutePath();
            String pathToSave = !filePath.contains(type.getExtension()) ? filePath + type.getExtension() : filePath;
            File fileToSave = new File(pathToSave);
            if (!fileToSave.exists()) {
                fileToSave.createNewFile();
            }
            if (fileContent instanceof String) {
                FileUtils.writeStringToFile(fileToSave, String.valueOf(fileContent));
            } else {
                FileUtils.copyFile((File) fileContent, savedFile);
            }
        }
    }

    /**
     * Return selected file
     *
     * @param windowTitle
     * @param fileName
     * @param window
     * @param type
     * @param filePath
     * @param isExport
     * @return
     */
    public static File getSelectedFile(String windowTitle, String fileName, Window window, FileType type, String filePath, boolean isExport) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle(windowTitle);

        fileChooser.setInitialFileName(fileName);
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(type.getFilterDescription(), type.getFilterExtension());
        fileChooser.getExtensionFilters().add(extFilter);

        if (!Util.isNullOrEmpty(filePath) && new File(filePath).exists()) {
            fileChooser.setInitialDirectory(new File(filePath));
        }
        if (isExport) {
            return fileChooser.showSaveDialog(window);
        } else {
            return fileChooser.showOpenDialog(window);
        }
    }

    /**
     *
     */
    private FileManager() {
        // private constructor
    }

}

@@ -2212,23 +2212,8 @@
 rn %22
-generatedFiles/
 %22;%0A 

Fix path for linux