//
//  CSFilewDownUpCell.m
//  zhuxun
//
//  Created by winsoft on 17/6/22.
//
//

#import "CSFileDownUpCell.h"

#import "CSFileDownLoadControllerView.h"

#import "CloudStorageUploadCenter.h"

#import "CloudStorageDownloadCenter.h"
///=============================================================================
/// @name CSFileDownUpCell
///=============================================================================
@interface CSFileDownUpCell()
@property (nonatomic , weak) UIImageView *headerIconImageView;
@property (nonatomic , weak) UILabel *fileNameLabel;

@property (nonatomic , weak) UILabel *fileCreateTimeLabel;
@property (nonatomic , weak) UILabel *fileProcessSizeLabel;

@property (nonatomic , weak) UILabel *fileSizeLabel;

@property (nonatomic , weak) UILabel *speedLabel;

@property (nonatomic , weak) UIButton *fileExtensionSwitchImageView;

@property (nonatomic , weak) CSFileDownLoadControllerView *csFileDownLoadControllerView;

@property (nonatomic , weak) CSFileExtensionViewForCell *fileExtensionView;
/**
 *  记录上一次已经进行的长度
 */
@property (nonatomic , assign) NSInteger lastProcessedDataLength;
@property (nonatomic , copy) NSDate *lastRecordTime;
@end
@implementation CSFileDownUpCell
+ (CSFileDownUpCell *)fileDownUpCellWithCSFileDownUpFrameModel:(CSFileDownUpFrameModel*)csFileDownUpFrameModel tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath identifier:(NSString *)identifier opItems:(NSArray *)opItems
{
    CSFileDownUpCell *csFileDownUpCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!csFileDownUpCell) {
        csFileDownUpCell = [[CSFileDownUpCell alloc]initFileCellWithTableView:tableView indexPath:indexPath identifier:identifier opItems:opItems];
    }
    //csFileCell.indexPath = indexPath
    
    return csFileDownUpCell;
}

- (instancetype)initFileCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath  identifier:(NSString *)identifier opItems:(NSArray *)opItems
{
    if (self  = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]) {
        
        self.tableView = tableView;
        
        UIImageView *headerIconImageView = [[UIImageView alloc]init];
        [headerIconImageView setImage:[UIImage imageNamed:@"file_unknown_icon"]];
        [self.contentView addSubview:headerIconImageView];
        self.headerIconImageView = headerIconImageView;
        
        UILabel *fileNameLabel = [[UILabel alloc]init];
        fileNameLabel.numberOfLines = 0;
        fileNameLabel.font = FileNameFont;
        [self.contentView addSubview:fileNameLabel];
        self.fileNameLabel = fileNameLabel;
        
        UILabel *fileCreateTimeLabel = [[UILabel alloc]init];
        fileCreateTimeLabel.textColor = LineForSubLineViewColor;
        fileCreateTimeLabel.font = FileCreateTimeFont;
        [self.contentView addSubview:fileCreateTimeLabel];
        self.fileCreateTimeLabel = fileCreateTimeLabel;
        
        UILabel *fileProcessSizeLabel = [[UILabel alloc]init];
        fileProcessSizeLabel.textColor = LineForSubLineViewColor;
        fileProcessSizeLabel.font = FileCreateTimeFont;
        [self.contentView addSubview:fileProcessSizeLabel];
        self.fileProcessSizeLabel = fileProcessSizeLabel;
        
        
        
        UILabel *fileSizeLabel = [[UILabel alloc]init];
        fileSizeLabel.textAlignment = NSTextAlignmentRight;
        fileSizeLabel.textColor = LineForSubLineViewColor;
        fileSizeLabel.font = FileCreateTimeFont;
        [self.contentView addSubview:fileSizeLabel];
        self.fileSizeLabel = fileSizeLabel;
        
        UILabel *speedLabel = [[UILabel alloc]init];
        speedLabel.textAlignment = NSTextAlignmentRight;
        speedLabel.textColor = LineForSubLineViewColor;
        speedLabel.font = FileCreateTimeFont;
        [self.contentView addSubview:speedLabel];
        self.speedLabel = speedLabel;
        
        
        UIButton *fileExtensionSwitchImageView = [[UIButton alloc]init];
        [fileExtensionSwitchImageView addTarget:self action:@selector(extensionSwithOnOff) forControlEvents:UIControlEventTouchUpInside];
        [fileExtensionSwitchImageView setImage:[UIImage imageNamed:@"list_Group_Arrow_Down"] forState:UIControlStateNormal];
        [self.contentView addSubview:fileExtensionSwitchImageView];
        self.fileExtensionSwitchImageView = fileExtensionSwitchImageView;
        
        CSFileDownLoadControllerView *csFileDownLoadControllerView = [[CSFileDownLoadControllerView alloc]init];
        
        [self.contentView addSubview:csFileDownLoadControllerView];
        self.csFileDownLoadControllerView = csFileDownLoadControllerView;;
        
        CSFileExtensionViewForCell *fileExtensionView = [[CSFileExtensionViewForCell alloc]initWithItmes: opItems];
        
        __weak typeof(self)weakSelf = self;
        fileExtensionView.extensionOpBlock = ^(ExtensionOP extensionOP, CSFileDownUpFrameModel *csFileDownUpFrameModel){
            
            if (weakSelf.extensionOpBlock) {
                weakSelf.extensionOpBlock(extensionOP, weakSelf.csFileDownUpFrameModel);
            }
            
        };
        
        csFileDownLoadControllerView.downUpStatusChangeBlock = ^(DownUpStatus downUpStatus){
            
            if (weakSelf.downUpStatusBlock) {
                weakSelf.downUpStatusBlock(weakSelf.csFileDownUpFrameModel, downUpStatus);
            }
            
        };
        
        [self.contentView addSubview:fileExtensionView];
        self.fileExtensionView = fileExtensionView;
        
        
        
    }
    
    return self;
}

- (void)initDownUpBlockWithDownUpModel:(DownUpModel)downUpModel
{
    if (!self.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadProgressBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadSuccessBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadFailedBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadProgressBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadSuccessBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadFailedBlock = nil;
    }else{
        
        for (CSFileDownUpModel *csFileDownUpModel in self.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels) {
            
            csFileDownUpModel.uploadProgressBlock = nil;
            csFileDownUpModel.uploadSuccessBlock = nil;
            csFileDownUpModel.uploadFailedBlock = nil;
            csFileDownUpModel.downloadProgressBlock = nil;
            csFileDownUpModel.downloadSuccessBlock = nil;
            csFileDownUpModel.downloadFailedBlock = nil;
            
        }
        
    }
    
    if (downUpModel == DownUpModelDown) {
        [self initDownCallBlock];
    }else [self initUpCallBlock];
    
}

- (void)initDownCallBlock
{
    __weak typeof(self)weakSelf = self;
    
    __weak typeof(self.csFileDownLoadControllerView)weakDowunLoadControllerView = self.csFileDownLoadControllerView;
    
    __weak typeof(self.fileProcessSizeLabel)weakProcessSizeLabel = self.fileProcessSizeLabel;
    
    __weak typeof(self.fileSizeLabel)weakFileSizeLabel = self.fileSizeLabel;
    
    __weak typeof(self.speedLabel)weakSpeedLabel = self.speedLabel;
    
    
    if (!self.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        
        
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress, NSString *router, CSFileDownUpModel *csfileDownUpModel,NSString *uuid) {
            
            if ([uuid isEqualToString:weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpUUID]) {
                
                
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpSize = receivedSize;
                
                
                NSDate *now = [NSDate date];
                
                
                if (!weakSelf.lastRecordTime || [now timeIntervalSinceDate:weakSelf.lastRecordTime] >= 1) {
                    
                    NSInteger speed = receivedSize - weakSelf.lastProcessedDataLength;
                    
                    weakSelf.lastProcessedDataLength = receivedSize;
                    
                    weakSelf.lastRecordTime = now;
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        weakSpeedLabel.text = [NSString stringWithFormat:@"%@/s",[weakSelf bitToKb_MbWithBit:speed]];
                        
                        weakDowunLoadControllerView.percent = progress;
                        
                        weakProcessSizeLabel.text = [NSString stringWithFormat:@"%@/%@",[weakSelf bitToKb_MbWithBit:receivedSize],weakFileSizeLabel.text];
                    });
                    
                }
                
            }
            
            
            
            
        };
        
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadSuccessBlock = ^(NSInteger receivedSize, NSString *localFullFileRouter, CSFileDownUpModel *csfileDownUpModel,NSString *uuid) {
            //operation 将完整的key传出.
            
            if ([uuid isEqualToString:weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpUUID]) {
                
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus = DownUpStatusSuccess;
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpSize = receivedSize;
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.localRootPath = localFullFileRouter;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [weakSelf initData];
                    
                    if (weakSelf.fileDownUpSuccessBlock) {
                        weakSelf.fileDownUpSuccessBlock(weakSelf.csFileDownUpFrameModel);
                    }
                });
            }
        };
        
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadFailedBlock = ^(NSString *errMessage, CSFileDownUpModel *csfileDownUpModel,NSString *uuid) {
            
            if ([uuid isEqualToString:weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpUUID]){
                
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus = DownUpStatusFailure;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    weakProcessSizeLabel.text = weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelDown?@"下载失败":@"上传失败";
                    
                    weakDowunLoadControllerView.downUpStatus = DownUpStatusFailure;
                });
            }
            
        };
        
    }else{
        
        //文件夹
        for (CSFileDownUpModel *csFileDownUpModel in self.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels) {
            
            
            csFileDownUpModel.downloadProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress, NSString *router, CSFileDownUpModel *callBackFileDownUpModel,NSString *uuid) {
                //if subs contail.待新增(待修改外层隔一秒判断一次.)
                
                NSArray *targetFileModelArray = [weakSelf.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"downUpUUID = %@",uuid]];
                
                if (targetFileModelArray.count) {
                    
                    CSFileDownUpModel *subCSFileDownUpModel = targetFileModelArray[0];
                    subCSFileDownUpModel.downUpSize = receivedSize;
                    
                    NSDate *now = [NSDate date];
                    //
                    if (!weakSelf.lastRecordTime || [now timeIntervalSinceDate:weakSelf.lastRecordTime] >= 1) {
                        
                        weakSelf.lastRecordTime = now;
                        
                        [weakSelf getFolderSizeAndPorcessSizeWithResultBlock:^(NSInteger progressSize, NSInteger totalSize) {
                            
                            NSInteger speed = progressSize - weakSelf.lastProcessedDataLength;
                            
                            weakSelf.lastProcessedDataLength = progressSize;
                            
                            dispatch_async(dispatch_get_main_queue(), ^{
                                
                                weakSpeedLabel.text = [NSString stringWithFormat:@"%@/s",[weakSelf bitToKb_MbWithBit:speed]];
                                
                                weakDowunLoadControllerView.percent = progressSize*1.0f/totalSize*1.0f;
                                
                                weakProcessSizeLabel.text = [NSString stringWithFormat:@"%@/%@",[weakSelf bitToKb_MbWithBit:progressSize],[weakSelf bitToKb_MbWithBit:totalSize]];
                            });
                            
                            
                            
                            
                        }];
                        
                    }
                    
                }
                
                
                
                
            };
            
            csFileDownUpModel.downloadSuccessBlock = ^(NSInteger receivedSize, NSString *localFullFileRouter, CSFileDownUpModel *callBackFileDownUpModel,NSString *uuid) {
                //operation 将完整的key传出.
                NSArray *targetFileModelArray = [weakSelf.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"downUpUUID = %@",uuid]];
                
                if (targetFileModelArray.count) {
                    
                    CSFileDownUpModel *subCSFileDownUpModel = targetFileModelArray[0];
                    subCSFileDownUpModel.downUpSize = receivedSize;
                    subCSFileDownUpModel.downUpStatus = DownUpStatusSuccess;
                    
                    [weakSelf getFolderSizeAndPorcessSizeWithResultBlock:^(NSInteger progressSize, NSInteger totalSize) {
                        
                        if (progressSize == totalSize) {
                            
                            weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus = DownUpStatusSuccess;
                            
                            dispatch_async(dispatch_get_main_queue(), ^{
                                
                                [weakSelf initData];
                                
                                if (weakSelf.fileDownUpSuccessBlock) {
                                    weakSelf.fileDownUpSuccessBlock(weakSelf.csFileDownUpFrameModel);
                                }
                            });
                        }
                        
                    }];
                }
            };
            
            csFileDownUpModel.downloadFailedBlock = ^(NSString *errMessage, CSFileDownUpModel *callBackFileDownUpModel,NSString *uuid) {
                
                NSArray *targetFileModelArray = [weakSelf.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"downUpUUID = %@",uuid]];
                
                if (targetFileModelArray.count){
                    
                    CSFileDownUpModel *subCSFileDownUpModel = targetFileModelArray[0];
                    
                    //都失败的话才赋值文件夹状态为error.待新增
                    
                    
                }
                
            };
            
        }
        
    }
    
    //down
    
    
    
    
}

- (void)initUpCallBlock
{
    __weak typeof(self)weakSelf = self;
    
    __weak typeof(self.csFileDownLoadControllerView)weakDowunLoadControllerView = self.csFileDownLoadControllerView;
    
    __weak typeof(self.fileProcessSizeLabel)weakProcessSizeLabel = self.fileProcessSizeLabel;
    
    __weak typeof(self.fileSizeLabel)weakFileSizeLabel = self.fileSizeLabel;
    
    __weak typeof(self.speedLabel)weakSpeedLabel = self.speedLabel;
    
    //up
    if (!weakSelf.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadProgressBlock = ^(double progress, NSInteger sendedSize, CSFileDownUpModel *csFileDownUpModel,NSString *uuid) {
            
#warning 2017-07-03 速度计算方式: 当前时间与上次记录时间差>=1s时,上次的downpSize 与 sendedSize之差就是网速.
            if ([uuid isEqualToString:weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpUUID]){
                
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpSize = sendedSize;
                
                NSDate *now = [NSDate date];
                
                //每秒刷新一次
                if (!weakSelf.lastRecordTime || [now timeIntervalSinceDate:weakSelf.lastRecordTime] >= 1) {
                    
                    NSInteger speed = sendedSize - weakSelf.lastProcessedDataLength;
                    
                    weakSelf.lastProcessedDataLength = sendedSize;
                    
                    weakSelf.lastRecordTime = now;
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        weakSpeedLabel.text = [NSString stringWithFormat:@"%@/s",[weakSelf bitToKb_MbWithBit:speed]];
                        
                        weakDowunLoadControllerView.percent = progress;
                        
                        weakProcessSizeLabel.text = [NSString stringWithFormat:@"%@/%@",[weakSelf bitToKb_MbWithBit:sendedSize],weakFileSizeLabel.text];
                    });
                    
                }
                
            }
            
        };
        
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadSuccessBlock = ^(NSString *url, long long fileSize, CSFileDownUpModel *csFileDownUpModel,NSString *uuid) {
            
            if ([uuid isEqualToString:weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpUUID]){
                
                //通知上层需要移动位置
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpSize = fileSize;
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus = DownUpStatusSuccess;
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.sid = csFileDownUpModel.sid;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [weakSelf initData];
                    
                    if (weakSelf.fileDownUpSuccessBlock) {
                        weakSelf.fileDownUpSuccessBlock(weakSelf.csFileDownUpFrameModel);
                    }
                });
            }
            
        };
        
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadFailedBlock = ^(NSString *errMessage, CSFileDownUpModel *csFileDownUpModel,NSString *uuid) {
            
            if ([uuid isEqualToString:weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpUUID]) {
                
                weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus = DownUpStatusFailure;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    weakProcessSizeLabel.text = weakSelf.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelDown?@"下载失败":@"上传失败";
                    
                    weakDowunLoadControllerView.downUpStatus = DownUpStatusFailure;
                });
            }
            
            
        };
        
    }else{
        
        //上传文件夹暂不处理.
    }
    
}



- (void)extensionSwithOnOff
{
    
    if (self.extensionSwitchBlock) {
        self.extensionSwitchBlock(self.csFileDownUpFrameModel);
    }
}

- (void)setCsFileDownUpFrameModel:(CSFileDownUpFrameModel *)csFileDownUpFrameModel
{
    _csFileDownUpFrameModel = csFileDownUpFrameModel;
    
    [self initData];
    
    [self initDownUpBlockWithDownUpModel:csFileDownUpFrameModel.csFileDownUpModel.downUpType];
    
    [self registerDownUpBlock];
    
}

- (void)initData
{
    
    self.fileNameLabel.text = self.csFileDownUpFrameModel.csFileDownUpModel.name;
    
    self.fileCreateTimeLabel.text = self.csFileDownUpFrameModel.strServerTime;
    
    self.fileSizeLabel.text = self.csFileDownUpFrameModel.fileSizeStr;
    
    //base process/status
    if (self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus == DownUpStatusSuccess) {
        self.fileProcessSizeLabel.hidden = YES;
        self.csFileDownLoadControllerView.hidden = YES;
        self.speedLabel.hidden = YES;
        
        self.fileCreateTimeLabel.hidden = NO;
        self.fileSizeLabel.hidden = NO;
        self.fileExtensionSwitchImageView.hidden = NO;
    }else{
        
        self.fileCreateTimeLabel.hidden = YES;
        self.fileSizeLabel.hidden = YES;
        self.fileExtensionSwitchImageView.hidden = YES;
        
        self.speedLabel.hidden = NO;
        self.fileProcessSizeLabel.hidden = NO;
        self.csFileDownLoadControllerView.hidden = NO;
        
        self.csFileDownLoadControllerView.downUpStatus = self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus;
    }
    
    //header iamge
    if (self.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        
        [self.headerIconImageView setImage:[UIImage imageNamed:@"file_folder_icon"]];
        
        //        self.fileSizeLabel.hidden = YES;
    }else{
        
        [self.headerIconImageView setImage:[UIImage imageNamed:@"file_unknown_icon"]];
        
        //        self.fileSizeLabel.hidden = NO;
    }
    
    
    //animation?
    if (self.csFileDownUpFrameModel.isFileExtensionOP) {
        
        self.fileExtensionView.hidden = NO;
        
        [self.fileExtensionSwitchImageView setImage:[UIImage imageNamed:@"list_Group_Arrow_Normal"] forState:UIControlStateNormal];
        
    }else {
        
        self.fileExtensionView.hidden = YES;
        
        [self.fileExtensionSwitchImageView setImage:[UIImage imageNamed:@"list_Group_Arrow_Down"] forState:UIControlStateNormal];
        
    }
    
    
    self.cellSelected = self.csFileDownUpFrameModel.isSelected;
    
    
    
    if (!self.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        
        self.lastProcessedDataLength = self.csFileDownUpFrameModel.csFileDownUpModel.downUpSize;
        self.lastRecordTime = [NSDate date];
        
        
        //部分计算可以移到frammodel.
        
        if (self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus == DownUpStatusWait) {
            self.fileProcessSizeLabel.text = @"排队等待中...";
        }else if(self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus == DownUpStatusDoing || self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus == DownUpStatusPause){
            self.fileProcessSizeLabel.text = [NSString stringWithFormat:@"%@/%@",[self bitToKb_MbWithBit:self.csFileDownUpFrameModel.csFileDownUpModel.downUpSize],self.fileSizeLabel.text];
        }else if (self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus == DownUpStatusFailure)
        {
            self.fileProcessSizeLabel.text = self.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelDown?@"下载失败":@"上传失败";
        }
        
        self.csFileDownLoadControllerView.percent = self.csFileDownUpFrameModel.csFileDownUpModel.downUpSize * 1.0f/self.csFileDownUpFrameModel.csFileDownUpModel.size * 1.0f;
        
        self.speedLabel.text = nil;
    }else{
        
#warning 2017-07-06 当文件夹下的一个文件下载好时，获取父节点文件夹，再获取所有文件，如果文件都下载完成，则修改文件夹状态为成功，。
        
        self.lastRecordTime = [NSDate date];
        
        self.fileSizeLabel.hidden = YES;
        //TEXT  重新计算
        __weak typeof(self.fileProcessSizeLabel)weakProcessSizeLabel = self.fileProcessSizeLabel;
        __weak typeof(self.csFileDownLoadControllerView)weakControllerView = self.csFileDownLoadControllerView;
        __weak typeof(self)weakself = self;
        
        [self getFolderSizeAndPorcessSizeWithResultBlock:^(NSInteger progressSize, NSInteger totalSize) {
            
            weakProcessSizeLabel.text = [NSString stringWithFormat:@"%@/%@",[self bitToKb_MbWithBit:progressSize],[self bitToKb_MbWithBit:totalSize]];
            weakControllerView.percent = progressSize * 1.0f/totalSize * 1.0f;
            weakself.lastProcessedDataLength = progressSize;
        }];
        
        self.speedLabel.text = nil;
        
    }
    
    
}

- (void)registerDownUpBlock
{
    
    __weak typeof(self)weakself = self;
    
    if (!self.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        if (self.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelUp && self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus != DownUpStatusSuccess) {
            
            [[CloudStorageUploadCenter sharedInstance]registeUploadForUIWithcsFileDownUpModel:self.csFileDownUpFrameModel.csFileDownUpModel ProcessBlock:self.csFileDownUpFrameModel.csFileDownUpModel.uploadProgressBlock successBlock:self.csFileDownUpFrameModel.csFileDownUpModel.uploadSuccessBlock failBlock:self.csFileDownUpFrameModel.csFileDownUpModel.uploadFailedBlock];
            
            //需要initDownUpCallBlock.并且区分..down和up模式
            
        }else if(self.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelDown && self.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus != DownUpStatusSuccess)
        {
            
            [[CloudStorageDownloadCenter sharedInstance]registeDownloadBlockForUIWithcsFileDownUpModel:self.csFileDownUpFrameModel.csFileDownUpModel ProcessBlock:self.csFileDownUpFrameModel.csFileDownUpModel.downloadProgressBlock successBlock:self.csFileDownUpFrameModel.csFileDownUpModel.downloadSuccessBlock failBlock:self.csFileDownUpFrameModel.csFileDownUpModel.downloadFailedBlock];
            
        }
    }else{
        
        NSMutableArray *processModelArray = [NSMutableArray array];
        //应该获取所有子文件
        
        for (CSFileDownUpModel *downUpModel in self.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels) {
            if (downUpModel.downUpStatus != DownUpStatusSuccess && downUpModel.downUpSize != downUpModel.size) {
                
                [processModelArray addObject:downUpModel];
            }
            
        }
        
        if (weakself.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelUp && weakself.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus != DownUpStatusSuccess) {
            
            if (processModelArray.count)
            {
                for (CSFileDownUpModel *downUpModel in processModelArray) {
                    
                    [[CloudStorageUploadCenter sharedInstance]registeUploadForUIWithcsFileDownUpModel:downUpModel ProcessBlock:downUpModel.uploadProgressBlock successBlock:downUpModel.uploadSuccessBlock failBlock:downUpModel.uploadFailedBlock];
                    
                }
            }
            
            
            //需要initDownUpCallBlock.并且区分..down和up模式
            
        }else if(weakself.csFileDownUpFrameModel.csFileDownUpModel.downUpType == DownUpModelDown && weakself.csFileDownUpFrameModel.csFileDownUpModel.downUpStatus != DownUpStatusSuccess)
        {
            
            if (processModelArray.count){
                
                for (CSFileDownUpModel *downUpModel in processModelArray) {
                    
                    [[CloudStorageDownloadCenter sharedInstance]registeDownloadBlockForUIWithcsFileDownUpModel:downUpModel ProcessBlock:downUpModel.downloadProgressBlock successBlock:downUpModel.downloadSuccessBlock failBlock:downUpModel.downloadFailedBlock];
                    
                }
                
            }
            
        }
        
    }
    
    
    
}


- (void)getFolderSizeAndPorcessSizeWithResultBlock:(void(^)(NSInteger progressSize, NSInteger totalSize))resultBlock
{
    
    
    NSInteger totalSize = 0;
    NSInteger progressSize = 0;
    for (CSFileDownUpModel *downUpModel in self.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels) {
        
        totalSize += downUpModel.size;
        progressSize += downUpModel.downUpSize;
        
    }
    
    if (resultBlock) {
        resultBlock(progressSize,totalSize);
    }
    
    
}

- (NSString *)bitToKb_MbWithBit:(NSInteger)bit
{
#warning show = mb,,,true = kb
    double downLoadedSize = bit * 1.00f/1024 * 1.00f;
    NSString *downloadSizeStr = nil;
    
    if (downLoadedSize>1024 * 1024) {
        CGFloat dSize = downLoadedSize / (1024 * 1024 * 1.0f);
        
        downloadSizeStr = [NSString stringWithFormat:@"%.2lf Gb",floor(dSize*100) / 100 ];
        
    }else if (downLoadedSize>1024) {
        CGFloat dSize = downLoadedSize / 1024 * 1.0f;
        
        downloadSizeStr = [NSString stringWithFormat:@"%.2lf Mb",floor(dSize*100) / 100 ];
        
    }else
    {
        downloadSizeStr = [NSString stringWithFormat:@"%.2lf Kb",floor(downLoadedSize*100) / 100];
        
    }
    
    return downloadSizeStr;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _headerIconImageView.frame = self.csFileDownUpFrameModel.headerIconImageViewFrame;
    
    _fileNameLabel.frame = self.csFileDownUpFrameModel.fileNameLabelFrame;
    
    _fileCreateTimeLabel.frame = self.csFileDownUpFrameModel.fileCreateTimeLabelFrame;
    
    _fileProcessSizeLabel.frame = self.csFileDownUpFrameModel.fileProcessSizeLabelFrame;
    
    _fileExtensionSwitchImageView.frame = self.csFileDownUpFrameModel.fileExtensionSwithImageViewFrame;
    
    _csFileDownLoadControllerView.frame = self.csFileDownUpFrameModel.fileProcessControllerViewFrame;
    
    _fileSizeLabel.frame = self.csFileDownUpFrameModel.fileSizeLabelFrame;
    
    _speedLabel.frame = self.csFileDownUpFrameModel.fileDownUpSpeedLabelFrame;
    
    //animation?
    __weak typeof(self)weakself = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakself.fileExtensionView.frame = weakself.csFileDownUpFrameModel.fileExtensionViewFrame;
    }];
    
}



- (void)dealloc
{
    if (!self.csFileDownUpFrameModel.csFileDownUpModel.isdir) {
        
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadProgressBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadSuccessBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.uploadFailedBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadProgressBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadSuccessBlock = nil;
        self.csFileDownUpFrameModel.csFileDownUpModel.downloadFailedBlock = nil;
        
    }else{
        
        for (CSFileDownUpModel *csFileDownUpModel in self.csFileDownUpFrameModel.csFileDownUpModel.subFileDownUpModels) {
            
            csFileDownUpModel.uploadProgressBlock = nil;
            csFileDownUpModel.uploadSuccessBlock = nil;
            csFileDownUpModel.uploadFailedBlock = nil;
            csFileDownUpModel.downloadProgressBlock = nil;
            csFileDownUpModel.downloadSuccessBlock = nil;
            csFileDownUpModel.downloadFailedBlock = nil;
            
        }
        
    }
    
}



@end




