
#import "RCTConvert+AMap3D.h"

@implementation RCTConvert (AMapView)

RCT_ENUM_CONVERTER(MAPinAnnotationColor, (@{
    @"red": @(MAPinAnnotationColorRed),
    @"green": @(MAPinAnnotationColorGreen),
    @"purple": @(MAPinAnnotationColorPurple),
                                          }), MAPinAnnotationColorRed, integerValue)

RCT_ENUM_CONVERTER(MAUserTrackingMode, (@{
    @"none": @(MAUserTrackingModeNone),
    @"follow": @(MAUserTrackingModeFollow),
    @"followWithHeading": @(MAUserTrackingModeFollowWithHeading),
                                        }), MAUserTrackingModeNone, integerValue)

+ (MAMapType)MAMapType:(id)type {
    return [type integerValue];
}

+ (Coordinate *)Coordinate:(id)json {
    return [[Coordinate alloc] initWithCoordinate:[self CLLocationCoordinate2D:json]];
}

+(AMapNaviPoint*)AMapNaviPoint:(id)json {
    return [AMapNaviPoint locationWithLatitude:[self CGFloat:json[@"latitude"]] longitude:[self CGFloat:json[@"longitude"]]];
}

+(AMapNaviPOIInfo*)AMapNaviPOIInfo:(id)json {
    if(json == nil) {
        return nil;
    }
    AMapNaviPOIInfo* info = [AMapNaviPOIInfo new];
    info.mid = [self NSString:json[@"poiId"]];
    info.locPoint = [self AMapNaviPoint:json[@"coordinate"]];
    info.startAngle = [self double:json[@"direction"]];
    
    return info;
}

+ (LocationStyle *)LocationStyle:(id)json {
    LocationStyle *locationStyle = [LocationStyle new];
    locationStyle.fillColor = [self UIColor:json[@"fillColor"]];
    locationStyle.strokeColor = [self UIColor:json[@"strokeColor"]];
    locationStyle.strokeWidth = [self CGFloat:json[@"strokeWidth"]];
    locationStyle.locationDotBgColor = [self UIColor:json[@"locationDotBgColor"]];
    locationStyle.locationDotFillColor = [self UIColor:json[@"locationDotFillColor"]];
    locationStyle.image = [self UIImage:json[@"image"]];
    return locationStyle;
}

+ (MAHeatMapNode *)MAHeatMapNode:(id)json {
    MAHeatMapNode *node = [MAHeatMapNode new];
    node.coordinate = [self CLLocationCoordinate2D:json];
    node.intensity = 1;
    return node;
}

+ (MAMultiPointItem *)MAMultiPointItem:(id)json {
    MAMultiPointItem *item = [MAMultiPointItem new];
    item.coordinate = [self CLLocationCoordinate2D:json];
    return item;
}

+ (MACoordinateRegion)MACoordinateRegion:(id)json {
    return MACoordinateRegionMake(
                                  [self CLLocationCoordinate2D:json],
                                  MACoordinateSpanMake(
                                                       [self CLLocationDegrees:json[@"latitudeDelta"]],
                                                       [self CLLocationDegrees:json[@"longitudeDelta"]]));
}

+(NaviParams*)NaviParams:(id)json {
    if(![json isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    NaviParams* params = [NaviParams new];
    params.from = [RCTConvert NaviTargetArray:json[@"from"]];
    params.to = [RCTConvert NaviTargetArray:json[@"to"]];
    params.type = [RCTConvert NSInteger:json[@"type"]];
    params.strategy = [RCTConvert NSInteger:json[@"strategy"]];
    params.wayPoints = [RCTConvert NaviTargetArray:json[@"wayPoints"]];
    
    return params;
}

+(RideNaviParams*)RideNaviParams:(id)json {
    if(![json isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    RideNaviParams* params = [RideNaviParams new];
    params.from = [RCTConvert RideNaviTarget:json[@"from"]];
    params.to = [RCTConvert RideNaviTarget:json[@"to"]];
    params.strategy = [RCTConvert NSInteger:json[@"strategy"]];
    
    return params;
}

+(NSArray*) NaviTargetArray:(id)json {
    if(json == nil) {
        return nil;
    }
    if(![json isKindOfClass:[NSArray class]]) {
        return nil;
    }
    if([json count] == 0) {
        return nil;
    }
    if([json[0] isKindOfClass:[NSString class]]) {
        return [RCTConvert NSStringArray:json];
    }
    if([json[0] isKindOfClass:[NSDictionary class]]) {
        return [json[0] objectForKey:@"poiId"] ? [RCTConvert AMapNaviPOIInfoArray:json] : [RCTConvert AMapNaviPointArray:json];
    }
    return nil;
}

+(id)RideNaviTarget:(id)json {
    if(json == nil) {
        return nil;
    }
    return [json objectForKey:@"poiId"] ? [RCTConvert AMapNaviPOIInfo:json] : [RCTConvert AMapNaviPoint:json];
}

RCT_ARRAY_CONVERTER(Coordinate)
RCT_ARRAY_CONVERTER(MAHeatMapNode)
RCT_ARRAY_CONVERTER(MAMultiPointItem)
RCT_ARRAY_CONVERTER(AMapNaviPoint)
RCT_ARRAY_CONVERTER(AMapNaviPOIInfo)

@end
