//
//  downLoadPreViewCell.m
//  隐私保护
//
//  Created by xd h on 2024/1/8.
//

#import "downLoadPreViewCell.h"


@implementation downLoadPreViewCell
@synthesize cellBgView;
@synthesize mImageView;
@synthesize titleLabel;
@synthesize titleLabel2;
@synthesize rightImage;
@synthesize lineView;
@synthesize checkButton;

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        [self drawView];
    }
    
    return self;
}

- (void)drawView
{
    cellBgView = [[UIView alloc] init];
    [self.contentView addSubview:cellBgView];
    cellBgView.backgroundColor = [UIColor whiteColor];
    [cellBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15.f);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_W - 2*15.f);
    }];
    
    /*图片*/
    mImageView = [[UIImageView alloc] init];
    [mImageView setBackgroundColor:[UIColor clearColor]];
    mImageView.image = [UIImage imageNamed:@"uploadFile_image"];
    [cellBgView addSubview:mImageView];
    [mImageView setContentMode:(UIViewContentModeScaleAspectFill)];
    [mImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(36);
        make.centerY.equalTo(cellBgView.mas_centerY);
    }];
    
    /*右侧箭头*/
    rightImage = [[UIImageView alloc] init];
    [rightImage setBackgroundColor:[UIColor clearColor]];
    [rightImage setImage:[UIImage imageNamed:@"cell_right_access"]];
    [cellBgView addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(28);
        make.right.mas_equalTo(-15.f);
        make.height.mas_equalTo(28);
        make.centerY.equalTo(cellBgView.mas_centerY);
    }];
    
    checkButton = [[UIButton alloc] init];
    [checkButton setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
    [checkButton setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
    [cellBgView addSubview:checkButton];
    
    [checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-18.f);
        make.centerY.equalTo(cellBgView.mas_centerY);
        make.width.mas_equalTo(36.f);
        make.height.mas_equalTo(36.f);
    }];
    
    [checkButton addTarget:self action:@selector(maskSwitchPressed:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    /**标题*/
    titleLabel = [[UILabel alloc] init];
    [cellBgView addSubview:titleLabel];
    titleLabel.font = [UIFont boldSystemFontOfSize:14.f];
    titleLabel.numberOfLines = 3;
    [titleLabel setTextColor:HW0A132BColor];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(mImageView.mas_right).offset(5.f);
        make.right.equalTo(checkButton.mas_left).offset(-5.f);
        make.centerY.equalTo(cellBgView.mas_centerY);
        //make.top.mas_equalTo(15.f);
    }];
    
    /**副标题*/
    titleLabel2 = [[UILabel alloc] init];
    [cellBgView addSubview:titleLabel2];
    titleLabel2.font = [UIFont systemFontOfSize:12.f];
    //[titleLabel2 setTextAlignment:(NSTextAlignmentRight)];
    [titleLabel2 setTextColor:HW666666Color];
    [titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-53.f);
        make.left.mas_equalTo(titleLabel.mas_left);
        //make.centerY.equalTo(cellBgView.mas_centerY);
        make.bottom.mas_equalTo(-15);
    }];
    
    lineView = [[UIView alloc] init];
    [lineView setBackgroundColor:HW979797Color10];
    [cellBgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15.f);
        make.left.mas_equalTo(15.f);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1.f);
    }];
    
    
    //titleLabel2.text = @"2023/10/22";
}

- (void)maskSwitchPressed:(UIButton *)maskSwitch{
    if(_didClickSwitch){
        _didClickSwitch(maskSwitch.selected);
    }
}

- (void)setCurFileModel:(couldPhoneFileModel *)curFileModel
{
    mImageView.image = [UIImage imageNamed:@"uploadFile_image"];
    checkButton.selected = curFileModel.isSelectType;

    
    //时间戳转换为日期
    NSString *timeStr = [[NSString alloc] initWithFormat:@"%ld",curFileModel.time];
    NSString *dateStr = [iTools getDateStringWithTimeStr:timeStr];
    
    NSString * totalSizeStr = nil;
    NSInteger totalSize_k = curFileModel.length / 1024;
    if(totalSize_k < 1024){
        totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
    }
    else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
        totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
        
    }
    else{
        totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
    }
    
    NSString * leftStr = curFileModel.name;
    NSString * rightStr = [[NSString alloc] initWithFormat:@"\n%@      %@",dateStr,totalSizeStr];
    
    
    NSString *totalStr = [[NSString alloc] initWithFormat:@"%@%@",leftStr,rightStr];
    
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    
    NSRange redRange = NSMakeRange([totalStr rangeOfString:rightStr].location, [totalStr rangeOfString:rightStr].length);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor hwColor:@"#666666" alpha:1.0] range:redRange];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:redRange];
    // 设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];        //设置行间距
    
    [noteStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr  length])];
    
    
    titleLabel.attributedText = noteStr;
}

@end
