package com.axis.innovators.box.tools;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;

/**
 * 在当前jar下创建文件夹
 * @author tzdwindows 7
 */
public class FolderCreator {
    private static final Logger logger = LogManager.getLogger(FolderCreator.class);
    public static final String LIBRARY_NAME = "library";
    public static final String MODEL_PATH = "model";
    public static final String PLUGIN_PATH = "plug-in";

    public static String getPluginFolder() {
        String folder = createFolder(PLUGIN_PATH);
        if (folder == null) {
            logger.error("Plugin folder creation failed, please use administrator privileges to execute this procedure");
            return null;
        }
        return folder;
    }

    public static String getModelFolder() {
        String folder = createFolder(MODEL_PATH);
        if (folder == null) {
            logger.error("Model folder creation failure, please use administrator privileges to execute this procedure");
            return null;
        }
        return folder;
    }

    public static String getLibraryFolder() {
        String folder = createFolder(LIBRARY_NAME);
        if (folder == null) {
            logger.error("Library folder creation failed, please use administrator privileges to execute this procedure");
            return null;
        }
        return folder;
    }

    /**
     * 创建文件夹
     * @param folderName folder name
     * @return folder path
     */
    private static String createFolder(String folderName) {
        String jarDir = System.getProperty("user.dir");
        File folder = new File(jarDir, folderName);
        if (!folder.exists()) {
            if (!folder.mkdir()) {
                logger.error("Folder creation failure");
                return null;
            }
        }
        return folder.getAbsolutePath();
    }
}
