//
//  RNThirdNavigationManager.m
//  RNThirdNavigationManager
//
//  Created by LvBingru on 10/22/15.
//  Copyright © 2015 erica. All rights reserved.
//

#import "RNThirdNavigationManager.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <React/RCTConvert.h>

@implementation RNThirdNavigationManager{
}


RCT_EXPORT_MODULE(NavigationManager)

RCT_EXPORT_METHOD(openNavi:(NSDictionary *)source target:(NSDictionary *)target)
{
    double _endLatitude = [RCTConvert double:target[@"latitude"]] ?: .0;
    double _endLongitude = [RCTConvert double:target[@"longitude"]] ?: .0;
    NSString* _endName = [RCTConvert NSString:target[@"name"]] ?: @"";
    
    MKMapItem *sLocation;
    if (source == nil) {
        sLocation = [MKMapItem mapItemForCurrentLocation];
    }
    else {
        double _startLatitude = [RCTConvert double:source[@"latitude"]] ?: .0;
        double _startLongitude = [RCTConvert double:source[@"longitude"]] ?: .0;
        NSString* _startName = [RCTConvert NSString:source[@"name"]] ?: @"";
        
        sLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(_startLatitude, _startLongitude) addressDictionary:nil]];
        sLocation.name = _startName;
    }
    MKMapItem *dLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(_endLatitude, _endLongitude) addressDictionary:nil]];
    dLocation.name = _endName;
    
    [MKMapItem openMapsWithItems:@[sLocation, dLocation]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}

@end
