//
//  RNSanModule.m
//  RNBTDevice
//
//  Created by lvwang2002 on 2019/5/13.
//  Copyright © 2019 zmxv. All rights reserved.
//

#import "RNSanModule.h"

@implementation RNSanModule
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;

-(instancetype)init{
    self = [super init];
    if(self){
        
        
    }
    return self;
}

RCT_EXPORT_METHOD(connectDynamicGlucose:(NSString *)deviceMac){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    device.deviceDelegate = self;
    device.nativeDevice.deviceDelegate = self;
    [device connect];
    
    NSArray *devices = [manager getSearchedDevices];
    for(BLEDevice *myDevice in devices){
        NSString *myDeviceUUID = [[myDevice getUUID] UUIDString];


        if([myDeviceUUID isEqualToString:deviceMac]){
            continue;
        }

        if((myDevice.connectStatus != DeviceConnectingStatus) &&
           (myDevice.connectStatus != DeviceConnectedStatus)){
            continue;
        }

        dispatch_async([manager getPostQueue], ^{
            [myDevice disconnect];
        });
    }
}

RCT_EXPORT_METHOD(didConnectedDevice:(BLEDevice *)device){
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc"
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"CONNECTED_BT_DEVICE"                                                    body:body];
    
}



-(void)didDisconnectDevice:(BLEDevice *)device{
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatus":@"disconnect"
                          };
    //    [self sendEventWithName:@"DISCONNECTED_BT_DEVICE" body:body];
    [self.bridge.eventDispatcher sendAppEventWithName:@"DISCONNECT_BT_DEVICE"                                                    body:body];
}


-(void)didOverTimeForConnectDevice:(BLEDevice *)device{
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatus":@"connect time over"
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
}

RCT_EXPORT_METHOD(doAuthorizationDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice doAuthorization];
}

RCT_EXPORT_METHOD(startMonitorDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice startMonitor];
}

RCT_EXPORT_METHOD(stopMonitorDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice stopMonitor];
}


RCT_EXPORT_METHOD(disconnecAllDYGDevicetWithout:(NSString *)deviceMac){
        ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
        NSArray *devices = [manager getSearchedDevices];
        for(BLEDevice *myDevice in devices){
            NSString *myDeviceUUID = [[myDevice getUUID] UUIDString];
    
    
            if([myDeviceUUID isEqualToString:deviceMac]){
                continue;
            }
            
            if([[myDevice getDeviceName] hasPrefix:@"TH-"]){
                continue;
            }
            
            dispatch_async([manager getPostQueue], ^{
                [myDevice disconnect];
            });
        }
}

//RCT_EXPORT_METHOD(stopMonitorDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
//    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
//    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
//    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
//    if(!device){
//        NSDictionary *body = @{
//                               @"deviceName":@"no device",
//                               @"deviceMac":deviceMac,
//                               @"deviceStatusDescription":@"no device",
//                               @"deviceStatus":@"error"
//                               };
//        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
//        return;
//    }
//
//    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
//
//
//    [nativeDevice stopMonitor];
//}

//RCT_EXPORT_METHOD(doAuthorizationDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
//    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
//    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
//    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
//    if(!device){
//        NSDictionary *body = @{
//                               @"deviceName":@"no device",
//                               @"deviceMac":deviceMac,
//                               @"deviceStatusDescription":@"no device",
//                               @"deviceStatus":@"error"
//                               };
//        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
//        return;
//    }
//    
//    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
//    
//    
//    [nativeDevice doAuthorization];
//}

RCT_EXPORT_METHOD(getMonitorStatusDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getMonitorStatus];
}

RCT_EXPORT_METHOD(getNameDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getName];
}

RCT_EXPORT_METHOD(getVersionDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getVersion];
}

RCT_EXPORT_METHOD(getBatteryDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getBattery];
}

RCT_EXPORT_METHOD(getAllDataCountDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getAllDataCount];
}

RCT_EXPORT_METHOD(getAllDataDYG:(NSString *)deviceMac RequestCode:(int)requestCode){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getAllData];
}


RCT_EXPORT_METHOD(getNumberDataFromIndexDYG:(NSString *)deviceMac
                  RequestCode:(int)requestCode
                  Index:(int)index
                  Count:(int)count){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getNumberDataFromIndex:index Count:count];
}

RCT_EXPORT_METHOD(getAllDataFromIndexDYG:(NSString *)deviceMac RequestCode:(int)requestCode Index:(int)index){
    ENBLEDeviceManager *manager = [ENBLEDeviceManager shareDeviceManager];
    NSUUID *myUUID = [[NSUUID alloc] initWithUUIDString:deviceMac];
    BLEDevice *device = [manager getDeviceWithUUID:myUUID];
    if(!device){
        NSDictionary *body = @{
                               @"deviceName":@"no device",
                               @"deviceMac":deviceMac,
                               @"deviceStatusDescription":@"no device",
                               @"deviceStatus":@"error"
                               };
        [self.bridge.eventDispatcher sendAppEventWithName:@"ERROR_BT_DEVICE"                                                    body:body];
        return;
    }
    
    ENSanDyGlucoseDevice *nativeDevice = (ENSanDyGlucoseDevice *)[device getNativeDevice];
    
    
    [nativeDevice getAllDataFromIndex:index];
}

-(void)onCreatedChannel:(BLEDevice *)device{
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc"
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_CREATED_CHANNEL"                                                    body:body];
}




/** 认证成功 */
-(void)onAuthenticated:(BLEDevice *)device IsSuc:(Boolean) isSuc{
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"isSuc":[NSNumber numberWithBool:isSuc]
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_DO_AUTHORIZATION"                                                    body:body];
    
}

///**  */
//
/** 获取状态 */

-(void)onGetMonitorStatus:(BLEDevice *)device IsStartMonitor:(Boolean)isStartMonitor{
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"isStartMonitor":[NSNumber numberWithBool:isStartMonitor]
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_MONITOR_STATUS"                                                    body:body];
}

/** 获取电池电量 */
-(void)onGetBattery:(BLEDevice *)device Value:(double)value{
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"value":@(value)
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_BATTERY"                                                    body:body];
}

/** 获取全部数据的数量  */
-(void)onGetAllDataCount:(BLEDevice *)device Count:(UInt16)count{
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"count":[NSNumber numberWithInt:count]
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_ALL_DATA_COUNT"                                                    body:body];
    
}
//
/** 得到过程数据 */

-(void)onGetProgressData:(BLEDevice *)device
                DataList:(NSArray *)datalist
                   Index:(int)index{
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"dataList":datalist,
                          @"dataIndex":@(index)
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_GET_ALL_DATA"                                                    body:body];
}


-(void)onStartMonitor:(BLEDevice *)device IsSuc:(Boolean)isSuc{
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"isSuc":[NSNumber numberWithBool:isSuc]
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_START_MONITOR"                                                    body:body];
    
    
}
//
-(void)onStopMonitor:(BLEDevice *)device IsSuc:(Boolean)isSuc{
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"isSuc":[NSNumber numberWithBool:isSuc]
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_STOP_MONITOR"                                                    body:body];
    
}
/////** 获取数据 */
-(void)onUpdateNewDataDevice:(BLEDevice *)device
                       Index:(UInt16)index
                   ElecValue:(double)elecValue{
    device.nativeDevice.deviceDelegate = self;
    
    NSDictionary *body =@{
                          @"deviceName":[device getDeviceName],
                          @"deviceMac":[[device getUUID] UUIDString],
                          @"deviceStatusDescription":@"连接成功",
                          @"deviceStatus":@"connectSuc",
                          @"index":@(index),
                          @"value":@(elecValue)
                          };
    [self.bridge.eventDispatcher sendAppEventWithName:@"DYG_ON_UPDATE_NEW_DATA"                                                    body:body];
    
}




@end
