 import CoreLocation
 import MapKit
 
+// enum to hold possible navigation apps on user's device
 public enum PazNavigationApp {
     case AppleMaps
     case GoogleMaps

        case .Waze:
            return "waze://"
        }
        
    }
    
    public var url: URL? {
        return URL(string: self.urlString)
    }
    
    public var available: Bool {
        if self == .AppleMaps {
            return true
        }
        guard let url = self.url else {
            return false
        }
        return UIApplication.shared.canOpenURL(url)
    }
    
    public func directionsUrlString(coordinate: CLLocationCoordinate2D, name: String = "Destination") -> String {
        var urlString = self.urlString
        switch self {
        case .AppleMaps:
            urlString.append("?q=\(coordinate.latitude),\(coordinate.longitude)=d&t=h")
        case .GoogleMaps:
            urlString.append("?saddr=&daddr=\(coordinate.latitude),\(coordinate.longitude)&directionsmode=driving")
        case .Navigon:
            urlString.append("coordinate/\(name)/\(coordinate.longitude)/\(coordinate.latitude)")
        case .TomTom:
            urlString.append("geo:action=navigateto&lat=\(coordinate.latitude)&long=\(coordinate.longitude)&name=\(name)")
        case .Waze:
            urlString.append("?ll=\(coordinate.latitude),\(coordinate.longitude)&navigate=yes")
        }
        let urlwithPercentEscapes = urlString.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed) ?? urlString

        return urlwithPercentEscapes
    }

    public func directionsUrl(coordinate: CLLocationCoordinate2D, name: String = "Destination") -> URL? {
        let urlString = self.directionsUrlString(coordinate: coordinate, name: name)
        return URL(string: urlString)
    }
    
    public func openWithDirections(coordinate: CLLocationCoordinate2D, name: String = "Destination", completion: ((Bool) -> Swift.Void)? = nil) {
        if self == .AppleMaps {
            let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
            mapItem.name = self.name
            let success = mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
            completion?(success)
        }
        guard let url = self.directionsUrl(coordinate: coordinate, name: name) else {
            completion?(false)
            return
        }
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: {
                (success) in

        }
    }
    
    /* func to create an Alert where the options
     are the available navigation apps on the user's device.
     The callback informs if the operation was successful */
    public static func directionsAlertController(coordinate: CLLocationCoordinate2D,
                                                 name: String = "Destination",
                                                 title: String = "Directions Using",
                                                 message: String? = nil,
                                                 completion: ((Bool) -> ())? = nil)
        -> UIAlertController {
            
            let directionsAlertView = UIAlertController(title: title,
                                                        message: nil,
                                                        preferredStyle: .actionSheet)
            
            for navigationApp in PazNavigationApp.AvailableServices {
                
                let action = UIAlertAction(title: navigationApp.name,
                                           style: UIAlertActionStyle.default,
                                           handler: { action in
                                            navigationApp.openWithDirections(coordinate: coordinate,
                                                                             name: name,
                                                                             completion: completion)
                })
                
                directionsAlertView.addAction(action)
            }
            
            let cancelAction = UIAlertAction(title: "Dismiss",
                                             style: UIAlertActionStyle.cancel,
                                             handler: { action in completion?(false) })
            
            directionsAlertView.addAction(cancelAction)
            
            return directionsAlertView
    }
}
