//
//  RCTBaiduNavModule.m
//  MoneyApp
//
//  Created by Run on 2017/11/21.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "RCTBaiduNavModule.h"

@implementation RCTBaiduNavModule

RCT_EXPORT_MODULE(BaiduNav);

RCT_EXPORT_METHOD(startNav:(NSDictionary *)startP end:(NSDictionary *)endPoint) {
  //节点数组
  NSMutableArray *nodesArray = [[NSMutableArray alloc]    initWithCapacity:2];
  double lat = [RCTConvert double:startP[@"latitude"]];
  double lng = [RCTConvert double:startP[@"longitude"]];
  double endLat = [RCTConvert double:endPoint[@"latitude"]];
  double endLng = [RCTConvert double:endPoint[@"longitude"]];
  
  //起点
  BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
  startNode.pos = [[BNPosition alloc] init];
  startNode.pos.x = lng;
  startNode.pos.y = lat;
  startNode.pos.eType = BNCoordinate_BaiduMapSDK;
  [nodesArray addObject:startNode];
  
  //终点
  BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
  endNode.pos = [[BNPosition alloc] init];
  endNode.pos.x = endLng;
  endNode.pos.y = endLat;
  endNode.pos.eType = BNCoordinate_BaiduMapSDK;
  [nodesArray addObject:endNode];
  //发起路径规划
  dispatch_async(dispatch_get_main_queue(), ^{
    [BNCoreServices_RoutePlan setDisableOpenUrl:YES];
    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
  });
}

/**
 *  算路成功回调
 */
- (void)routePlanDidFinished:(NSDictionary*)userInfo {
  NSLog(@"Finished----%@",userInfo);
//   [BNCoreServices_UI showNaviUI: BN_NaviTypeSimulator delegete:self isNeedLandscape:YES];

  //路径规划成功，开始导航
  [BNCoreServices_UI showPage:BNaviUI_NormalNavi delegate:self extParams:nil];
  
}

/**
 *  检索成功回调
 */
- (void)searchDidFinished:(NSDictionary*)userInfo {
  NSLog(@"search Finished----%@",userInfo);
}

/**
 *  算路失败回调
 */
- (void)routePlanDidFailedWithError:(NSError *)error andUserInfo:(NSDictionary*)userInfo {
  NSLog(@"error----%@",userInfo);
  NSString *errorMessage = @"算路失败";
  switch ([error code]%10000)
  {
    case BNAVI_ROUTEPLAN_ERROR_LOCATIONFAILED:
    {
      NSLog(@"暂时无法获取您的位置,请稍后重试");
      errorMessage = @"暂时无法获取您的位置,请稍后重试";
    }
      break;
    case BNAVI_ROUTEPLAN_ERROR_ROUTEPLANFAILED:
    {
      NSLog(@"无法发起导航");
      errorMessage = @"无法发起导航";
    }
      break;
    case BNAVI_ROUTEPLAN_ERROR_LOCATIONSERVICECLOSED:
    {
      NSLog(@"定位服务未开启,请到系统设置中打开定位服务。");
      errorMessage = @"定位服务未开启,请到系统设置中打开定位服务.";
    }
      break;
    case BNAVI_ROUTEPLAN_ERROR_NODESTOONEAR:
    {
      NSLog(@"起终点距离起终点太近");
      errorMessage = @"起终点距离起终点太近";
    }
      break;
    default:
      break;
  }
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:errorMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  [alert show];
}

#pragma mark - 安静退出导航

- (void)exitNaviUI
{
  [BNCoreServices_UI exitPage:EN_BNavi_ExitTopVC animated:YES extraInfo:nil];
}

#pragma mark - BNNaviUIManagerDelegate

//退出导航页面回调
- (void)onExitPage:(BNaviUIType)pageType  extraInfo:(NSDictionary*)extraInfo
{
  if (pageType == BNaviUI_NormalNavi)
  {
    NSLog(@"退出导航");
  }
  else if (pageType == BNaviUI_Declaration)
  {
    NSLog(@"退出导航声明页面");
  }
}


@end
