//
//   DDMALocationManager.m
//   DDMAMapKit_Private
//   Created by DDIsFriend on 2023/2/24.
    

#import "DDMALocationManager.h"
#import <DDLogKit_Private/DDOCLog.h>

@interface DDMALocationManager ()<AMapLocationManagerDelegate>

@end

@implementation DDMALocationManager
// MARK: <Class Initialize>
+ (void)startWithAppKey:(nullable NSString *)appKey{
    [[AMapServices sharedServices] setEnableHTTPS:YES];
    [AMapServices sharedServices].apiKey = appKey;
}

+ (void)agreePrivacy{
    [AMapLocationManager updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];
    [AMapLocationManager updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
}

- (AMapLocationManager *)maLocationManager{
    if (_maLocationManager == nil) {
        _maLocationManager = [AMapLocationManager new];
        _maLocationManager.delegate = self;
    }
    return _maLocationManager;
}

// MARK: <AMapLocationManagerDelegate>
- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager*)locationManager{
    if ([self.delegate respondsToSelector:@selector(ddMALocationManager:doRequireLocationAuth:)]) {
        [self.delegate ddMALocationManager:manager doRequireLocationAuth:locationManager];
    }else{
        [locationManager requestWhenInUseAuthorization];
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager doRequireTemporaryFullAccuracyAuth:(CLLocationManager *)locationManager completion:(void (^)(NSError *))completion API_AVAILABLE(ios(14.0)){
    if (locationManager.accuracyAuthorization == CLAccuracyAuthorizationReducedAccuracy) {
        /// Every time you open the app and detect that the positioning accuracy is reduce, you need to apply for a temporary full accuracy.
        [self ddMALocationManager:locationManager requestTemporaryFullAccuracyAuthorizationWithCompletion:^(NSError * _Nullable error) {
            
        }];
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status API_DEPRECATED("",ios(11.0 , 13.0)){
    [self ddMACombineManager:manager locationManager:nil didChangeAuthorization:[CLLocationManager authorizationStatus] accuracyAuthorization:CLAccuracyAuthorizationFullAccuracy];
}

- (void)amapLocationManager:(AMapLocationManager *)manager locationManagerDidChangeAuthorization:(CLLocationManager*)locationManager API_AVAILABLE(ios(14.0)){
    CLAccuracyAuthorization accuracyAuthorization = CLAccuracyAuthorizationFullAccuracy;
    if (locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy) {
        accuracyAuthorization = CLAccuracyAuthorizationFullAccuracy;
    }else if (locationManager.accuracyAuthorization == CLAccuracyAuthorizationReducedAccuracy){
        accuracyAuthorization = CLAccuracyAuthorizationReducedAccuracy;
        /// When opening the app authorization location for the first time, a temporary promt is also required.
        [self ddMALocationManager:locationManager requestTemporaryFullAccuracyAuthorizationWithCompletion:^(NSError * _Nullable error) {
            
        }];
    }
    [self ddMACombineManager:manager locationManager:locationManager didChangeAuthorization:[locationManager authorizationStatus] accuracyAuthorization:accuracyAuthorization];
}

- (void)ddMALocationManager:(CLLocationManager *_Nullable)locationManager requestTemporaryFullAccuracyAuthorizationWithCompletion:(void(^)(NSError *_Nullable error))completion API_AVAILABLE(ios(14.0)) {
    /// This is async.
    [locationManager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"FORCE_FULL_ACCURACY" completion:^(NSError * _Nullable error) {
        // 授权定位下才能请求单次定位，不然会有性能问题
        BOOL canBeLocated = (locationManager.authorizationStatus == kCLAuthorizationStatusAuthorizedAlways || locationManager.authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse);
        
        if (canBeLocated && locationManager.accuracyAuthorization == CLAccuracyAuthorizationReducedAccuracy && self.forceFullAccuracy) {
            [self ddMALocationManager:locationManager requestTemporaryFullAccuracyAuthorizationWithCompletion:nil];
        }
    }];
}

- (void)ddMACombineManager:(AMapLocationManager *)manager locationManager:(CLLocationManager *_Nullable)locationManager didChangeAuthorization:(CLAuthorizationStatus)authorizationStatus accuracyAuthorization:(CLAccuracyAuthorization)accuracyAuthorization{
    switch (authorizationStatus) {
        case kCLAuthorizationStatusNotDetermined:
            DDLog("当前定位权限 - kCLAuthorizationStatusNotDetermined");
            break;
        case kCLAuthorizationStatusDenied:
            DDLog("当前定位权限 - kCLAuthorizationStatusDenied");
            break;
        case kCLAuthorizationStatusRestricted:
            DDLog("当前定位权限 - kCLAuthorizationStatusRestricted");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            DDLog("当前定位权限 - kCLAuthorizationStatusAuthorizedAlways");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            DDLog("当前定位权限 - kCLAuthorizationStatusAuthorizedWhenInUse");
            break;
        default:
            break;
    }
    switch (accuracyAuthorization) {
        case CLAccuracyAuthorizationFullAccuracy:
            DDLog("当前精度 - CLAccuracyAuthorizationFullAccuracy");
            break;
        case CLAccuracyAuthorizationReducedAccuracy:
            DDLog("当前精度 - CLAccuracyAuthorizationReducedAccuracy");
            break;
        default:
            break;
    }
    
    if ([self.delegate respondsToSelector:@selector(ddMALocationManager:locationManager:didChangeAuthorization:accuracyAuthorization:)]){
        [self.delegate ddMALocationManager:manager locationManager:locationManager didChangeAuthorization:authorizationStatus accuracyAuthorization:accuracyAuthorization];
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
    if ([self.delegate respondsToSelector:@selector(ddMALocationManager:didUpdateLocation:reGeocode:)]) {
        [self.delegate ddMALocationManager:manager didUpdateLocation:location reGeocode:reGeocode];
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error{
    if ([self.delegate respondsToSelector:@selector(ddMALocationManager:didFailWithError:)]) {
        [self.delegate ddMALocationManager:manager didFailWithError:error];
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateHeading:(CLHeading *)heading{
    if ([self.delegate respondsToSelector:@selector(ddMALocationManager:didUpdateHeading:)]) {
        [self.delegate ddMALocationManager:manager didUpdateHeading:heading];
    }
}
@end
