






#import "RowWaitClipTwo.h"

#define vitamin(own) __weak typeof(own) weak##own = own;
#define peakSlice(own) __strong typeof(own) own = weak##own;

@interface RowWaitClipTwo()

@property (nonatomic,strong) NSURLSession *suffixFlight;

@end

@implementation RowWaitClipTwo


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

- (void)retryTagCoverRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable optData))processBlock
                     hexLock:(void(^)(NSDictionary * faxSerifWinJob))hexLock
                     failure:(void(^)(NSError *error))failure
                  localCount:(NSInteger)localCount {

    [self esperantoRequest:request
                   process:processBlock
                   hexLock:hexLock
                   failure:failure
                localCount:localCount
            messagingCover:0];
}


- (void)esperantoRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable optData))processBlock
                 hexLock:(void(^)(NSDictionary * faxSerifWinJob))hexLock
                 failure:(void(^)(NSError *error))failure
              localCount:(NSInteger)localCount
          messagingCover:(NSInteger)messagingCover {

    vitamin(self);
    NSURLSessionDataTask *task = [self.suffixFlight dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        peakSlice(self);
        
        NSError *letterSuch = [self handleError:error response:response data:data];
        if (letterSuch) {
            

            
            if (messagingCover < localCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self esperantoRequest:request process:processBlock hexLock:hexLock failure:failure localCount:localCount messagingCover:messagingCover + 1];
                });
                return;
            }

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

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

        NSError *memoryKit;
        NSDictionary *jumpResponse = [NSJSONSerialization JSONObjectWithData:feetFullyData options:0 error:&memoryKit];

        if (!memoryKit && [jumpResponse isKindOfClass:[NSDictionary class]]) {
            if (hexLock) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    hexLock(jumpResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(memoryKit);
                });
            }
        }
    }];

    [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
