1//
2// Created by Leland Richardson on 12/27/15.
3// Copyright (c) 2015 Facebook. All rights reserved.
4//
5
6#import "RCTConvert+AirMap.h"
7
8#import <React/RCTConvert+CoreLocation.h>
9#import "AIRMapCoordinate.h"
10
11@implementation RCTConvert (AirMap)
12
13+ (MKCoordinateSpan)MKCoordinateSpan:(id)json
14{
15  json = [self NSDictionary:json];
16  return (MKCoordinateSpan){
17    [self CLLocationDegrees:json[@"latitudeDelta"]],
18    [self CLLocationDegrees:json[@"longitudeDelta"]]
19  };
20}
21
22+ (MKCoordinateRegion)MKCoordinateRegion:(id)json
23{
24  return (MKCoordinateRegion){
25    [self CLLocationCoordinate2D:json],
26    [self MKCoordinateSpan:json]
27  };
28}
29
30+ (MKMapCamera*)MKMapCamera:(id)json
31{
32    json = [self NSDictionary:json];
33    return [RCTConvert MKMapCameraWithDefaults:json existingCamera:nil];
34}
35
36+ (MKMapCamera*)MKMapCameraWithDefaults:(id)json existingCamera:(MKMapCamera*)camera
37{
38    json = [self NSDictionary:json];
39    if (camera == nil) {
40        camera = [[MKMapCamera alloc] init];
41    } else {
42        camera = [camera copy];
43    }
44    if (json[@"center"]) {
45        camera.centerCoordinate = [self CLLocationCoordinate2D:json[@"center"]];
46    }
47    if (json[@"pitch"]) {
48        camera.pitch = [self double:json[@"pitch"]];
49    }
50    if (json[@"altitude"]) {
51        camera.altitude = [self double:json[@"altitude"]];
52    }
53    if (json[@"heading"]) {
54        camera.heading = [self double:json[@"heading"]];
55    }
56    return camera;
57}
58
59
60RCT_ENUM_CONVERTER(MKMapType, (@{
61  @"standard": @(MKMapTypeStandard),
62  @"satellite": @(MKMapTypeSatellite),
63  @"hybrid": @(MKMapTypeHybrid),
64  @"satelliteFlyover": @(MKMapTypeSatelliteFlyover),
65  @"hybridFlyover": @(MKMapTypeHybridFlyover),
66  @"mutedStandard": @(MKMapTypeMutedStandard)
67}), MKMapTypeStandard, integerValue)
68
69// NOTE(lmr):
70// This is a bit of a hack, but I'm using this class to simply wrap
71// around a `CLLocationCoordinate2D`, since I was unable to figure out
72// how to handle an array of structs like CLLocationCoordinate2D. Would love
73// to get rid of this if someone can show me how...
74+ (AIRMapCoordinate *)AIRMapCoordinate:(id)json
75{
76    AIRMapCoordinate *coord = [AIRMapCoordinate new];
77    coord.coordinate = [self CLLocationCoordinate2D:json];
78    return coord;
79}
80
81RCT_ARRAY_CONVERTER(AIRMapCoordinate)
82
83+ (NSArray<NSArray<AIRMapCoordinate *> *> *)AIRMapCoordinateArrayArray:(id)json
84{
85    return RCTConvertArrayValue(@selector(AIRMapCoordinateArray:), json);
86}
87
88@end
89