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

#import <Foundation/Foundation.h>
#import <RYCommunication/base.h>
#import <RYCommunication/RYSessionTask.h>

NS_ASSUME_NONNULL_BEGIN

/*
 *  \~chinese
 *
 *  通用数据解析模型
 *
 *  \~english
 *
 *  Common data resolve model
 *
 */
@interface RYCommonResolveModel : NSObject

@property (nonatomic, assign) BOOL isDiscardable;///< \~chinese 是否丢弃 \~english discardable

@property (nonatomic, strong) NSData *data;///< \~chinese 数据内容 \~english content

@property (nonatomic, assign) NSInteger cost;///< \~chinese 消耗的数据量 \~english data length costed

@property (nonatomic, strong) NSData *rawData;///< \~chinese 源数据 \~english raw data

@end

/*
 *  \~chinese
 *
 *  路由处理模块
 *
 *  \~english
 *
 *  Router block
 *
 */
@interface RYDataRouterBlock : NSObject

@property (nonatomic, assign) NSInteger minDataLength;///< \~chinese 最小数据长度 \~english min data length

/*
 *  \~chinese
 *
 *  用于将数据生成指定模型的block
 *
 *  \~english
 *
 *  a block to generate resolve model from supplied data
 *
 */
@property (nonatomic, copy) RYCommonResolveModel * _Nullable (^ handleBlock)(NSData *);

@end

@interface RYKeyedDataRouterBlock : NSObject

@property (nonatomic, strong) RYDataRouterBlock *block;

@property (nonatomic, strong) NSData *key;

@end

/*
 *  \~chinese
 *
 *  数据路由器
 *
 *  \~english
 *
 * Data router
 *
 */
@interface RYDataRouter : NSObject

/*
 *  \~chinese
 *
 *  注册操作
 *
 *  @param key key
 *  @param block 处理block
 *
 *  \~english
 *
 *  register opration
 *
 *  @param key key
 *  @param block process block
 *
 */
- (void)registerHandle:(NSData *)key block:(RYDataRouterBlock *)block;

/*
 *  \~chinese
 *
 *  获取操作block
 *
 *  \~english
 *
 *  Get process block
 *
 */
- (RYDataRouterBlock * _Nullable)handle:(NSData *)key;

@end

@interface RYBaseDataResolver : NSObject <RYDataResolver>

- (void)distributeResolvedMessage:(id)result;

- (void)distributeResolveFailMessage:(NSData *)result;

@end

/*
 *  \~chinese
 *
 *  一个不进行任何处理的数据解析器
 *
 *  \~english
 *
 *  A resolver that do nothing
 *
 */
@interface RYNotHandlingResolver : RYBaseDataResolver

@end

/*
 * \~chinese
 *  
 * 通用解析器
 *  
 * \~english
 *  
 * Common resolver
 *  
 */
@interface RYCommonResolver : RYBaseDataResolver

/// 持有的路由块，用来合并解析器
@property (nonatomic, strong) NSMutableArray<RYKeyedDataRouterBlock *> *routerBlocks;

/// 路由对象
@property (nonatomic, strong) RYDataRouter *router;

/// 合并解析器
/// - Parameter resolver: 解析器
/// - Note: 只有RYCommonResolver类型支持合并
- (void)combine:(id<RYDataResolver>)resolver;

/*
 *  \~chinese
 *
 *  注册操作，子类重写
 *
 *  \~english
 *
 *  register handle, override by subclass
 *
 */
- (void)registerHandle;

/*
 *  \~chinese
 *
 *  注册操作
 *
 *  @param key key
 *  @param block 处理block
 *
 *  \~english
 *
 *  register opration
 *
 *  @param key key
 *  @param block process block
 *
 */
- (void)registerHandle:(NSData *)key block:(RYDataRouterBlock *)block;

@end

NS_ASSUME_NONNULL_END
