
@import AMapLocationKit;

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface AMapLocation : RCTEventEmitter <RCTBridgeModule, AMapLocationManagerDelegate>
@property(nonatomic, strong) AMapLocationManager* locationManager;
@end

@implementation AMapLocation

-(instancetype) init {
    if(self = [super init]) {
        self.locationManager = [[AMapLocationManager alloc] init];
        //设置不允许系统暂停定位
        [self.locationManager setPausesLocationUpdatesAutomatically:NO];
        //设置允许在后台定位
        [self.locationManager setAllowsBackgroundLocationUpdates:YES];
        //设置允许连续定位逆地理
        [self.locationManager setLocatingWithReGeocode:NO];
        self.locationManager.delegate = self;
    }
    return self;
}

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(start:(NSDictionary *)options) {
    //开始定位
    [self.locationManager startUpdatingLocation];
}

RCT_EXPORT_METHOD(stop) {
    [self.locationManager stopUpdatingLocation];
}

RCT_EXPORT_METHOD(enabledBackground: (BOOL) enabled) {
}
#pragma mark - AMapLocationManager Delegate

- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager *)locationManager
{
    [locationManager requestAlwaysAuthorization];
}

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    [self sendEventWithName:@"amap_location" body:@{
        @"error": @{
                @"code": @(error.code),
                @"info": error.localizedDescription == nil ? @"" : error.localizedDescription,
                @"details": error.localizedFailureReason == nil ? @"" : error.localizedFailureReason },
    }];
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    NSLog(@"lat:%F, log:%F", location.coordinate.latitude, location.coordinate.longitude);
    [self sendEventWithName:@"amap_location" body:@{
        @"error": @{@"code": @0, @"info": @"success", @"details": @"location success!" },
        @"type": @0,
        @"latitude": [NSNumber numberWithDouble:location.coordinate.latitude],
        @"longitude": [NSNumber numberWithDouble: location.coordinate.longitude],
        @"accuracy": [NSNumber numberWithDouble: location.verticalAccuracy],
        @"provider": @"",
        @"speed": [NSNumber numberWithDouble:location.speed],
        @"bearing": @-1,
        @"satellites": @-1,
        @"country": reGeocode != nil ? reGeocode.country : @"",
        @"city": reGeocode != nil ? reGeocode.city : @"",
        @"cityCode": reGeocode != nil ? reGeocode.citycode : @"",
        @"district": reGeocode != nil ? reGeocode.district : @"",
        @"address": reGeocode != nil ? reGeocode.formattedAddress : @"",
        @"poiName": reGeocode != nil ? reGeocode.POIName : @"",
        @"quality": @ {
            @"adviseMessage":@"",
            @"gpsSatellites": @-1,
            @"gpsStatus":@-1,
            @"netUseTime": @-1,
            @"networkType":@"",
            @"isInstalledHighDangerMockApp":@-1
        }
    }];
}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"amap_location"];
}
@end
