//
//  DispatcherModule.m
//  testnpm
//
//  Created by Castiel on 2018/4/3.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "DispatcherModule.h"
#import "CustomNavigationController.h"
#import "BaseParamsConfig.h"
#import <React/RCTDefines.h>

@implementation DispatcherModule

RCT_EXPORT_MODULE(Navigation);

RCT_EXPORT_METHOD(push:(NSDictionary *) params) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        BaseParamsConfig *baseParamsConfig = [[BaseParamsConfig alloc] initWithDict:params];
        if ([baseParamsConfig.openType isEqualToString:OPENTYPE_H5]) {
            
        }
        else {
            //      NSURL* bundlePath = [[NSBundle mainBundle] URLForResource:@"bbb" withExtension:@"jsbundle"];
            NSURL *bundlePath = [NSURL URLWithString:[baseParamsConfig bundlePath]];
            BaseRNViewController *baseRNViewController = [[BaseRNViewController alloc] initWithBundlePath:bundlePath withModuleName:@"rnboat" initialProperties:[baseParamsConfig launchOptions]];
            
            NSAssert([self rootViewControllerIsCustomNavigationController], @"rootViewController不是CustomNavigationController");
            CustomNavigationController *navCtl = (CustomNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
            [navCtl pushViewController:baseRNViewController animated:YES];
        }
    });
}

RCT_EXPORT_METHOD(pop:(NSDictionary *) params) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        NSAssert([self rootViewControllerIsCustomNavigationController], @"rootViewController不是CustomNavigationController");
        CustomNavigationController *navCtl = (CustomNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        [navCtl popViewControllerAnimated:YES];
    });
}

RCT_EXPORT_METHOD(popToRoot:(NSDictionary *) params) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        NSAssert([self rootViewControllerIsCustomNavigationController], @"rootViewController不是CustomNavigationController");
        CustomNavigationController *navCtl = (CustomNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        [navCtl popToRootViewControllerAnimated:YES];
    });
}

RCT_EXPORT_METHOD(hideLoadingDialog) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        NSAssert([self rootViewControllerIsCustomNavigationController], @"rootViewController不是CustomNavigationController");
        CustomNavigationController *navCtl = (CustomNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        if ([navCtl.topViewController isKindOfClass:[BaseRNViewController class]]) {
            [((BaseRNViewController *) navCtl.topViewController) hideLoadingDialog];
        }
    });
}

RCT_EXPORT_METHOD(showLoadingDialog) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        NSAssert([self rootViewControllerIsCustomNavigationController], @"rootViewController不是CustomNavigationController");
        CustomNavigationController *navCtl = (CustomNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        if ([navCtl.topViewController isKindOfClass:[BaseRNViewController class]]) {
            [((BaseRNViewController *) navCtl.topViewController) showLoadingDialog];
        }
    });
}

RCT_EXPORT_METHOD(reLaunch:(NSDictionary *) params) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        BaseParamsConfig *baseParamsConfig = [[BaseParamsConfig alloc] initWithDict:params];
        if ([baseParamsConfig.openType isEqualToString:OPENTYPE_H5]) {
            
        }
        else {
            NSAssert([self rootViewControllerIsCustomNavigationController], @"rootViewController不是CustomNavigationController");
            CustomNavigationController *navCtl = (CustomNavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
            if ([navCtl.topViewController isKindOfClass:[BaseRNViewController class]]) {
//                NSURL* bundlePath = [[NSBundle mainBundle] URLForResource:@"bbb" withExtension:@"jsbundle"];
                NSURL *bundlePath = [NSURL URLWithString:[baseParamsConfig bundlePath]];
                [((BaseRNViewController *) navCtl.topViewController) reloadWithBundlePath:bundlePath initialProperties:nil];
            }
        }
    });
}

-(Boolean) rootViewControllerIsCustomNavigationController {
    return [[UIApplication sharedApplication].delegate.window.rootViewController isKindOfClass:[CustomNavigationController class]];
}
@end

