//

//  SandboxUtilities.swift

//  iOSSandboxing2

//

//  Created by coti on 15/2/17.

//  Copyright © 2017 coti. All rights reserved.

//  Last modified Feb 18 2017


import Foundation


public let dfm = FileManager.default


//MARK: Essential directories

/// Not usefuil in iOS ;)

public func homeURL() -> URL {

    return dfm.urls(for: FileManager.SearchPathDirectory.userDirectory,

                    in: FileManager.SearchPathDomainMask.userDomainMask).first!

    

}


public func tmpURL() -> URL {

    return dfm.temporaryDirectory

}


public func documentsURL() -> URL {

    return dfm.urls(for: FileManager.SearchPathDirectory.documentDirectory,

                    in: FileManager.SearchPathDomainMask.userDomainMask).first!

}


public func libraryURL() -> URL {

    return dfm.urls(for: FileManager.SearchPathDirectory.libraryDirectory,

                    in: FileManager.SearchPathDomainMask.userDomainMask).first!

    

}


//MARK: isItADirectory?

//

// Check whether a path or URL is directory or not

//

public func isDirectory(url: URL) -> Bool {

    var isDir: ObjCBool = false;

    dfm.fileExists(atPath: url.path, isDirectory: &isDir)

    return isDir.boolValue;

}


public func isDirectory(path: String) -> Bool {

    var isDir: ObjCBool = false;

    dfm.fileExists(atPath: path, isDirectory: &isDir)

    return isDir.boolValue;

}


//MARK: isItHidden?

//

// Simple test for hiddenness

//


public func isHidden(url : URL) -> Bool {

    return url.lastPathComponent.hasPrefix(".")

}


//

// Given the URL of a directory and an extension, this function produces a list

// of URLs with that extension.

//

// Case is not meaningful for extensions

//

//MARK: List files with a given extension (or all with *)

public  func listOfFilesIn(directory: URL, withExtension ext : String) -> [URL] {

    

    do {

        let contents = try dfm.contentsOfDirectory(at: directory,

                                                   includingPropertiesForKeys: nil ,

                                                   options: .skipsSubdirectoryDescendants)

        let justFilesNoDirs =  contents.filter() { !isDirectory(url: $0) }

        return ext == "*" ? justFilesNoDirs : justFilesNoDirs.filter() { $0.pathExtension == ext }

    } catch {

        print("\nCould not list files in " + directory.path)

        print("\(error)")

        return [URL]()

    }

}

//

// Given the URL of a directory and an extension, this function produces a list

// of URLs with that extension.

//

// Case is not meaningful for extensions

//

//MARK: List directories in a given one

public  func listOfDirectoriesIn(directory: URL) -> [URL] {

    

    do {

        let contents = try dfm.contentsOfDirectory(at: directory,

                                                   includingPropertiesForKeys: nil ,

                                                   options: .skipsSubdirectoryDescendants)

        return contents.filter() { isDirectory(url: $0) }

    } catch {

        print("\nCould not list directories in " + directory.path)

        print("\(error)")

        return [URL]()

    }

}


//

// Given a list of URLs, this funcion shows a prettyprinted list of any items in the list

//

//MARK: Show objects in a directory as a text

public func showContentsOf(list : [URL]) -> Void {

    

    for item in list where dfm.fileExists(atPath: item.path) {

        print(item.path.padding(toLength: 60,

                                withPad: " ",

                                startingAt: 0)

            + "is a"

            + (isHidden(url: item) ? " hidden" : "")

            + (isDirectory(url: item) ? " directory" : " file"))

    }

} // End of showContentsOf:


//

// Given a list of URLs, this funcion returns a prettyprinted string that containsany items in the list

//

// MARK: Give back objects in a directory as a String

public func stringWithPrettyPrintedURLsIn(list : [URL]) -> String {

    

    var result : String = ""

    for item in list where dfm.fileExists(atPath: item.path) {

        result += item.path.padding(toLength: 60,

                                    withPad: " ",

                                    startingAt: 0)

            + "is a"

            + (isHidden(url: item) ? " hidden" : "")

            + (isDirectory(url: item) ? " directory" : " file")

            + "\n"

        

    }

    return result

} // End of prettyPrintURLsIn