






#import "FaxRetMilesJob.h"

#define mustYet(bit) __weak typeof(bit) weak##bit = bit;
#define birthPipe(bit) __strong typeof(bit) bit = weak##bit;

@interface FaxRetMilesJob()

@property (nonatomic,strong) NSURLSession *dayDigestKey;

@end

@implementation FaxRetMilesJob


+ (instancetype)shared {
    static FaxRetMilesJob *shared = nil;
    static dispatch_once_t redoToken;
    dispatch_once(&redoToken, ^{
        shared = [[super allocWithZone:NULL] init];
        shared.dayDigestKey = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:shared delegateQueue:[[NSOperationQueue alloc] init]];
        shared.dayDigestKey.delegateQueue.maxConcurrentOperationCount = 1;
    });
    return shared;
}

- (void)baseTryOurSonRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eyeData))processBlock
                     success:(void(^)(NSDictionary * airGivenBadFix))success
                     failure:(void(^)(NSError *error))failure
                  readyCount:(NSInteger)readyCount {

    [self diastolicRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                readyCount:readyCount
            formMasterEdit:0];
}


- (void)diastolicRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eyeData))processBlock
                 success:(void(^)(NSDictionary * airGivenBadFix))success
                 failure:(void(^)(NSError *error))failure
              readyCount:(NSInteger)readyCount
          formMasterEdit:(NSInteger)formMasterEdit {

    mustYet(self);
    NSURLSessionDataTask *task = [self.dayDigestKey dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        birthPipe(self);
        
        NSError *edgaFriend = [self handleError:error response:response data:data];
        if (edgaFriend) {
            

            
            if (formMasterEdit < readyCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self diastolicRequest:request process:processBlock success:success failure:failure readyCount:readyCount formMasterEdit:formMasterEdit + 1];
                });
                return;
            }

            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(edgaFriend);
                });
            }
            return;
        }

        
        NSData *backAlphaData = processBlock ? processBlock(data) : data;
        if (!backAlphaData) {
            NSError *idleWayFatTaggerDaughters = [NSError errorWithDomain:@"NetworkCore"
                                                           code:-30002
                                                       userInfo:@{NSLocalizedDescriptionKey : @"Data processing failed"}];
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(idleWayFatTaggerDaughters);
                });
            }
            return;
        }

        NSError *levelHang;
        NSDictionary *hallResponse = [NSJSONSerialization JSONObjectWithData:backAlphaData options:0 error:&levelHang];

        if (!levelHang && [hallResponse isKindOfClass:[NSDictionary class]]) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    success(hallResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(levelHang);
                });
            }
        }
    }];

    [task resume];
}


- (NSError *)handleError:(NSError *)error response:(NSURLResponse *)response data:(NSData *)data {
    if (error) {
        return error;
    }

    if (!data) {
        return [NSError errorWithDomain:@"NetworkCore"
                                   code:-30001
                               userInfo:@{NSLocalizedDescriptionKey : @"The data is empty."}];
    }

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]] || httpResponse.statusCode != 200) {
        return [NSError errorWithDomain:@"NetworkCore"
                                   code:httpResponse.statusCode
                               userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"HTTPError，code: %ld", (long)httpResponse.statusCode]}];
    }

    return nil;
}

@end
