//
//  AliyunIdentityNetwork.h
//  AliyunIdentityNetwork
//
//  Copyright © 2024 aliyun.com. All rights reserved.
//

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

NS_ASSUME_NONNULL_BEGIN

//! Project version number for AliyunIdentityNetwork.
FOUNDATION_EXPORT double AliyunIdentityNetworkVersionNumber;

//! Project version string for AliyunIdentityNetwork.
FOUNDATION_EXPORT const unsigned char AliyunIdentityNetworkVersionString[];

#pragma mark - Block Definitions

typedef void(^AliyunNetworkBlock)(bool success, NSDictionary *__nullable resContent, NSString *__nullable error);
typedef void(^AliyunOssBlock)(bool success, int fileIdx, NSString *fileName);
typedef void(^AliyunOssUploadBlock)(bool success);

#pragma mark - AliyunIdentityNetwork

/**
 * 统一网络层
 *
 * 负责 HTTP POST (POP 网关协议)、OSS 文件上传、WebSocket 通信。
 * 通过 IDVNetworkEnvProvider 协议获取环境配置，不包含任何业务逻辑。
 */
@interface AliyunIdentityNetwork : NSObject

@property(nonatomic, strong) NSOperationQueue *networkQueue;

+ (AliyunIdentityNetwork *)sharedInstance;

#pragma mark - Environment Provider

/// 设置网络环境配置提供者 (由 Adapter 层注入)
- (void)setEnvProvider:(id<IDVNetworkEnvProvider>)provider;

/// 获取当前环境配置提供者
- (id<IDVNetworkEnvProvider>)envProvider;

#pragma mark - Lifecycle

- (void)initNetwork;
- (void)cancelAll;

/// 重置容灾/降级状态 (备用模式、URL 轮转下标、服务端下发域名)。
/// 每次开启新的核验会话时调用，避免上一会话遗留的备用模式/下标
/// 影响后续请求 (否则单例状态会导致「第一次成功、第二次起失败」)。
- (void)resetFailoverState;

#pragma mark - Server Auth URLs

/// 设置服务端下发的域名列表 (来自 InitDeviceIntl 响应)
/// 若设置则优先于本地 httpFallbackUrls 使用
@property(nonatomic, strong, nullable) NSArray<NSString *> *serverAuthUrls;

#pragma mark - HTTP Request

/// 发起带完整参数的 HTTP 请求
- (void)sendHttpRequest:(NSString *)appKey
              appSecret:(NSString *)appSecret
               withSeed:(NSString *)appSeed
                    Url:(NSString *)url
                 action:(NSString *)action
                version:(NSString *)version
                   host:(NSString *)host
                  param:(NSDictionary *)parameters
        completionBlock:(AliyunNetworkBlock)completionBlock;

/// 发起简化的 HTTP 请求 (自动从 envProvider 获取 URL/Key 等)
- (void)sendHttpRequestInteli:(NSString *)action
                      version:(NSString *)version
                     withSeed:(NSString *)appSeed
                        param:(NSDictionary *)parameters
              completionBlock:(AliyunNetworkBlock)completionBlock;

#pragma mark - Signature

+ (NSString *)computeSignature:(NSDictionary *)parameters secret:(NSString *)secret;

#pragma mark - Protocol Encryption

+ (BOOL)isSecCertifyId:(NSString *)certifyId;

#pragma mark - OSS Upload

- (void)uploadOssFile:(int)fileIdx
         withFileName:(NSString *)fileName
      withFileContent:(NSData *)fileContent
        withAccessKey:(NSString *)accessKey
        withSecretKey:(NSString *)secretKey
    withSecurityToken:(NSString *)securityToken
         withEndPoint:(NSString *)endPoint
       withNamePrefix:(NSString *)namePrefix
       withBucketName:(NSString *)bucketName
            withBlock:(AliyunOssBlock)ossBlock;

- (BOOL)uploadOssFileSyn:(int)fileIdx
            withFileName:(NSString *)fileName
         withFileContent:(NSData *)fileContent
           withAccessKey:(NSString *)accessKey
           withSecretKey:(NSString *)secretKey
       withSecurityToken:(NSString *)securityToken
            withEndPoint:(NSString *)endPoint
          withNamePrefix:(NSString *)namePrefix
          withBucketName:(NSString *)bucketName;

- (void)uploadOssFileDataArray:(NSMutableArray *)fileContentArray
                 fileNameArray:(NSMutableArray *)fileNameArray
                     accessKey:(NSString *)accessKey
                     secretKey:(NSString *)secretKey
                 securityToken:(NSString *)securityToken
                      endPoint:(NSString *)endPoint
                    namePrefix:(NSString *)namePrefix
                    bucketName:(NSString *)bucketName
                         block:(AliyunOssUploadBlock)ossBlock;

#pragma mark - WebSocket

/// 构建 WebSocket POP 请求参数
+ (NSMutableDictionary *)doSocketRequestWithAppKey:(NSString *)appKey
                                         appSecret:(NSString *)appSecret
                                          withSeed:(NSString *)appSeed
                                               Url:(NSString *)Url
                                            action:(NSString *)action
                                           version:(NSString *)version
                                              host:(NSString *)host
                                             param:(NSDictionary *)parameters
                                   completionBlock:(AliyunNetworkBlock)completionBlock;

@end

NS_ASSUME_NONNULL_END
