//
//  ENCholesterolDevice.m
//  ENBLEProject
//
//  Created by lvwang2002 on 16/4/8.
//  Copyright © 2016年 Facebook. All rights reserved.
//

#import "ENCholesterolDevice.h"
static  NSString *UUID_BTLE_SERVICE = @"C14D2C0A-401F-B7A9-841F-E2E93B80F631";
static NSString *UUID_BTLE_RECEIVE = @"81eb77bd-89b8-4494-8a09-7f83d986ddc7";
@implementation ENCholesterolDevice
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
		//找到服务,开始扫描指定的服务
	NSArray *array = [peripheral services];
//
	for( CBService *service in array ){
//			//		NSString *uuid = [NSString stringWithFormat:@"%@",service.UUID];
		CBUUID *serviceUUID = [CBUUID UUIDWithString:UUID_BTLE_SERVICE];
		NSLog(@"service uuid%@",serviceUUID);
		if([serviceUUID isEqual:service.UUID]){
			NSArray *uuids = @[[CBUUID UUIDWithString:UUID_BTLE_RECEIVE]];
			[peripheral discoverCharacteristics:uuids forService:service];
			break;
		}
	}
	
	
	
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;{
	CBUUID *receiveUUID = [CBUUID UUIDWithString:UUID_BTLE_RECEIVE];
	
	for( CBCharacteristic *characteristic in service.characteristics){
		NSLog(@"characteristic is %@",characteristic);
		
		if([characteristic.UUID isEqual:receiveUUID]){
			[_peripheral setNotifyValue:YES forCharacteristic:characteristic];
		}
	}
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForReceiveData:(NSData *)data UUID:(CBUUID *)uuid  error:(NSError *)error{
	CBUUID *receiveUUID = [CBUUID UUIDWithString:UUID_BTLE_RECEIVE];
	
	if(![receiveUUID isEqual:uuid]){
		return;
	}
	
	[self receiveData:data];
}

-(void)receiveData:(NSData *)data{
	[self getXuezhiData:data];
}

	//    private StringBuilder mXuezhiValue = new StringBuilder();// ASCII
static NSString  *mXuezhiValue = @"";


-(void)getXuezhiData:(NSData *)data{
		// // 处理血脂数据
		// mXuezhiData.setVisibility(View.VISIBLE);
		// mOtherLayout.setVisibility(View.GONE);
//	Log.e(TAG,"血脂数据:"+Bytes2HexString(data,0,data.length));
	NSString *xuezhiData;
//	@try {
//		xuezhiData = new String(data, "UTF-8");
	xuezhiData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//		mXuezhiValue += xuezhiData;
	mXuezhiValue = [NSString stringWithFormat:@"%@%@",mXuezhiValue,xuezhiData];
	
	
//	} catch (UnsupportedEncodingException e) {
//		e.printStackTrace();
//	}
	
	NSLog(@"xue zhi %@",mXuezhiValue);
	
	NSUInteger size = data.length;
//	String[] dataArr = null;
	NSArray *dataArr = [[NSArray alloc] init];
	const unsigned char *dataByte = [data bytes];
	
	if (size >= 4) {
			// 连续两个空行0x0d，0x0a
		if ((dataByte[size - 2] == 13 &&
				 dataByte[size - 1] == 10 &&
				 dataByte[size - 4] == 13 &&
				 dataByte[size - 3] == 10 ) ||
				(dataByte[size - 2] == 10 &&
				 dataByte[size - 1] == 13 &&
				 dataByte[size - 4] == 10 &&
				 dataByte[size - 3] == 13) ) {
				//String text = mXuezhiValue.toString();
				dataArr = [self getValue:mXuezhiValue];
			
						//final String[] myDataArray = dataArr;
			NSLog(@"chol%@",dataArr[0]);
			NSLog(@"hdl%@",dataArr[1]);
			NSLog(@"trig%@",dataArr[2]);
			NSLog(@"ldl%@",dataArr[3]);
					
					NSDictionary *info = @{
																 @"chol":dataArr[0],
																 @"hdl":dataArr[1],
																 @"trig":dataArr[2],
																 @"ldl":dataArr[3]
																 };
					
			if([self.deviceDelegate respondsToSelector:@selector(onCholesterolResult:WithDevice:)]){
				[(id)self.deviceDelegate onCholesterolResult:info WithDevice:_device];
			}
			mXuezhiValue = @"";
		}
	}
}

// String[] dataString = null;
//String[] dataString2 = null;

-(NSArray *)getValue:(NSString *)text{
	
	NSLog(@"text:%@", text);
		//	Log.e("dataString", String.valueOf(dataString.length));
	
	NSArray *dataString = [text componentsSeparatedByString:@"\n"];
	NSMutableArray *dataString2 = [[NSMutableArray alloc] initWithCapacity:10];
	
	if (dataString.count >= 7) {
		NSString *chol = dataString[2];
		NSString *hdl = dataString[3];
		NSString *trig = dataString[4];
		NSString *ldl = dataString[6];
			//		chol = chol.split("mg")[0];
		chol = [chol componentsSeparatedByString:@"mg"][0];
			//		chol = chol.split(":")[1];
		chol = [chol componentsSeparatedByString:@":"][1];
			//		hdl = hdl.split("mg")[0];
		hdl = [hdl componentsSeparatedByString:@"mg"][0];
			//		hdl = hdl.split(":")[1];
		hdl = [hdl componentsSeparatedByString:@":"][1];
			//		trig = trig.split("mg")[0];
		trig = [trig componentsSeparatedByString:@"mg"][0];
			//		trig = trig.split(":")[1];
		trig = [trig componentsSeparatedByString:@":"][1];
			//		ldl = ldl.split("mg")[0];
		ldl = [ldl componentsSeparatedByString:@"mg"][0];
			//		ldl = ldl.split(":")[1];
		ldl = [ldl componentsSeparatedByString:@":"][1];
		
		dataString2[0] = chol;
		dataString2[1] = hdl;
		dataString2[2] = trig;
		dataString2[3] = ldl;
	} else {
		if (dataString != nil && dataString2 != nil) {
			dataString = nil;
			dataString2 = nil;
		}
	}
	return dataString2;
	
}


@end
