//
//  backupsFilerecordTableView.m
//  隐私保护
//
//  Created by xd h on 2024/1/4.
//

#import "backupsFilerecordTableView.h"

#import "UIScrollView+EmptyDataSet.h"
#import "backupsFileManager.h"
#import "backupsFileRecordCell.h"
#import "AFNetworkReachabilityManager.h"

@interface backupsFilerecordTableView()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    UIView* curTableHeadView;
    UILabel *titleHeadLabel;
}


@end

@implementation backupsFilerecordTableView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initCommon];
    
    }
    return self;
}

- (void)initCommon {
    self.delegate = self;
    self.dataSource = self;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    [self setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    [self setSeparatorColor:[UIColor clearColor]];
    [self setBackgroundColor:[UIColor clearColor]];
    [self setTableFooterView:[UIView new]];
    [self setBounces:YES];
    if (@available(iOS 15.0, *)) {
        self.sectionHeaderTopPadding = 0;
    }
    
    self.scrollEnabled = NO;
    
    //空数据引入第三方开源处理
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
}


- (void)setPhotosBackupsIngTaskModel:(photosBackupsTaskModel *)photosBackupsIngTaskModel
{
    _photosBackupsIngTaskModel = photosBackupsIngTaskModel;
    NSArray *curArr = @[_photosBackupsIngTaskModel];
    _curDataArr = curArr;
    [self reloadDataFun];
}

- (void)setTableViewHeadTitleFun
{
    if(_curDataArr.count == 0){
        titleHeadLabel.text = @"";
        return;
    }
    
    photosBackupsTaskModel *model = _curDataArr.firstObject;
    
    NSString *text = @"";
    if(model.curBackupsState == backupsStateUploading
       ||model.curBackupsState == backupsStateSuspend){
        text = NSLocalizedString(@"File_Transfer_List_head_title_ing",nil);
        text = [[NSString alloc] initWithFormat:@"%@(%ld)",text,model.count - model.didBackupsCount - model.failCount];
    }
    else if(model.curBackupsState == backupsStateFail){
        text = NSLocalizedString(@"File_Transfer_List_head_title_fail",nil);
        text = [[NSString alloc] initWithFormat:@"%@(%ld)",text,model.didBackupsCount];
    }
    else{
        text = NSLocalizedString(@"File_Transfer_List_head_title_done",nil);
        text = [[NSString alloc] initWithFormat:@"%@(%ld)",text,model.didBackupsCount];
    }

    titleHeadLabel.text = text;
}

- (void)setCurDataArr:(NSArray *)curDataArr
{
    _curDataArr = curDataArr;
    [self reloadDataFun];
}

#pragma mark - 列表委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;//_curDataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    if(_curDataArr.count > section){
//        NSArray *curArr = _curDataArr[section];
//        return curArr.count;
//    }
    
    return _curDataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    //NSInteger section = indexPath.section;
    static NSString *identifier = @"backupsFileRecordCell";
    
    backupsFileRecordCell * cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell){
        cell = [[backupsFileRecordCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setAccessoryType:(UITableViewCellAccessoryNone)];
    }
    
    if(row < _curDataArr.count){
        photosBackupsTaskModel *model = _curDataArr[row];
        cell.curPhotosBackupsTaskModel = model;

        
        KWeakSelf
        cell.didLongPressClick = ^{
            //[weakSelf didLongPressClickFun];
        };

        cell.didClckSelectBut = ^(BOOL isSelect) {
            //[weakSelf selectModelOneByOne:model BySelect:isSelect];
        };

        cell.didTapPressClick = ^{
            if(model.curBackupsState == backupsStateUploading){
                [weakSelf handleBackupsingStateTapFunBy:YES with:model];
            }
            else if(model.curBackupsState == backupsStateSuspend){
                [weakSelf handleBackupsingStateTapFunBy:NO with:model];
            }
            
        };
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView* headView = [UIView new];
    headView.backgroundColor = [UIColor whiteColor];
    titleHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_W -15, 50)];
    titleHeadLabel.font = [UIFont systemFontOfSize:12.0];
    titleHeadLabel.textColor = [UIColor hwColor:@"#666666" alpha:1.0];
    [headView addSubview:titleHeadLabel];
    
    [self setTableViewHeadTitleFun];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark 空数据
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *imageName = @"uploadFile_noData";
    
    if(self.tag == 101){
        imageName = @"uploadFile_noData";
    }
    else if(self.tag == 102){
        imageName = @"uploadFile_noData";
    }
    
    return [UIImage imageNamed:imageName];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = NSLocalizedString(@"File_upload_Record_no_data",nil);
    
    if(self.tag == 101){
        text = NSLocalizedString(@"File_download_Record_no_data",nil);
    }
    else if(self.tag == 102){
        text = NSLocalizedString(@"File_backups_Record_no_data",nil);
    }
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: HW999999Color};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

//调整图片位置
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -150;
}

-(void)reloadDataFun{
    mainBlock(^{
        [self setTableViewHeadTitleFun];
        [self reloadData];
    });
}

#pragma mark cell长按时间
- (void)didLongPressClickFun{
        if(self->_didLongPressClick){
            self->_didLongPressClick();
        }
}


#pragma mark 处理上传中的 状态点击事件
- (void)handleBackupsingStateTapFunBy:(BOOL)isSuspendType  with:(photosBackupsTaskModel*)model
{
    if (isSuspendType) {
        model.isBackupsSuspendType = backupsSuspendByUser;
        model.curBackupsState = backupsStateSuspend;
        
        [[backupsFileManager shareInstance] suspendBackupsFileFun];
    }
    else{
        BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
        if(!haveOpenBackups){
            [[iToast makeText:NSLocalizedString(@"File_Record_backups_set_close_tip",nil)] show];
            [self reloadData];
            return;
        }
           
        //相册权限
        if (![[TZImageManager manager] authorizationStatusAuthorized]){
            [[iToast makeText:NSLocalizedString(@"File_Record_backups_photo_close_tip",nil)] show];
            [self reloadData];
            return;
        }
        
        BOOL isCanUseCellular = [HWDataManager getBoolWithKey:Const_file_backups_canUse_Cellular];

        if(!isCanUseCellular){//不允许流量备份
            //
            if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
                [[iToast makeText:NSLocalizedString(@"backups_cannot_use_WWAN_tip",nil)] show];
                [self reloadData];
                return;
            }
            
        }
        
        [[backupsFileManager shareInstance] reBackupsFileFunBy:model];
    }
    
}

#pragma mark 处理上传中的 状态点击事件
- (void)handleUploadFailStateWith:(uploadFileDataModel*)model{
    
//    [_curDataArr removeObject:model];
//    [self reloadDataFun];
//
//    if(_didClickReUploadBlock){
//        _didClickReUploadBlock(model);
//    }
//
//    NSMutableArray*arr = [NSMutableArray new];
//    model.curUploadStateType = uploadStateWait;
//    [arr addObject:model];
//    [[uploadFileManager shareInstance] reUploadFileFunBy:arr];
    
}
@end
