//
//  uploadFileRecordTableView.m
//  隐私保护
//
//  Created by xd h on 2023/11/22.
//

#import "uploadFileRecordTableView.h"
#import "uploadFileRecordCell.h"
#import "UIScrollView+EmptyDataSet.h"
#import "uploadFileManager.h"

@interface uploadFileRecordTableView()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic,strong) NSMutableArray *selectModelArr;
@end

@implementation uploadFileRecordTableView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initCommon];
        _selectModelArr = [NSMutableArray new];
        _curDataArr = [NSMutableArray new];
    }
    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.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if(_curDataArr){
        return _curDataArr.count;
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    static NSString *identifier = @"uploadFileRecordCell";
    
    uploadFileRecordCell * cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell){
        cell = [[uploadFileRecordCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setAccessoryType:(UITableViewCellAccessoryNone)];
    }
    
    
    NSMutableArray *curArr = _curDataArr;
    
    if(row < curArr.count){
        uploadFileDataModel *model = curArr[row];
        cell.curUploadFileDataModel = model;
        cell.isEditType = _isEditType;

        if(_isEditType){
            if([_selectModelArr containsObject:model]){
                cell.isSelectType = YES;
            }
            else{
                cell.isSelectType = NO;
            }
        }
        
        KWeakSelf
        cell.didLongPressClick = ^{
            [weakSelf didLongPressClickFun];
        };

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

        cell.didTapPressClick = ^{
            if(model.curUploadStateType == uploadStateUploading){
                [weakSelf handleUploadingStateTapFunBy:YES with:model];
            }
            else if(model.curUploadStateType == uploadStateSuspend){
                [weakSelf handleUploadingStateTapFunBy:NO with:model];
            }
            else if(model.curUploadStateType == uploadStateFail){
                [weakSelf handleUploadFailStateWith:model];
            }
        };
    }
    
    

    return cell;
}

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


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

#pragma mark 空数据
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return [UIImage imageNamed:@"uploadFile_noData"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = NSLocalizedString(@"File_upload_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 RefresHeadUIFun];
        [self reloadData];
    });
}

#pragma mark 熟悉头部数据
-(void)RefresHeadUIFun
{
    uploadFileRecordTableViewHeadView *headView = (uploadFileRecordTableViewHeadView *)self.tableHeaderView;
    
    if(!headView || ![headView isKindOfClass:[uploadFileRecordTableViewHeadView class]]){
        return;
    }
    
    if(!_curDataArr ||_curDataArr.count ==0){
        headView.hidden = YES;
        return;
    }
   
    headView.hidden = NO;

    NSInteger tag = self.tag;
    
    if(tag == 100){
        NSString *leftStr =  NSLocalizedString(@"File_upload_Record_uploading",nil);
        NSString *rightStr =  NSLocalizedString(@"File_upload_Record_all_suspend",nil);
        NSString *rightSelectStr =  NSLocalizedString(@"File_upload_Record_all_open",nil);
        NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_curDataArr.count];

        headView.titleLabel.text = titleStr;
        [headView.rightButton setTitle:rightStr forState:UIControlStateNormal];
        [headView.rightButton setTitle:rightSelectStr forState:UIControlStateSelected];
        
    }
    else if(tag == 101){
        NSString *leftStr =  NSLocalizedString(@"File_upload_Record_did_upload",nil);
        NSString *rightStr =  NSLocalizedString(@"File_upload_Record_clear_Record",nil);
        NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_curDataArr.count];

        headView.titleLabel.text = titleStr;
        [headView.rightButton setTitle:rightStr forState:UIControlStateNormal];
    }
    else if(tag == 102){
        NSString *leftStr =  NSLocalizedString(@"File_upload_Record_did_upload",nil);
        NSString *rightStr =  NSLocalizedString(@"File_upload_Record_clear_Record",nil);
        NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_curDataArr.count];

        headView.titleLabel.text = titleStr;
        [headView.rightButton setTitle:rightStr forState:UIControlStateNormal];
    }
}

#pragma mark  点击全选
- (void)setIsSelectAllType:(BOOL)isSelectAllType
{
    _selectModelArr = [NSMutableArray arrayWithArray:_curDataArr];
    [self reloadDataFun];
}

-(void)setIsEditType:(BOOL)isEditType
{
    _isEditType = isEditType;
    
    if(_isEditType){
        //_bgScrollV.scrollEnabled = NO;
    }
    else{
        //_bgScrollV.scrollEnabled = YES;
        [_selectModelArr removeAllObjects];
        
    }
    
    [self reloadDataFun];
}

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

#pragma mark 单个点击选中 取消
- (void)selectModelOneByOne:(uploadFileDataModel*)model BySelect:(BOOL)isSelcet
{
    if(isSelcet){
        [_selectModelArr addObject:model];
    }
    else{
        [_selectModelArr removeObject:model];
    }
}

#pragma mark 单个点击选中后删除
- (void)deleteModelOneByOneFun{
    if(!_selectModelArr || _selectModelArr.count==0){
        
        return;
    }
    
    KWeakSelf
    [[uploadFileManager shareInstance] deleteUploadFileRecordBy:_selectModelArr complete:^(BOOL isSuccess) {
        HLog(@"isSuccess:%d",isSuccess);
        
        if(isSuccess){
            NSMutableArray *curArr = self->_curDataArr;
            [curArr removeObjectsInArray:self->_selectModelArr];
            [weakSelf reloadDataFun];
            self->_selectModelArr = [NSMutableArray new];
        }
    }];
}


#pragma mark 处理上传中的 状态点击事件
- (void)handleUploadingStateTapFunBy:(BOOL)isSuspendType with:(uploadFileDataModel*)model
{
    if (isSuspendType) {
        [[uploadFileManager shareInstance] suspendUploadFileFun:NO];
    }
    else{
        NSMutableArray*arr = [NSMutableArray new];
        [arr addObject:model];
        [[uploadFileManager shareInstance] reUploadFileFunBy:arr];
    }
    
}

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