//
//  CSFileCell.m
//  zhuxun
//
//  Created by winsoft on 17/6/16.
//
//

#import "CSFileCell.h"

@interface CSFileCell()
@property (nonatomic , weak) UIImageView *headerIconImageView;
@property (nonatomic , weak) UILabel *fileNameLabel;
@property (nonatomic , weak) UILabel *fileCreateTimeLabel;
@property (nonatomic , weak) UILabel *fileSizeLabel;
@property (nonatomic , weak) UIButton *fileExtensionSwitchImageView;
@property (nonatomic , weak) CSFileExtensionViewForCell *fileExtensionView;
@end
@implementation CSFileCell
+ (CSFileCell *)fileCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath identifier:(NSString *)identifier opItems:(NSArray *)opItems
{
    CSFileCell *fileCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!fileCell) {
        fileCell = [[CSFileCell alloc]initFileCellWithTableView:tableView indexPath:indexPath identifier:identifier opItems:opItems];
    }
    //csFileCell.indexPath = indexPath

    return fileCell;
}

- (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 *fileSizeLabel = [[UILabel alloc]init];
        fileSizeLabel.textAlignment = NSTextAlignmentRight;
        fileSizeLabel.textColor = LineForSubLineViewColor;
        fileSizeLabel.font = FileCreateTimeFont;
        [self.contentView addSubview:fileSizeLabel];
        self.fileSizeLabel = fileSizeLabel;
        
        
        
        if (opItems) {
            
            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;
            
            CSFileExtensionViewForCell *fileExtensionView = [[CSFileExtensionViewForCell alloc]initWithItmes: opItems];
            
            __weak typeof(self)weakSelf = self;
            fileExtensionView.extensionOpBlock = ^(ExtensionOP extensionOP,CSFileFrameModel *csFileFrameModel){
                
                if (weakSelf.extensionOpBlock) {
                    weakSelf.extensionOpBlock(extensionOP, weakSelf.csFileFrameModel);
                }
                
            };
            
            [self.contentView addSubview:fileExtensionView];
            self.fileExtensionView = fileExtensionView;
        }
       
    }
    
    return self;
}

- (void)setCsFileFrameModel:(CSFileFrameModel *)csFileFrameModel
{
    _csFileFrameModel = csFileFrameModel;
    
    [self initData];
    
    [self setNeedsLayout];
}

- (void)initData
{
    //header iamge
    if (self.csFileFrameModel.fileModel.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;
    }
    
    self.fileNameLabel.text = self.csFileFrameModel.fileModel.name;
    
    self.fileCreateTimeLabel.text = self.csFileFrameModel.strServerTime;
    
    double  kbSize = self.csFileFrameModel.fileModel.size * 1.00f/1024 * 1.00f;
    
    
    if (kbSize>1024) {
        CGFloat mSize = kbSize / 1024 * 1.0f;
        
        self.fileSizeLabel.text = [NSString stringWithFormat:@"%.2lfM",floor(mSize*100) / 100 ];
        
    }else
    {
        self.fileSizeLabel.text = [NSString stringWithFormat:@"%.2lfkb",floor(kbSize*100) / 100];
        
    }
        
    //animation?
    if (self.csFileFrameModel.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];

    }
    
    if (self.allowsMultipleSelection) {
        self.fileExtensionSwitchImageView.hidden = YES;
    }else self.fileExtensionSwitchImageView.hidden = NO;
    
    self.cellSelected = self.csFileFrameModel.isSelected;
    

    
}

- (void)extensionSwithOnOff
{

    if (self.extensionSwitchBlock) {
        self.extensionSwitchBlock(self.csFileFrameModel);
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _headerIconImageView.frame = self.csFileFrameModel.headerIconImageViewFrame;
    
    _fileNameLabel.frame = self.csFileFrameModel.fileNameLabelFrame;
    
    _fileCreateTimeLabel.frame = self.csFileFrameModel.fileCreateTimeLabelFrame;
    
   
    
    _fileSizeLabel.frame = self.csFileFrameModel.fileSizeLabelFrame;
    
    //animation?
    __weak typeof(self)weakself = self;
    if(self.fileExtensionView){
        
         _fileExtensionSwitchImageView.frame = self.csFileFrameModel.fileExtensionSwithImageViewFrame;
        
        [UIView animateWithDuration:0.5 animations:^{
            weakself.fileExtensionView.frame = weakself.csFileFrameModel.fileExtensionViewFrame;
        }];
    }
    
    
}
@end
