//
//  util.m
//  arena
//  工具相关
//  Created by 张琳 on 2017/10/28.
//  Copyright © 2017年 江苏斑马软件技术有限公司. All rights reserved.
//

#import "util.h"

@implementation util

/* 导出模块 */
RCT_EXPORT_MODULE(util);



/// 扫描条形码、二维码
RCT_EXPORT_METHOD(scan:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.biz.util.scan" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    //主要这里必须使用主线程发送,不然有可能失效
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam]; //把收到的RN的参数用通知发送出去
    });
}


//拍照
RCT_EXPORT_METHOD(photo:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.biz.util.photo" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    //主要这里必须使用主线程发送,不然有可能失效
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam]; //把收到的RN的参数用通知发送出去
    });
}

@end

