//
//  XinJieManage.h
//  xinjiesmartframework
//
//  Created by hw on 2026/3/2.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
//#import "Model/SummaryModel.h"

NS_ASSUME_NONNULL_BEGIN

extern const NSString   *kMacKey ; //连接对应的地址  非Mac真实地址
extern const NSString   *kAutoLink; //自动连接

@class BlueResultObject;
@class BabyBluetooth;
@class BatchDataService;


typedef void (^CompletionBlock)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI);

typedef void (^LoginBlock)(BOOL sucessed, NSString *errorString);

typedef void (^TouchthresholdBlock)(NSDictionary * _Nonnull result,NSError *error);

@protocol XinJieDelegate<NSObject>

@required
@optional

/// 返回戒指的一些硬件属性，包括真实的MAC地址  要求固件版本     >0.26.03.2.1
/// - Parameter model: <#model description#>
- (void)completionResultObject:(id)model;


/// 连接状态变化
/// - Parameter connected: 连接/断开
- (void)statusChange:(NSNumber *)connected;

@end


@interface XinJieManage : NSObject

@property (strong, nonatomic) CBPeripheral *curentConnectPeripheral;//当前连接的设备
@property (assign, nonatomic) BOOL is_return;//是否要回传数据
@property (strong, nonatomic, nullable) NSString *userID;
@property (strong, nonatomic, nullable) NSString *userKey;

/**
 * 单例构造方法
 * @return XinJieManage共享实例
 */
+ (instancetype)shareXinJieManageBluetooth;

/// 配置相关数据（必须）
/// @param userID 用户id
/// @param userKey 用户key
/// @param filterName 用于过滤蓝牙信号
- (void)configUserID:(NSString *)userID userKey:(NSString *)userKey filterName:(NSString *)filterName;
/**
 * 当前连接的戒指的虚拟mac地址（苹果设备把mac地址加密）
 */
- (NSString *)fecthVirtualConnectMac;

/// 根据特征值获取对象
/// @param characteristic @"4249B001-2827-458E-B34C-CB6FAF92F42C"
- (CBCharacteristic *)findCharacteristicByUUIDString:(NSString *)characteristic;
/// 添加回调函数
/// @param delegate 回调
- (void)addBlockArray:(NSObject *)delegate;

/// 删除回调
/// @param delegate 回调
- (void)removeBlockArray:(NSObject *)delegate;

//- (void)startScan:(UINavigationController *) nav filterName:(NSString *)filterName;

///  监听通道
/// @param channelOnPeropheralView 通道
/// @param block 回调
- (void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channelOnPeropheralView
                                             block:(void (^)(CBPeripheral *peripheral, CBService *service, NSError *error))block;

/// 测试界面
/// @param nav 导航栏
/// @param filterName  用于过滤蓝牙信号
-(void)gotoFunctionTest:(UINavigationController *) nav
             filterName:(NSString *)filterName;

/// 检查是扫描还是直接连接（检测是否有连接过的设备）
- (void)checkScanOrConnect;

/// 是否要自动连接
- (BOOL)isAutoLink;

/// 是否是连接状态
- (BOOL)isConnected;

/// 停止扫描
- (void)stopScan;
/// 断开连接
- (void)disconnected;
/// 开始连接
/// @param peripheral <#uuid description#>
- (void)connectToPeripheral:(CBPeripheral *)peripheral;
/// 自动连接通过UUID
- (void)autoConnectByUUID;

/// 同步当前时间到蓝牙戒指
/// - Parameter block: 仅返回是否写入成功
- (void)synchronizeTime_ring:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

/// 获取时间到蓝牙设备
/// - Parameter block: result = @{@"timestamp":timestamp}
- (void)fetchTime_ring:(void (^)(NSDictionary *result,NSError *error))block;

/// 请求固件版本信息发出
/// - Parameter block: result = @{@"Ringversion":receiveDataString}
- (void)fetchRingversion_ring:(void (^)(NSDictionary *result,NSError *error))block;

/// 请求电量信息发出
/// - Parameter block: @{@"BatteryLevel":receiveDataString}
- (void)fetchBatteryLevel_ring:(void (^)(NSDictionary *result,NSError *error))block;

/// 关机
/// - Parameter block: 仅返回是否写入成功
- (void)shutdown_ring:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

/// 恢复出厂
/// - Parameter block: 仅返回是否写入成功
- (void)reset_ring:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

/// 获取最新步数信息
/// - Parameter block: 成功返回JSON字段
- (void)fetchLastStep_ring:(void (^)(NSDictionary * _Nonnull result,NSError *error))block ;

/// 汇总最新各类型的最新值Summary Characteristic
/// - Parameter block: 成功返回JSON字段
- (void)fetchLastOtherValue_ring:(void (^)(NSDictionary * _Nonnull result,NSError *error))block;

/// 获取充电状态
/// - Parameter block: @{@"isBatteryCharging":@(isBatteryCharging)}
- (void)fetchBatteryChargingStatus_ring:(void (^)(NSDictionary * _Nonnull result,NSError *error))block;

/// 获取触摸阈值
/// - Parameters:
///   - block: 回调
///   - respondBlock: @{@"touchthreshold":@(360)}
- (void)fetchTouchthreshold_ring:(void (^)(CBCharacteristic *characteristic,NSError *error))block
                    respondBlock:(void (^)(NSDictionary * _Nonnull result,NSError *error))respondBlock;

/// 获取心率、运动、体温芯片连接通讯状态，；在（！！！！未充电！！！！）情况下3秒绿灯3秒红外灯3秒红灯（红外灯人眼无法观察）
/// - Parameter block: 只返回是否写入成功
/// - respondBlock: @{@"goodorbad":@(7)}
- (void)fetchGoodOrBad_ring:(void (^)(CBCharacteristic *characteristic,NSError *error))block
               respondBlock:(void (^)(NSDictionary * _Nonnull result,NSError *error))respondBlock;

/// 通过特征值获取当前戒指真实的MAC地址（用于iOS设备）
/// - Parameter block: @{@"realMacAddress":realMacAddress}
- (void)fetchiOSRealMac_ring:(void (^)(NSDictionary * _Nonnull result,NSError *error))block;
/// 心率设置自动测试间隔
/// - Parameters:
///   - setValue: 分钟
///   - finishBlock: 只返回是否写入成功
- (void)heartSetting_ring:(int)setValue finishBlock:(void (^)(CBCharacteristic *characteristic,NSError *error))finishBlock;

/// 血氧设置自动测试间隔
/// - Parameters:
///   - setValue: 分钟
///   - finishBlock: 只返回是否写入成功
- (void)bloodSetting_ring:(int)setValue finishBlock:(void (^)(CBCharacteristic *characteristic,NSError *error))finishBlock;

/// 压力设置自动测试间隔
/// - Parameters:
///   - setValue: 分钟
///   - finishBlock: 只返回是否写入成功
- (void)pressSetting_ring:(int)setValue finishBlock:(void (^)(CBCharacteristic *characteristic,NSError *error))finishBlock;


/// 自动连接设置
/// - Parameter autoLink: TRUE为自动
- (void)autoLinkSetting_ring:(BOOL)autoLink;

/// 批量获取戒指数据
/// - Parameters:
///   - requestAll: //dataType 1-计步，2-心率 3-RR Interval(RR间期) 4=压力，5=血氧，6=体温，7=睡眠
///   - timestampMap: dictionary:{@"1" ==> 1773825946,@"2"==>1773825942}
///   - showLoading: 是否loading
///   - respondBlock: @{@"onlyOneTypeBlock":dataArray,@"dataType":@(dataType)}
- (void)fetchBatchDataFromBluetooth:(BOOL)requestAll
                       timestampMap:(NSDictionary *)timestampMap
                        showLoading:(BOOL)showLoading
                       respondBlock:(void (^)(NSDictionary * _Nonnull result,NSError *error))respondBlock;

/// 获取戒指里面某一类型的批量数据
/// - Parameters:
///   - dataType: //用于特定类请求 1=计步, 2=心率, 3=RRI(RR间期)，4=压力, 5=血氧, 6=体温, 7=睡眠,
///   - startTimestamp: 开始时间戳（最长7天内的数据）
///   - maxRetries: 最大重试次数
///   - respondBlock: @{@"onlyOneTypeBlock":dataArray,@"dataType":@(dataType)}
- (void)fetchLatestDataByType:(NSInteger)dataType
               startTimestamp:(NSInteger)timestamp
                   maxRetries:(int)maxRetries
                 respondBlock:(void (^)(NSDictionary * _Nonnull result,NSError *error))respondBlock;

/// 获取对应的真实值（私有函数）
/// - Parameters:
///   - oriData: 原始数据
///   - subRange: subRange
- (NSNumber *)fetchValueByData:(NSData *)oriData subRange:(NSRange)subRange;

//手动测试
- (void)sendStopTest:(void (^)(NSDictionary * _Nonnull result,NSError *error))callback;
//心率测试
- (void)heartRate_test:(void (^)(NSDictionary * _Nonnull result,NSError *error))respondBlock;

/// 血氧测试
/// - Parameter respondBlock: <#respondBlock description#>
- (void)bloodOxygen_test:(void (^)(NSDictionary * _Nonnull result, NSError *error))respondBlock;
/// hrv测试
- (void)hrv_test:(void (^)(NSDictionary * _Nonnull result, NSError *error))respondBlock;

- (void)startOtaUpgrade:(NSString *)firmwareUrl callback:(void (^)(NSDictionary * _Nonnull result, NSError *error))callback;

- (void)fetchHeartData:(void (^)(NSDictionary * _Nonnull result, NSError *error))callback;

- (void)fetchOxygenbloodData:(void (^)(NSDictionary * _Nonnull result, NSError *error))callback;

- (void)fetchHRVData:(void (^)(NSDictionary * _Nonnull result, NSError *error))callback;


@end

NS_ASSUME_NONNULL_END
