//

//  Utilities-iOS.swift

//  No specific project, used throuhgout

// This version last modified on Feb 24. 2017

//  This iOS version created on March 14 2017


import UIKit


public let dfm = FileManager.default


func notifyUser(_ controller:UIViewController,

                alertTitle title:String,

                alertMessage:String,

                ok:@escaping (UIAlertAction) -> Void) {

    

    let alertViewController = UIAlertController(title: title, message: alertMessage, preferredStyle: .alert)

    

    let okAction = UIAlertAction(title: "YES", style: .default, handler: ok)

    

    alertViewController.addAction(okAction)

    

    controller.present(alertViewController, animated: true, completion: nil)

}


//func doSomething(alert:UIAlertAction) {

//    // Put this function in the class that calls notifyUser()

//    // Do whatever is needed

//}


public func userConfirmsAction(_ controller:UIViewController,

                               alertTitle title:String,

                               alertMessage:String,

                               runOnConfirm: @escaping (UIAlertAction) -> () ) {

    

    let okOrCancelAlert = UIAlertController(title:title,

                                            message: alertMessage,

                                            preferredStyle: UIAlertControllerStyle.alert)

    

    okOrCancelAlert.addAction(UIAlertAction(title: "OK",

                                            style: .destructive,

                                            handler: runOnConfirm ))

    

    okOrCancelAlert.addAction(UIAlertAction(title: "CANCEL",

                                            style: .cancel,

                                            handler: { (action: UIAlertAction!) in

    }))

    

    present(okOrCancelAlert, animated: true, completion: nil)

    

}


//func doSomething(alert:UIAlertAction) {

//    // Put this function in the class that calls notifyUser()

//    // Do whatever is needed

//}


public func homeURL() -> URL {

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

                    in: FileManager.SearchPathDomainMask.userDomainMask).first!

    

}


public func homePath() -> String {

    return homeURL().path

}


public func tmpURL() -> URL {

    if #available(iOS 10.0, *) {

        return dfm.temporaryDirectory

    } else {

        // Fallback on earlier versions

        return URL(string: NSTemporaryDirectory())!

    }

}


public func tmpPath() -> String{

    return tmpURL().path

}


public func documentsURL() -> URL {

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

                    in: FileManager.SearchPathDomainMask.userDomainMask).first!

}


public func documentsPath() -> String {

    return documentsURL().path

}


public func libraryURL() -> URL {

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

                    in: FileManager.SearchPathDomainMask.userDomainMask).first!

    

}


public func libraryPath() -> String {

    return libraryURL().path

}


public func splitIntoNameAndExtension(total:String) -> [String]? {

    

    let parts = total.components(separatedBy: ".")

    

    guard

        parts.count == 2,

        let name = parts.first,

        !name.isEmpty,

        let ext = parts.last,

        !ext.isEmpty

        else

    { return nil }

    

    return [name, ext]

}