//
//  CGMAlgo.h
//  CGMAlgo
//
//  Created by yuwell on 2024/11/5.
//

#import <Foundation/Foundation.h>
#import "AlgorithmTools.h"

/**
 * Analysis bytes from transmitter when BleBluetooth scan
 */
@interface Verify : NSObject

// transmitter name CT4: SNXXXXX
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *category;
// transmitter(ble Device) status isBinding
@property (nonatomic, assign) BOOL isBound;
@property (nonatomic, assign) NSInteger type;

- (NSString *)toString;

@end

/**
 * Analysis bytes from transmitter when command (response pullGlucose) or push from transmitter
 */
@interface Glucose : NSObject

@property (nonatomic, assign) int gluID;
@property (nonatomic, assign) int year;
@property (nonatomic, assign) int month;
@property (nonatomic, assign) int day;
@property (nonatomic, assign) int hour;
@property (nonatomic, assign) int minute;
@property (nonatomic, assign) float iw;
@property (nonatomic, assign) float ib;
@property (nonatomic, assign) float T;
@property (nonatomic, assign) float electric;

- (NSString *)toString;

@end



@interface TransmitterReset : NSObject

@property (nonatomic, assign) BOOL isBind;
@property (nonatomic, assign) UnbindType unbindType;
@property (nonatomic, assign) Byte transmitterTips;
@property (nonatomic, assign) int lengthResponse;

- (id)initWithParameterData:(NSData *)data;
- (id)initWithParameterIsBind:(BOOL)isBind;

- (void)setResponse:(NSData *)data;

- (BOOL)getIsBind;

- (UnbindType)getUnbindType;

- (Byte)getTransmitterTips;

- (int)getLengthResponse;

- (NSString *)toString;

@end


@interface ProtocalTools : NSObject

+ (BOOL)commonCT4DeviceType:(NSString *)deviceType;
/**
 * Parse the Bluetooth broadcast binary sent by the transmitter when searching for the transmitter
 * @param data When Bluetooth is scanning, the transmitter broadcasts the binary bytes
 * @return {@link Verify}
 */
+ (Verify *)verify:(NSDictionary *)data;

/**
 * check request
 * @param device {@link EDevice}
 * @return bytes：send to transmitter bytes
 */
+ (NSData *)check_request:(EDevice *)device;
/**
 * check response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return CHECK_FAIL_POWER or SUCCESS or FAIL
 */
+ (HW_CHECK_RET)check_response:(NSData *)data andEDevice:(EDevice *)device;
/**
 * check response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @param iw_limit Threshold of valid signal, 3.0 is recommended
 * @return SUCCESS or CHECK_FAIL_POWER or CHECK_FAIL_IW or FAIL
 */
+ (HW_CHECK_RET)check_response2:(NSData *)data andEDevice:(EDevice *)device andIwLimit:(float)iw_limit;
/**
 * check response, For testing only, not to judge the transmitter power
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return SUCCESS or FAIL
 */
+ (HW_CHECK_RET)check_response_test:(NSData *)data andEDevice:(EDevice *)device;

/**
 * set date request
 * @param device {@link EDevice}
 * @return send to transmitter bytes, Android local time
 */
+ (NSData *)setDate_request:(EDevice *)device;
/**
 * set date response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return if true is success
 */
+ (BOOL)setDate_response:(NSData *)data andEDevice:(EDevice *)device;

/**
 * init request
 * @param device {@link EDevice}
 * @return send to transmitter bytes
 */
+ (NSData *)start_init_request:(EDevice *)device;
/**
 * init response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return if true is success
 */
+ (BOOL)init_response:(NSData *)data andEDevice:(EDevice *)device;

/**
 * low power request
 * @param device {@link EDevice}
 * @return send to transmitter bytes
 */
+ (NSData *)lowPower_request:(EDevice *)device;
/**
 * low power response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return if true is success
 */
+ (BOOL)lowPower_response:(NSData *)data andEDevice:(EDevice *)device;

/**
 * Get data information from the transmitter
 * @param glucoseId The data glucoseId
 * @param device {@link EDevice}
 * @return send to transmitter bytes
 */
+ (NSData *)pullGlucose_request:(NSInteger)glucoseId andEDevice:(EDevice *)device;
/**
 * Get historical data information from the transmitter
 * @param glucoseId The starting data glucoseId in the historical data
 * @param number How much historical data do you want to get at one time? The maximum data size is 40, based on the device mtu
 * @param device {@link EDevice}
 * @return send to transmitter bytes
 */
+ (NSData *)pullGlucose_series_request:(NSInteger)glucoseId
                             andNumber:(NSInteger)number
                            andEDevice:(EDevice *)device;

/**
 * Get the parsing of the push data information
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return {@link Glucose}
 */
+ (Glucose *)getGlucose:(NSData *)data andEDevice:(EDevice *)device;
/**
 * Get the parsing of the returned historical data information
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return {@link List<Glucose>}
 */
+ (NSArray<Glucose*> *)getGlucose_series:(NSData *)data andEDevice:(EDevice *)device;

/**
 * unbind request
 * @param device {@link EDevice}
 * @return send to transmitter bytes
 */
+ (NSData *)unBind_request:(EDevice *)device;
/**
 * unbind response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return true : if true, unbind success
 */
+ (BOOL)unBind_response:(NSData *)data andEDevice:(EDevice *)device;

/**
 * reset request
 * @param device {@link EDevice}
 * @return send to transmitter bytes
 */
+ (NSData *)reset_request:(EDevice *)device;
/**
 * reset response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return If true, the transmitter is reset and the transmitter has been disabled.
 */
+ (BOOL)reset_response:(NSData *)data andEDevice:(EDevice *)device;
/**
 * reset response
 * @param data response bytes from transmitter
 * @param device {@link EDevice}
 * @return {@link TransmitterReset}
 * @throws Exception response bytes from transmitter error
 */
+ (TransmitterReset *)reset_response2:(NSData *)data andEDevice:(EDevice *)device;

@end
