package com.capgemini.framework.common;

import lombok.extern.slf4j.Slf4j;

import java.io.File;

/** Class for folder management. **/
@Slf4j
public final class FolderHelper {

    /** Constructor. **/
    private FolderHelper() {
        // Constructor of helper classes must be private
    }

    /**
     * Check directory existence.
     * 
     * @param aDirPath Directory which must be checked
     * @return True when the file exists
     */
    private static boolean dirExist(final String aDirPath) {
        return dirExist(aDirPath, true);
    }

    /**
     * Check directory existence with logger option.
     * 
     * @param isVerbose True for verbose mode, else the silent mode is used
     * @param aDirPath Directory which must be checked
     * @return True when the file exists
     */
    private static boolean dirExist(final String aDirPath, final boolean isVerbose) {

        boolean status = true;

        if (aDirPath == null) {
            if (isVerbose) {
                log.debug("Directory name is null");
            }
            return false;
        }

        File theDir = new File(aDirPath);
        if (!theDir.exists() || theDir.isFile()) {
            if (isVerbose) {
                log.debug("Directory does not exist: " + theDir.getAbsolutePath());
            }
            status = false;
        }

        return status;
    }

    /**
     * Delete all files and sub-directories from a directory.
     * 
     * @param aDirectoryPath Path of the directory
     * @return True if delete operation is successful
     */
    public static boolean delete(final String aDirectoryPath) {

        boolean isSuccess = true;

        /* Check if folder has not been already deleted */
        if (dirExist(aDirectoryPath)) {

            File theMainDirectory = new File(aDirectoryPath);

            String[] theFileEntries = theMainDirectory.list();
            for (String currentFileEntry : theFileEntries) {
                File currentFile = new File(theMainDirectory.getPath(), currentFileEntry);
                if (currentFile.isDirectory()) {
                    isSuccess = isSuccess && delete(currentFile.getPath());
                    isSuccess = isSuccess && currentFile.delete();
                }
                else {
                    isSuccess = isSuccess && currentFile.delete();
                }
            }
        }

        return isSuccess;
    }

    /**
     * Create all directories if needed, return true when they already exist.
     * 
     * @param aDirectory path for created directory
     * @return false when create operation has failed
     */
    public static boolean mkdir(final File aDirectory) {

        boolean status = true;

        if (!aDirectory.isDirectory()) {

            /* Recursive creation if needed */
            File theParentDirectory = aDirectory.getParentFile();
            if ((theParentDirectory != null) && !theParentDirectory.isDirectory()) {
                status = mkdir(theParentDirectory);
            }

            status &= aDirectory.mkdirs();
            if (!status) {
                log.error("Unable to create parent directory: " + aDirectory.getAbsolutePath());
            }
        }

        return status;
    }
}
