//
//  AMapRidePathView.m
//  react-native-anavi
//
//  Created by xiuyanger on 2020/9/15.
//

#import "AMapRidePathView.h"
#import "AMapUtility.h"
#import "UIImage+AMap3D.h"

@implementation AMapRidePathView

-(instancetype) init {
    if(self = [super init]) {
        self.pathWidth = 10;
        self.pathColor = [UIColor blueColor];
        self.nodeVisible = YES;
        self.startMarkerIcon = [UIImage amap_imageNamed: @"start"];
        self.endMarkerIcon = [UIImage amap_imageNamed:@"end"];
        self.nodeMarkerIcon = [UIImage amap_imageNamed:@"ride"];
        self.pathPadding = 20;
        self.startMarkerTitle = @"起点";
        self.endMarkerTitle = @"终点";
    }
    return self;
}

-(void) calculateRoute {
    if(self.mapView == nil) {
        return;
    }
    AMapRidingRouteSearchRequest *navi = [[AMapRidingRouteSearchRequest alloc] init];
    
    /* 出发点. */
    navi.origin = [AMapGeoPoint locationWithLatitude:self.startPoint.latitude
                                           longitude:self.startPoint.longitude];
    /* 目的地. */
    navi.destination = [AMapGeoPoint locationWithLatitude:self.endPoint.latitude
                                                longitude:self.endPoint.longitude];
    
    [self.search AMapRidingRouteSearch:navi];
}

-(void)presentRoute {
    if(!self.drawPath) {
        return;
    }
    if(self.mapView == nil) {
        return;
    }
    if(self.route == nil || self.route.paths == nil || self.route.paths.count == 0) {
        return;
    }
    [super presentRoute];
    
    AMapPathAnnotationType type = MANaviAnnotationTypeRiding; //骑行类型
    
    AMapGeoPoint *startPoint = [AMapGeoPoint locationWithLatitude:self.startPoint.latitude longitude:self.startPoint.longitude]; //起点
    
    AMapGeoPoint *endPoint = [AMapGeoPoint locationWithLatitude:self.endPoint.latitude longitude:self.endPoint.longitude];  //终点
    
    //根据已经规划的路径，起点，终点，规划类型，是否显示实时路况，生成显示方案
    self.pathRoute = [AMapPathRoute routeForPath:self.route.paths[0] withNaviType:type showTraffic:NO startPoint:startPoint endPoint:endPoint];
    self.pathRoute.pathWidth = self.pathWidth;
    self.pathRoute.routeColor = self.pathColor;
    self.pathRoute.nodeMarkerIcon = self.nodeMarkerIcon;
    
    [self.pathRoute addToMapView:self.mapView];  //显示到地图上
    
    UIEdgeInsets edgePaddingRect = UIEdgeInsetsMake(self.pathPadding, self.pathPadding, self.pathPadding, self.pathPadding);
    
    //缩放地图使其适应polylines的展示
    [self.mapView setVisibleMapRect:[AMapUtility mapRectForOverlays:self.pathRoute.routePolylines]
                        edgePadding:edgePaddingRect
                           animated:YES];
}

@end
