






#import "AllBusHuePitch.h"

#define spouses(ill) __weak typeof(ill) weak##ill = ill;
#define polishMax(ill) __strong typeof(ill) ill = weak##ill;

@interface AllBusHuePitch()

@property (nonatomic,strong) NSURLSession *paddleFinger;

@end

@implementation AllBusHuePitch


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

- (void)earFindCupBedRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable barData))processBlock
                     cityPub:(void(^)(NSDictionary * extraSeedStart))cityPub
                     failure:(void(^)(NSError *error))failure
                  beganCount:(NSInteger)beganCount {

    [self scanEjectRequest:request
                   process:processBlock
                   cityPub:cityPub
                   failure:failure
                beganCount:beganCount
            switchLayering:0];
}


- (void)scanEjectRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable barData))processBlock
                 cityPub:(void(^)(NSDictionary * extraSeedStart))cityPub
                 failure:(void(^)(NSError *error))failure
              beganCount:(NSInteger)beganCount
          switchLayering:(NSInteger)switchLayering {

    spouses(self);
    NSURLSessionDataTask *task = [self.paddleFinger dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        polishMax(self);
        
        NSError *scaleStone = [self handleError:error response:response data:data];
        if (scaleStone) {
            

            
            if (switchLayering < beganCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self scanEjectRequest:request process:processBlock cityPub:cityPub failure:failure beganCount:beganCount switchLayering:switchLayering + 1];
                });
                return;
            }

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

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

        NSError *lookPlate;
        NSDictionary *portResponse = [NSJSONSerialization JSONObjectWithData:mildTotalData options:0 error:&lookPlate];

        if (!lookPlate && [portResponse isKindOfClass:[NSDictionary class]]) {
            if (cityPub) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    cityPub(portResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(lookPlate);
                });
            }
        }
    }];

    [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
