//
//   DDMAGeometry.m
//   DDMAMapKit_Private
//   Created by DDIsFriend on 2023/2/2.
    

#import "DDMAGeometry.h"
//#import <MAMapKit/MAPolyline.h>
//#import <AMapSearchKit/AMapSearchKit.h>
//#import <DDMAMapKit_Private/DDMASearch.h>

@implementation DDMAGeometry
+ (MAMapRect)getVisibleMapRectWithCoordinates:(CLLocationCoordinate2D *_Nullable)coords coordinateCount:(NSUInteger)coordinateCount{
    if (coords == NULL) return MAMapRectMake(0,0,0,0);

    MAMapPoint *mapPoints = new MAMapPoint[coordinateCount];

    for (NSUInteger i = 0; i < coordinateCount; i++) {
        mapPoints[i] = MAMapPointForCoordinate(coords[i]);
    }

    if (coordinateCount < 2) return MAMapRectMake(mapPoints[0].x,mapPoints[0].y,0,0);

    return [self getVisibleMapRectWithMapPoints:mapPoints mapPointCount:coordinateCount];
}

+ (MAMapRect)getVisibleMapRectWithMapPoints:(MAMapPoint *_Nullable)mapPoints mapPointCount:(NSUInteger)mapPointCount{
    if (mapPoints == NULL) return MAMapRectMake(0,0,0,0);
    if (mapPointCount < 2) return MAMapRectMake(mapPoints[0].x,mapPoints[0].y,0,0);
    
    double xMin = 0.0;
    double yMin = 0.0;
    double xMax = 0.0;
    double yMax = 0.0;
    
    MAMapPoint mapPoint = mapPoints[0];
    xMin = mapPoint.x;
    yMin = mapPoint.y;
    xMax = mapPoint.x;
    yMax = mapPoint.y;
    
    for (NSUInteger i = 1; i < mapPointCount; i++) {
        MAMapPoint point = mapPoints[i];
        if (xMin > point.x) {
            xMin = point.x;
        }
        if (yMin > point.y) {
            yMin = point.y;
        }
        if (xMax < point.x) {
            xMax = point.x;
        }
        if (yMax < point.y) {
            yMax = point.y;
        }
    }
    return MAMapRectMake(xMin, yMin, xMax - xMin, yMax - yMin);
}

+ (MAMapRect)getVisibleMapRectWithCLLocations:(NSArray<CLLocation *> *_Nullable)locations{
    if (locations == nil) return MAMapRectMake(0,0,0,0);
    if (locations.count == 0) return MAMapRectMake(0,0,0,0);
    CLLocationCoordinate2D *coords = new CLLocationCoordinate2D[locations.count];
    [locations enumerateObjectsUsingBlock:^(CLLocation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        coords[idx].latitude = obj.coordinate.latitude;
        coords[idx].longitude = obj.coordinate.longitude;
    }];
    return [self getVisibleMapRectWithCoordinates:coords coordinateCount:locations.count];
}

//+ (MAPolyline *)getPolylineWithRouteSearchResponse:(AMapRouteSearchResponse *)routeSearchResponse{
//    NSArray *array = [DDMASearch coordsArrayWithRouteSearchResponse:routeSearchResponse];
//    [array enumerateObjectsUsingBlock:^(DDMACoordinate  _Nonnull coordinate, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (idx == 0) {
//            CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)[(NSValue *)coordinate[DDMACoords] pointerValue];
//            NSUInteger count = [coordinate[DDMACoordsCount] integerValue];
//            MAPolyline *polyline = [MAPolyline polylineWithCoordinates:coords count:count];
//
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [mapView.maMapView setVisibleMapRect:[DDMAGeometry getVisibleMapRectWithCoordinates:coords coordinateCount:count] edgePadding:UIEdgeInsetsMake(50, 30, 50, 30) animated:YES];
//            });
//
//        }
//
//    }];
//}
@end
