//
//  base.h
//  RYCommunication
//
//  Created by ldc on 2020/9/7.
//  Copyright © 2020 Xiamen Hanin. All rights reserved.
//

#ifndef base_h
#define base_h

#import <Foundation/Foundation.h>
#import <RYCommunication/RYAuthorization.h>

#ifdef NoLog
#define NSLog(format, ...)
#endif

NS_ASSUME_NONNULL_BEGIN

@protocol RYAccessory;

typedef void(^RYAccessoryClosedBlock)(id<RYAccessory>, NSError * _Nullable);

typedef NS_ENUM(NSUInteger, RYAccessoryConnectStatus) {
    RYAccessoryConnectStatusDisconnected = 0,
    RYAccessoryConnectStatusConnecting,
    RYAccessoryConnectStatusConnected,
    RYAccessoryConnectStatusDisconnecting
};

/**
 * \~chinese
 *  
 * 数据解析器
 *  
 * \~english
 *  
 * Data resolver
 *  
 */
@protocol RYDataResolver <NSObject>

/// 解析成功回调
@property (nonatomic, copy ) void (^ _Nullable resolvedBlock)(id result);

/// 解析失败回调
@property (nonatomic, copy) void (^ _Nullable handleFailBlock)(NSData *) __attribute__((deprecated("use RYDataResponse in resolvedBlock")));

/// 添加解析失败监听
/// @param block 回调
- (void)addResolveFailListener:(nonnull void (^)(NSData* _Nonnull))block __attribute__((deprecated("use addResolvedListener")));;

/// 添加解析数据监听
/// @param block 回调
- (void)addResolvedListener:(void (^)(id))block;


/// 合并另一个解析器
/// @param resolver 解析器
- (void)combine:(id<RYDataResolver>)resolver;

/**
 *  \~chinese
 *
 *  读取外设输入数据
 *
 * @param data 输入数据
 *
 *  \~english
 *
 *  read input data for accessory
 *
 *  @param data input data
 *
 */
- (void)readInput:(NSData *)data;

/// 清空内部数据
- (void)clear;

@end

/**
 *  \~chinese
 *
 *  外部设备协议
 *
 *  \~english
 *
 *  External accessory protocol
 *
 */
@protocol RYAccessory <NSObject>

@property (nonatomic, copy) NSString *name;///< \~chinese 名称 \~english name

@property (nonatomic, strong) RYAuthorization * _Nullable auth;///< \~chinese 授权验证对象 \~english authorize object

@property (nonatomic, strong) id<RYDataResolver> _Nullable resolver;///< \~chinese 数据解析器 \~english data resolver

@property (nonatomic, assign) RYAccessoryConnectStatus connectStatus;

/// 设备连接状态监听，这里的设备断开状态并不意味着连接的终止，可能会有重连机制
@property (nonatomic, copy) void (^ _Nullable connectStatusListener)(RYAccessoryConnectStatus);

/// 设备当前是否连接
@property (nonatomic, assign, readonly) BOOL connected;

/// 连接关闭回调，意味着连接生命周期的终止
@property (nonatomic, copy) RYAccessoryClosedBlock _Nullable closedBlock;

/**
 *  \~chinese
 *
 *  连接
 *
 *  @param successBlock 连接成功回调
 *  @param failBlock 连接失败回调
 *
 *  \~english
 *
 *  connect
 *
 *  @param successBlock called when connect success
 *  @param failBlock called when connect fail
 *
 */
- (void)connect:(void(^ _Nullable)(void))successBlock fail:(void(^ _Nullable)(NSError *))failBlock;

/**
 *  \~chinese
 *
 *  断开连接
 *
 *  \~english
 *
 *  disconnnect
 *
 */
- (void)disconnect;

/**
 *  \~chinese
 *
 *  写入数据
 *
 *  @param data 要写入的数据
 *  @param block 写入进度回调
 *
 *  \~english
 *
 *  write data
 *
 *  @param data data will write
 *  @param block called when write process has updated
 *
 */
- (void)write:(NSData *)data progress:(void(^_Nullable)(NSProgress *))block;

/**
 *  \~chinese
 *
 *  停止写入
 *
 *  \~english
 *
 *  stop write
 *
 */
- (void)stopWrite;

@end

@protocol RYSessionIdentity <NSObject>

@property (nonatomic, copy) NSString *identity;

@end

/// 消息接收器
@protocol RYMessageReceiver <NSObject>

- (void)receive:(id)message;

@end

typedef void (^RYEmptyResultBlock)(NSError * _Nullable);
typedef void (^RYProgressBlock)(NSProgress *);
typedef void (^RYDataResultBlock)(NSData * _Nullable, NSError * _Nullable);
typedef void (^RYStringResultBlock)(NSString * _Nullable, NSError * _Nullable);
typedef void (^RYIntegerResultBlock)(NSInteger, NSError * _Nullable);

NS_ASSUME_NONNULL_END

#endif /* base_h */
