/**
 * @author      ETSI / STF481 / Yann Garcia
 * @version     $URL$
 *              $Id:
 */
package org.etsi.certificates;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public final class Helpers {


    /** 
     * A single instance of this class
     */
    private static Helpers instance;
    
    /** 
     * Synchronization object to provide single access to the instance
     */
    private static Object _sync = new Object();

    /**
     * Provide the access to a single instance of this class
     * @return    A single instance of this class
     */
    public static Helpers getInstance() {
        if (instance == null) {
            synchronized(_sync) {
                if (instance == null) {
                    instance = new Helpers();
                }
            }
        }

        return instance;
    }

    /**
     * Internal ctor
     */
    private Helpers() {
    }

    /**
     * Convert an array of strings to one string
     * @param list string list
     * @param separator 'separator' string between each element
     * @return string list items concatenated into a string
     */
    public String arrayToString(String[] list, String separator) {
        StringBuffer result = new StringBuffer();
        if (list.length > 0) {
            result.append(list[0]);
            for (int i = 1; i < list.length; i++) {
                result.append(separator);
                result.append(list[i]);
            }
        }
        
        return result.toString();
    }

    /**
     * Recursively walk a directory tree and return a List of all Files found; the List is sorted using File.compareTo().
     * @param startingDir is a valid directory, which can be read.
     */
    public List<File> getFileListing(File startingDir) throws FileNotFoundException {
        validateDirectory(startingDir);
        List<File> result = getFileListingNoSort(startingDir);
        Collections.sort(result);
        return result;
    }

    private List<File> getFileListingNoSort(File startingDir) throws FileNotFoundException {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = startingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
        
        for (File file : filesDirs) {
            if (file.isDirectory() && ((file.getName().indexOf(".svn") != -1) 
                                        || (file.getName().indexOf("._.DS_Store") != -1)
                                        || (file.getName().indexOf(".DS_Store") != -1))) { // For debug purpose only
                // Skip svn files
                continue;
            }
            result.add(file); // always add, even if directory
            if (!file.isFile()) {
                //must be a directory
                //recursive call!
                List<File> deeperList = getFileListingNoSort(file);
                result.addAll(deeperList);
            }
        }
        return result;
    }
    
    /**
     * Directory is valid if it exists, does not represent a file, and can be read.
     */
    private void validateDirectory (File aDirectory) throws FileNotFoundException {
        if (aDirectory == null) {
            throw new IllegalArgumentException("Directory should not be null.");
        }
        if (!aDirectory.exists()) {
            throw new FileNotFoundException("Directory does not exist: " + aDirectory);
        }
        if (!aDirectory.isDirectory()) {
            throw new IllegalArgumentException("Is not a directory: " + aDirectory);
        }
        if (!aDirectory.canRead()) {
            throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
        }
    } 
    
} // End of class Helpers
