
#import "RNIdreader.h"

#if __has_include(<React/RCTAssert.h>)
#import <React/RCTAssert.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTRootView.h>
#import <React/RCTUtils.h>
#import <React/RCTEventEmitter.h>
#else
#import "RCTAssert.h"
#import "RCTBridgeModule.h"
#import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import "RCTRootView.h"
#import "RCTUtils.h"
#import "RCTEventEmitter.h"
#endif

#import <CoreBluetooth/CoreBluetooth.h>
#import "IdreaderManager.h"

@implementation RNIdreader

const static NSString * BLUETOOTH_CALLBACK = @"BLUETOOTH_CALLBACK";
const static NSString * NFC_CALLBACK = @"NFC_CALLBACK";
const static NSString * SCANING = @"SCANING";
const static NSString * SCANED = @"SCANED";


RCT_EXPORT_METHOD(registerApp:(NSString *)appId :(NSString *)appSecret :(NSString *)appSecret_3des :(NSString *)nonce :(NSString *)businessExt)
{
    IdreaderManager * mgr = [IdreaderManager shareManager];
    mgr.appId = appId;
    mgr.appSecret = appSecret;
    mgr.appSecret_3des = appSecret_3des;
    mgr.nonce = nonce;
    mgr.businessExt = businessExt;

}

RCT_EXPORT_METHOD(settingBluetooth)
{
    [[IdreaderManager shareManager] settingBluetooth];
}

RCT_EXPORT_METHOD(isOpenBluetooth:(RCTResponseSenderBlock)callback)
{
    callback(@[@([[IdreaderManager shareManager] isOpenBluetooth])]);
}

RCT_EXPORT_METHOD(factoryName:(RCTResponseSenderBlock)callback)
{
    callback(@[[[IdreaderManager shareManager] factoryName]]);
}

RCT_EXPORT_METHOD(scanBluetooth)
{
    [[IdreaderManager shareManager] scan:^(NSArray * _Nonnull deviceList) {
        NSMutableArray *list = [NSMutableArray array];
        for (CBPeripheral *per in deviceList) {
            [list addObject:@{@"name": [NSString stringWithFormat:@"%@", per.name], @"id":[per.identifier UUIDString]}];
        }
        NSDictionary *body = @{@"status": @(true), @"type": SCANING, @"msg":@"获取设备列表成功", @"data":list};
        [self sendEventWithName:@"IDReaderEmitter" body:body];
    } error:^(NSString * _Nonnull error) {
        NSDictionary *body = @{@"status": @(false), @"type": SCANING, @"msg": error, @"data":[NSNull null]};
        [self sendEventWithName:@"IDReaderEmitter" body:body];
        
    }];
}

RCT_EXPORT_METHOD(stopScan)
{
    [[IdreaderManager shareManager] stopScan];
}

RCT_EXPORT_METHOD(readCard:(NSString *)bid :(RCTResponseSenderBlock)callback)
{
    [[IdreaderManager shareManager] read:bid callback:^(NSDictionary * _Nonnull result) {
        NSDictionary *body = @{@"status": @(true),@"type": BLUETOOTH_CALLBACK, @"data":result, @"msg": @"读取成功"};
        [self sendEventWithName:@"IDReaderEmitter" body:body];
    } error:^(NSString * _Nonnull error) {
        NSDictionary *body = @{@"status": @(false), @"type": BLUETOOTH_CALLBACK, @"msg": error, @"data":[NSNull null]};
        callback(@[body]);
    }];
}

- (NSArray<NSString *> *)supportedEvents
{
    return @[@"IDReaderEmitter"];
}

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

RCT_EXPORT_MODULE()

@end

