//
//  CSFileDownUpFrameModel.m
//  zhuxun
//
//  Created by winsoft on 17/6/23.
//
//

#import "CSFileDownUpFrameModel.h"
#import "NSString+Additions.h"
#import "NSDate+Helper.h"

#define FatherViewMaxWidth SCREEN_WIDTH

//SIZE
#define HeaderIconMarginX  5
#define HeaderIconMarginY  10
#define HeaderIconWH 40

#define FileNameMarginHeaderIcon 5
#define FileNameMarginY HeaderIconMarginY

#define ExtensionSwitchMarginFileName 20
#define ExtensionSwitchMarginX_Right 20
#define ExtensionSwitchW 32
#define ExtensionSwitchH 22

#define FileSizeMaxW 150

#define FileSpeedSizeMaxW 100



#define FileCreateTimeMarginFileName 5
#define FileCreateTimeMarginBottom FileNameMarginY
@interface CSFileDownUpFrameModel()
@property (nonatomic , assign, readonly) CGFloat cellHeight_unExtension;
@property (nonatomic , assign, readonly) CGFloat cellHeight_extension;
@end
@implementation CSFileDownUpFrameModel
- (instancetype)initWithCSFileDownUpModel:(CSFileDownUpModel *)csFileDownUpModel
{
    if (self = [super init]) {
        
        _csFileDownUpModel = csFileDownUpModel;
        
        [self setData];
        
        [self setFrame];
    }
    return self;
}


- (void)setData
{
    _strServerTime = [NSDate stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.csFileDownUpModel.downUpTimeStamp/1000] withFormat:@"yyyy-MM-dd HH:mm"];
    
    _strUpdateTime = [NSDate stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.csFileDownUpModel.downUpTimeStamp /1000] withFormat:@"yyyy-MM-dd HH:mm"];
    
    _fileSizeStr = [self bitToKb_MbWithBit:self.csFileDownUpModel.size];
}

- (void)setFrame
{
    //x 基于 headericon
    //select的话是否需要重置.
    //header
    _headerIconImageViewFrame = CGRectMake(HeaderIconMarginX, HeaderIconMarginY, HeaderIconWH, HeaderIconWH);
    
    CGFloat fileNameMaxLength = FatherViewMaxWidth - 2 * HeaderIconMarginX - HeaderIconWH - ExtensionSwitchMarginFileName - ExtensionSwitchMarginX_Right - ExtensionSwitchW;
    CGSize fileNameSize = [self.csFileDownUpModel.name stringgetSizebyMaxWidth:fileNameMaxLength Font:FileNameFont];
    
    //name
    _fileNameLabelFrame = CGRectMake(CGRectGetMaxX(_headerIconImageViewFrame) + FileNameMarginHeaderIcon, FileNameMarginY, fileNameSize.width, fileNameSize.height);
    
    CGSize fileTimeSize = [self.strServerTime stringgetSizebyMaxWidth:fileNameMaxLength Font:FileCreateTimeFont];
    
    //time
    _fileCreateTimeLabelFrame = CGRectMake(_fileNameLabelFrame.origin.x, CGRectGetMaxY(_fileNameLabelFrame) + FileCreateTimeMarginFileName, fileTimeSize.width, fileTimeSize.height);
    
    _fileProcessSizeLabelFrame = CGRectMake(_fileNameLabelFrame.origin.x, CGRectGetMaxY(_fileNameLabelFrame) + FileCreateTimeMarginFileName, 150, fileTimeSize.height);;
    
    //extensionSwitch
    //    _fileExtensionSwithImageViewFrame = CGRectMake(FatherViewMaxWidth - ExtensionSwitchW - ExtensionSwitchMarginX_Right, _fileNameLabelFrame.origin.y + 10, ExtensionSwitchW, ExtensionSwitchH);
    _fileExtensionSwithImageViewFrame = CGRectMake(FatherViewMaxWidth - ExtensionSwitchW - ExtensionSwitchMarginX_Right, 0, ExtensionSwitchW, CGRectGetMaxY(_fileCreateTimeLabelFrame));
    
    _fileProcessControllerViewFrame = CGRectMake(FatherViewMaxWidth - ControllViewWH - ExtensionSwitchMarginX_Right, _fileNameLabelFrame.origin.y, ControllViewWH, ControllViewWH);
    

    //size
    //size
    CGSize fileSize = [self.fileSizeStr stringgetSizebyMaxWidth:FileSizeMaxW Font:FileCreateTimeFont];
    
    _fileSizeLabelFrame = CGRectMake(CGRectGetMinX(_fileExtensionSwithImageViewFrame) - fileSize.width, _fileCreateTimeLabelFrame.origin.y, fileSize.width, fileSize.height);
    
    _fileDownUpSpeedLabelFrame = CGRectMake(CGRectGetMinX(_fileProcessControllerViewFrame) - FileSpeedSizeMaxW, _fileCreateTimeLabelFrame.origin.y, FileSpeedSizeMaxW, fileSize.height);
    
    //fileextensionView
    _fileExtensionViewFrame = CGRectMake(0, CGRectGetMaxY(_fileCreateTimeLabelFrame) + FileCreateTimeMarginBottom, FatherViewMaxWidth, FileExtensionViewH);
    
    //eight
    
    _cellHeight_unExtension = CGRectGetMaxY(_fileCreateTimeLabelFrame) + FileCreateTimeMarginBottom;
    
    _cellHeight_extension = CGRectGetMaxY(_fileExtensionViewFrame);
}

- (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)setIsFileExtensionOP:(BOOL)isFileExtensionOP
{
    _isFileExtensionOP = isFileExtensionOP;
    
    if (isFileExtensionOP) {
        _fileExtensionViewFrame = CGRectMake(0, CGRectGetMaxY(_fileCreateTimeLabelFrame) + FileCreateTimeMarginBottom, FatherViewMaxWidth, FileExtensionViewH);
    }else _fileExtensionViewFrame = CGRectMake(0, CGRectGetMaxY(_fileCreateTimeLabelFrame) + FileCreateTimeMarginBottom, FatherViewMaxWidth, 0);;
}

- (CGFloat)cellHeight
{
    if (_isFileExtensionOP) {
        return ceil(_cellHeight_extension);
    }else return  ceil(_cellHeight_unExtension);
}

@end
