//
//  KLTDatabaseHandler.h
//  KaiLiTe
//
//  Created by boosal on 2016/9/13.
//  Copyright © 2016年 boosal. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "KLTAppDelegate.h"
#import "ReceiveData+CoreDataClass.h"
#import "GlucoseData+CoreDataClass.h"
#import "Device+CoreDataClass.h"
#import "Advertise+CoreDataClass.h"
#import "CGMAlgo.h"

@interface KLTDatabaseHandler : NSObject


@property (nonatomic, strong) NSManagedObjectContext *context;
@property (nonatomic, strong) KLTAppDelegate *appDelegate;
//@property (nonatomic, strong) HKHealthStore *healthStore;

+ (KLTDatabaseHandler *)shared;
- (ReceiveData *)addReceiveCT3DataWithDevice:(Device *)currentDevice;
- (Device *)addDevice:(CBPeripheral *)peripheral;
- (GlucoseData *)addCT3GlucoseDataWithReceiveData:(ReceiveData *)data;

- (Advertise *)addAdvertisement:(NSDictionary *)ad;

- (NSArray *)queryTableWithName:(NSString *)name;
- (NSArray<GlucoseData*> *)queryGlucoseDataWithDevice:(Device *)currentDevice;

- (void)updateDeviceWithDeviceId:(NSString *)identify 
                     scannedTime:(NSString *)scannedTime
                     connectTime:(NSString *)connectTime
                  disConnectTime:(NSString *)disconnectTime;

- (void)updateDevice:(Device *)currentDevice
    initialBeginDate:(NSString *)beginTime
             endDate:(NSString *)endTime;

- (Device *)queryDeviceWithId:(NSString *)deviceID;

- (void)updatePOCAlgoBeforeRef:(ReceiveData *)data;

- (Device *)isLatestDeviceDisconnected;
- (NSInteger)getLatestAndMaxGlucoseIdOfDevice:(Device *)currentDevice;

- (NSInteger)numberOfLifeCycleSensorWithDevice:(Device *)currentDevice endNumber:(int)endNumber;
- (void)deleteReceiveDataArray:(NSArray *)dataArray;
- (NSArray *)queryReceiveDataWithDevice:(Device *)currentDevice needUserBG:(BOOL)needUserBG;
- (NSArray *)queryAllReceiveData;

- (void)reloadQueryHistoryData:(BOOL)calculateError2Data;
- (void)handleReceiveDataForError2Data;
- (void)deleteSQLiteDataArray;
- (BOOL)hasDeviceData;
@end
