//
//  Scan.m
//  Scan
//
//  Created by I Mac on 17/3/29.
//  Copyright © 2017年 YF. All rights reserved.
//

#import "Scan.h"
#import "ScanVc.h"
#import <React/RCTLog.h>
#import <React/RCTUtils.h>

@interface Scan(){
    NSString *scanCode_;
    NSString *code_;
}
@property(nonatomic,strong)RCTPromiseResolveBlock resove_self;
@property(nonatomic,strong)ScanVc *scanVC;

@end

@implementation Scan
RCT_EXPORT_MODULE();

RCT_REMAP_METHOD(scan,
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject){
    self.resove_self = resolve;
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(info:) name:@"scan" object:nil];
    self.scanVC = [[ScanVc alloc] init];
    dispatch_async(dispatch_get_main_queue(), ^{
        UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
        while (root.presentedViewController != nil) {
            root = root.presentedViewController;
        }
        [root presentViewController:self.scanVC animated:YES completion:nil];
    });
}
- (void)info:(NSNotification *)info{
    scanCode_ = info.userInfo[@"content"];
    if (self.resove_self) {
        self.resove_self(@{@"barCode":scanCode_});
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}
@end

