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

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

@implementation AMapDrivePathView

-(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:@"car"];
        self.pathPadding = 20;
        self.startMarkerTitle = @"起点";
        self.endMarkerTitle = @"终点";
    }
    return self;
}

-(void) calculateRoute {
    AMapDrivingRouteSearchRequest *navi = [[AMapDrivingRouteSearchRequest alloc] init];
        
        navi.requireExtension = YES;
        /* 策略 */
        navi.strategy = 10;
        /* 出发点. */
        navi.origin = [AMapGeoPoint locationWithLatitude:self.startPoint.latitude
                                               longitude:self.startPoint.longitude];
        /* 目的地. */
        navi.destination = [AMapGeoPoint locationWithLatitude:self.endPoint.latitude
                                                    longitude:self.endPoint.longitude];
        
        [self.search AMapDrivingRouteSearch: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 = MANaviAnnotationTypeDrive;
    self.pathRoute = [AMapPathRoute routeForPath:self.route.paths[0] withNaviType:type showTraffic:YES startPoint:[AMapGeoPoint locationWithLatitude:self.startPoint.latitude longitude:self.startPoint.longitude] endPoint:[AMapGeoPoint locationWithLatitude:self.endPoint.latitude longitude:self.endPoint.longitude]];
    
    self.pathRoute.pathWidth = self.pathWidth;
    self.pathRoute.routeColor = self.pathColor;
    self.pathRoute.nodeMarkerIcon = self.nodeMarkerIcon;
    
    [self.pathRoute addToMapView:self.mapView];
    
    /* 缩放地图使其适应polylines的展示. */
    [self.mapView setVisibleMapRect:[AMapUtility mapRectForOverlays:self.pathRoute.routePolylines]
                        edgePadding:UIEdgeInsetsMake(self.pathPadding, self.pathPadding, self.pathPadding, self.pathPadding)
                           animated:YES];
}
@end
