//
//  GizGeofenceMethodSwizzling.m
//  GizwitsGeofence
//
//  Created by Gizwits on 2018/8/29.
//

#import "GizGeofenceMethodSwizzling.h"
#import "GizGeofenceRegionMonitor.h"
#import "GizGeofenceLogging.h"

#import <objc/runtime.h>

@implementation GizGeofenceMethodSwizzling

+ (void)load {
    
    // 替换掉 AppDelegate 的方法 application:didFinishLaunchingWithOptions:
    
    Class appDelegate = NSClassFromString(@"AppDelegate");
    
    if (!appDelegate) {
        return;
    }
    
    SEL newSel = @selector(gizgeofence_application:didFinishLaunchingWithOptions:);
    
    class_addMethod(appDelegate, newSel, class_getMethodImplementation([self class], newSel), "v@@");
    
    Method originalMethod = class_getInstanceMethod(appDelegate, @selector(application:didFinishLaunchingWithOptions:));
    Method swizzlMethod = class_getInstanceMethod(appDelegate, newSel);
    
    method_exchangeImplementations(originalMethod, swizzlMethod);
}

- (BOOL)gizgeofence_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    GeofenceLog(@"%s launchOptions => %@", __func__, launchOptions);
    
    [GizGeofenceRegionMonitor defaultMonitor];
    
    return [self gizgeofence_application:application didFinishLaunchingWithOptions:launchOptions];
}

@end
