//
//   DDMANaviManager.m
//   DDMAMapKit_Private
//   Created by DDIsFriend on 2023/3/16.
    

#import "DDMANaviManager.h"
#import <AMapNaviKit/AMapNaviKit.h>
@interface DDMANaviManager ()
@property (nonatomic, strong)AMapNaviCompositeManager *compositeManager;
@end

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

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

+ (instancetype)shareManager{
    static DDMANaviManager *_Nullable _manager = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        _manager = [[self alloc] initWithSuper];
    });
    return _manager;
}

- (instancetype)initWithSuper{
    self = [super init];
    if (self == nil) return nil;
    
    return self;
}

- (instancetype)init{
    @throw [[NSException alloc] initWithName:NSGenericException reason:@"'init' is unavailable,please use 'shareManager' instead." userInfo:nil];
}

// MARK: <Func>

- (AMapNaviCompositeManager *)compositeManager{
    if (!_compositeManager) {
        _compositeManager = [[AMapNaviCompositeManager alloc] init];
    }
    return _compositeManager;
}

- (void)presentRoutePlanViewControllerFromCoordinate:(CLLocationCoordinate2D)fromCoordinate fromName:(nullable NSString *)fromName fromPOIId:(nullable NSString *)fromPOIId toCoordinate:(CLLocationCoordinate2D)toCoordinate toName:(nullable NSString *)toName toPOIId:(nullable NSString *)toPOIId {
    //导航组件配置类 since 5.2.0
    AMapNaviCompositeUserConfig *config = [[AMapNaviCompositeUserConfig alloc] init];
    //传入起点，并且带高德POIId
    [config setRoutePlanPOIType:AMapNaviRoutePlanPOITypeStart location:[AMapNaviPoint locationWithLatitude:fromCoordinate.latitude longitude:fromCoordinate.longitude] name:fromName POIId:fromPOIId];
    //传入终点，并且带高德POIId
    [config setRoutePlanPOIType:AMapNaviRoutePlanPOITypeEnd location:[AMapNaviPoint locationWithLatitude:toCoordinate.latitude longitude:toCoordinate.longitude] name:toName POIId:toPOIId];
    
    //启动
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.compositeManager presentRoutePlanViewControllerWithOptions:config];
    });
}
@end
