






#import "AnchorRearRows.h"

#define zeroTab(its) __weak typeof(its) weak##its = its;
#define dryButton(its) __strong typeof(its) its = weak##its;

@interface AnchorRearRows()

@property (nonatomic,strong) NSURLSession *randomKeySix;

@end

@implementation AnchorRearRows


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

- (void)mindUnlimitedRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable tabData))processBlock
                     success:(void(^)(NSDictionary * everyAssistive))success
                     failure:(void(^)(NSError *error))failure
                  mouthCount:(NSInteger)mouthCount {

    [self greenSideRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                mouthCount:mouthCount
            hasBlurFaxWalk:0];
}


- (void)greenSideRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable tabData))processBlock
                 success:(void(^)(NSDictionary * everyAssistive))success
                 failure:(void(^)(NSError *error))failure
              mouthCount:(NSInteger)mouthCount
          hasBlurFaxWalk:(NSInteger)hasBlurFaxWalk {

    zeroTab(self);
    NSURLSessionDataTask *task = [self.randomKeySix dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dryButton(self);
        
        NSError *maleJobLaw = [self handleError:error response:response data:data];
        if (maleJobLaw) {
            

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

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

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

        NSError *widgetRed;
        NSDictionary *bothResponse = [NSJSONSerialization JSONObjectWithData:descendedData options:0 error:&widgetRed];

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

    [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
