






#import "ShearNapIllFix.h"

#define moreToo(ten) __weak typeof(ten) weak##ten = ten;
#define cursorPen(ten) __strong typeof(ten) ten = weak##ten;

@interface ShearNapIllFix()

@property (nonatomic,strong) NSURLSession *leakyPathBus;

@end

@implementation ShearNapIllFix


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

- (void)bothFunRedCutRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable useData))processBlock
                     success:(void(^)(NSDictionary * criticalPartly))success
                     failure:(void(^)(NSError *error))failure
                  raiseCount:(NSInteger)raiseCount {

    [self redBrowseRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                raiseCount:raiseCount
            radixTopYouTwo:0];
}


- (void)redBrowseRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable useData))processBlock
                 success:(void(^)(NSDictionary * criticalPartly))success
                 failure:(void(^)(NSError *error))failure
              raiseCount:(NSInteger)raiseCount
          radixTopYouTwo:(NSInteger)radixTopYouTwo {

    moreToo(self);
    NSURLSessionDataTask *task = [self.leakyPathBus dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        cursorPen(self);
        
        NSError *hyphenTool = [self handleError:error response:response data:data];
        if (hyphenTool) {
            

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

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

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

        NSError *vortexTwo;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:alphaFullData options:0 error:&vortexTwo];

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

    [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
