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

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

@implementation AMapWalkPathView

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

-(void) calculateRoute {
    if(self.mapView == nil) {
        return;
    }

    AMapWalkingRouteSearchRequest *navi = [[AMapWalkingRouteSearchRequest 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 AMapWalkingRouteSearch: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 = MANaviAnnotationTypeWalking;
    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
