 
 import Foundation
 import CoreLocation
-import AddressBookUI
+import MapKit
 
 
 public class PGoLocationUtils {

            distanceMove = 1
        }
        
        return PGoCoordinate(latitude: startLatitude + ((endLatitude - startLatitude) * distanceMove), longitude: startLongitude + ((endLongitude - startLongitude) * distanceMove), altitude: 0, distance: maxDistance, displacement: distanceMove * maxDistance)
    }
    
    public func moveDistanceWithBearing(startLatitude:Double, startLongitude:Double, bearing: Double, distance: Double, bearingUnits: PGoLocationUtils.bearingUnits? = .Radian, unitOfDistance: PGoLocationUtils.unit? = .Meters) -> PGoLocationUtils.PGoCoordinate {

        let latitude = (asin(sin(lat) * cos(distanceRadian) + cos(lat) * sin(distanceRadian) * cos(bearingConverted))) * 180 / M_PI
        let longitude = (long + atan2(sin(bearingConverted) * sin(distanceRadian) * cos(lat), cos(distanceRadian) - sin(lat) * sin(latitude))) * 180 / M_PI
        
        return PGoCoordinate(
            latitude: latitude,
            longitude: longitude,
            altitude: nil,
            distance: nil,
            displacement: nil,
            address: nil,
            mapItem: nil
        )
    }
    
    private func getMKMapItem(lat: Double, long: Double) -> MKMapItem {
        let sourceLoc2D = CLLocationCoordinate2DMake(lat, long)
        let sourcePlacemark = MKPlacemark(coordinate: sourceLoc2D, addressDictionary: nil)
        let source = MKMapItem(placemark: sourcePlacemark)
        return source
    }
    
    public func getDirectionsFromToLocations(startLatitude:Double, startLongitude:Double, endLatitude:Double, endLongitude: Double, transportType: MKDirectionsTransportType? = .Walking, completionHandler: (PGoLocationUtils.PGoDirections?) -> ()) {
        
        /*
         
         Example func for completionHandler:
         func receivedDirections(result: PGoLocationUtils.PGoDirections?)
         
         */
        
        var result:Array<PGoCoordinate> = []
        
        let request: MKDirectionsRequest = MKDirectionsRequest()
        let start = getMKMapItem(startLatitude, long: startLongitude)
        let end = getMKMapItem(endLatitude, long: endLongitude)
        
        request.source = start
        request.destination = end
        request.requestsAlternateRoutes = true
        request.transportType = transportType!
        
        let directions = MKDirections(request: request)
        directions.calculateDirectionsWithCompletionHandler ({
            (response: MKDirectionsResponse?, error: NSError?) in
            if let routeResponse = response?.routes {
                let fastestRoute: MKRoute =
                    routeResponse.sort({$0.expectedTravelTime <
                        $1.expectedTravelTime})[0]
                
                for step in fastestRoute.steps {
                    result.append(
                        PGoCoordinate(
                            latitude: step.polyline.coordinate.latitude,
                            longitude: step.polyline.coordinate.longitude,
                            altitude: nil,
                            distance: step.distance,
                            displacement: nil,
                            address: nil,
                            mapItem: nil
                        )
                    )
                }
                completionHandler(
                    PGoLocationUtils.PGoDirections(
                        coordinates: result,
                        duration: fastestRoute.expectedTravelTime
                    )
                )
            } else {
                completionHandler(nil)
            }
        })
    }
}
