#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
   
    // Set account id
    AdView.shared.adviewAccountId = @"54649565";
    
    // Third-party id (deh) module — disabled by default.
//    AdView.shared.enableDEH = true;
    
    // Initialize AdViewSDK
    [AdView initializeSDKAndReturnError:nil
                             :^(enum ADVIEW_InitializationStatus status, NSError *error) {
        if (error) {
            NSLog(@"Initialization Error: %@", error.localizedDescription);
        }
    }];

//    [AdView.shared.deh setIdentityWithEmail:@"user@example.com" phone:nil];
    
    // Set sourceapp
    ADVIEW_Targeting.shared.sourceapp = @"AdViewDemoObjectiveC";
    
    // Initialize GoogleMobileAds SDK
    [[GADMobileAds sharedInstance] startWithCompletionHandler:^(GADInitializationStatus * _Nonnull status) {}];
    
    [AdViewAdMobUtils initializeGAD];
    
    // Initialize AppLovin MAX SDK
    ALSdkInitializationConfiguration *config = [ALSdkInitializationConfiguration
                                                    configurationWithSdkKey: @"86VQKXR4cOA8cTFPI7i1NI33iRltJt09BLceIIF1SKF7UHBKlcPomiv3dgJ46_lwJtzoJhTEtQyNEPe7xGVkZG"
                                                    builderBlock:^(ALSdkInitializationConfigurationBuilder *builder) {
        builder.mediationProvider = ALMediationProviderMAX;
    }];
    
    [[ALSdk shared] initializeWithConfiguration:config completionHandler:^(ALSdkConfiguration * _Nonnull configuration) {}];

    [self setupLocationManager];

    return YES;
}

#pragma mark - Location

- (void)setupLocationManager {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    self.locationManager.distanceFilter = 100;

    switch (self.locationManager.authorizationStatus) {
        case kCLAuthorizationStatusNotDetermined:
            [self.locationManager requestWhenInUseAuthorization];
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways:
            [self.locationManager startUpdatingLocation];
            break;
        default:
            break;
    }
}

- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
    CLAuthorizationStatus status = manager.authorizationStatus;
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
        [manager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = locations.lastObject;
    if (location == nil || !CLLocationCoordinate2DIsValid(location.coordinate)) {
        return;
    }

//    CLLocationCoordinate2D coord = location.coordinate;
//    [ADVIEW_Targeting.shared setLatitude:coord.latitude longitude:coord.longitude];
//    ADVIEW_Targeting.shared.location = location;
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Location error: %@", error.localizedDescription);
}


#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
}


@end
