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

#import "downloadThumbnailManager.h"

@implementation downloadThumbnailManager
static downloadThumbnailManager * cur_downloadFileShareInstance = nil;
+(downloadThumbnailManager *)shareInstance;
{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        cur_downloadFileShareInstance = [[downloadThumbnailManager alloc] init];
        
    });
    
    return cur_downloadFileShareInstance;
}

- (id)init
{
    self = [super init];
    
    if (self) {
        //[self initManager];
    }
    
    return self;
}

- (void)handlToDownloadThumbnailWith:(NSArray*)Arr
{
    KWeakSelf
    //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [couldPhoneFileModel bg_findAsync:downloadThumbnail_tableName limit:0 orderBy:nil desc:NO complete:^(NSArray * _Nullable array) {
            [weakSelf checkTaskRepeatWithHadTask:array withNewTask:Arr];
        }];
    //});
}

-(void)checkTaskRepeatWithHadTask:(NSArray*)hadTaskArr withNewTask:(NSArray*)newTaskArr
{
    NSMutableArray *curTaskArr = [NSMutableArray arrayWithArray:hadTaskArr];
    
    //for (couldPhoneFileModel *addModel in newTask)
    
    //倒序遍历
    [newTaskArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //NSLog(@"%ld----",idx);
        
        couldPhoneFileModel *addModel = newTaskArr[idx];
        addModel.bg_tableName = downloadThumbnail_tableName;
        
        NSString *newFileName =  [addModel getFileNameFun];
        BOOL isNeedAdd = YES;
        
        for (couldPhoneFileModel *hadModel in hadTaskArr) {
            NSString *hadFileName =  [addModel getFileNameFun];
            
            if([newFileName isEqualToString:hadFileName]){
                isNeedAdd = NO;
                break;
            }
        }
        
        [curTaskArr addObject:addModel];
    }];
   
    
    KWeakSelf
    [couldPhoneFileModel bg_saveOrUpdateArrayAsync:curTaskArr complete:^(BOOL isSuccess) {
        [weakSelf planToDownloadThumbnailFun];
    }];
}

- (void)planToDownloadThumbnailFun
{
    KWeakSelf
    [couldPhoneFileModel bg_findAsync:downloadThumbnail_tableName limit:0 orderBy:nil desc:NO complete:^(NSArray * _Nullable array) {
        [weakSelf checkCouldPhoneFileModelTaskStateBy:array];
    }];
}

- (void)checkCouldPhoneFileModelTaskStateBy:(NSArray*)array
{
    NSMutableArray *taskArr = [NSMutableArray new];
    
    for (couldPhoneFileModel*curModel in array) {
        if(curModel.curDownloadStateType != downloadStateDone){
            [taskArr addObject:curModel];
        }
    }
    
    _totalTaskArr = taskArr;
}

- (void)beginDownloadThumbnailFun
{
    if (_totalTaskArr.count >0) {
        _curDownloadFileModel = _totalTaskArr.firstObject;
    }
    else{
        return;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:downloadThumbnailBeginNotification object:_curDownloadFileModel];
}

@end
