//
//  OffcnUploadManager.m
//  OffcnIMSDKiOS
//
//  Created by vockey on 2020/8/21.
//  Copyright © 2020 offcn. All rights reserved.
//

#import "OffcnUploadManager.h"
#import "ZYJQiniuSDK.h"
#import "ZYJQNTransactionManager.h"
#import "OffcnNetworkExecutor.h"

@interface OffcnUploadManager ()

@property (nonatomic, strong) ZYJQNUploadManager *upManager;
@property (nonatomic, strong) NSString *QNToken;
@end

@implementation OffcnUploadManager

static OffcnUploadManager *uploadManager;
+(OffcnUploadManager *)defaultService{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        uploadManager = [[OffcnUploadManager alloc] init];
    });
    return uploadManager;
}
-(instancetype)init{
    self = [super init];
    if (self) {
        self.upManager = [[ZYJQNUploadManager alloc] init];
    }
    return self;
}

-(void)uploadToQNFilePath:(NSString *)filePath Finished:(void(^)(BOOL success,id response,NSString *errorMessage))finished ProgressPercent:(void(^)(NSString *key, float percent))progressPercent{
    
    __weak typeof(self) weakSelf = self;
    if (self.QNToken) {
        [self uploadToQNWithQNToken:self.QNToken filePath:filePath Finished:^(BOOL success, id response, NSString *errorMessage) {
            
            finished(success,response,errorMessage);
        } ProgressPercent:^(NSString *key, float percent) {
                           
            progressPercent(key,percent);
        }];
    }else{
        [self getQNTokenFinished:^(BOOL success, NSString *errorMessage) {
            if (success) {
                [weakSelf uploadToQNWithQNToken:weakSelf.QNToken filePath:filePath Finished:^(BOOL success, id response, NSString *errorMessage) {
                    
                    finished(success,response,errorMessage);
                } ProgressPercent:^(NSString *key, float percent) {
                    
                    progressPercent(key,percent);
                }];
            }else{
                finished(NO,nil,errorMessage);
            }
        }];
    }
}

-(void)getQNTokenFinished:(void(^)(BOOL success,NSString *errorMessage))finished{
    __weak typeof(self) weakSelf = self;
    [OffcnNetworkExecutor requestUploadTokenFinished:^(BOOL success, id response, BaseResponse *baseResponse) {
        
        if (success) {
            NSString *token = [NSString stringWithFormat:@"%@",response];
            weakSelf.QNToken = token;
//            NSLog(@"七牛token---%@",token);
            finished(YES,nil);
        }else{
            finished(NO,@"获取token失败");
        }
    }];
}

-(void)uploadToQNWithQNToken:(NSString *)QNToken filePath:(NSString *)filePath Finished:(void(^)(BOOL success,id response,NSString *errorMessage))finished ProgressPercent:(void(^)(NSString *key, float percent))progressPercent{
    ZYJQNUploadOption *uploadOption = [[ZYJQNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
        
        progressPercent(key,percent);
    }params:nil checkCrc:NO cancellationSignal:nil];
    
    [self.upManager putFile:filePath key:nil token:QNToken complete:^(ZYJQNResponseInfo *info, NSString *key, NSDictionary *resp) {
//        NSLog(@"info ===== %@", info);
//        NSLog(@"key ===== %@", key);
//        NSLog(@"resp ===== %@", resp);
        
        if (resp) {
            //发送消息
            NSString *dataStr = [NSString stringWithFormat:@"http://test-offcncloud-im.eoffcn.com/%@",resp[@"key"]];
            finished(YES,dataStr,nil);
        }else{
            finished(NO,nil,@"上传失败");
        }
    }option:uploadOption];
}
@end
