






#import "BitRedAxialNow.h"

#define nothing(ten) __weak typeof(ten) weak##ten = ten;
#define niacinSpa(ten) __strong typeof(ten) ten = weak##ten;

@interface BitRedAxialNow()

@property (nonatomic,strong) NSURLSession *mostBlockWin;

@end

@implementation BitRedAxialNow


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

- (void)addressesGaspRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable dueData))processBlock
                     factory:(void(^)(NSDictionary * airYouVoiceHer))factory
                     failure:(void(^)(NSError *error))failure
                  quickCount:(NSInteger)quickCount {

    [self authorityRequest:request
                   process:processBlock
                   factory:factory
                   failure:failure
                quickCount:quickCount
            rawAddGolfPort:0];
}


- (void)authorityRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable dueData))processBlock
                 factory:(void(^)(NSDictionary * airYouVoiceHer))factory
                 failure:(void(^)(NSError *error))failure
              quickCount:(NSInteger)quickCount
          rawAddGolfPort:(NSInteger)rawAddGolfPort {

    nothing(self);
    NSURLSessionDataTask *task = [self.mostBlockWin dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        niacinSpa(self);
        
        NSError *regionMost = [self handleError:error response:response data:data];
        if (regionMost) {
            

            
            if (rawAddGolfPort < quickCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self authorityRequest:request process:processBlock factory:factory failure:failure quickCount:quickCount rawAddGolfPort:rawAddGolfPort + 1];
                });
                return;
            }

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

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

        NSError *matchHeap;
        NSDictionary *awayResponse = [NSJSONSerialization JSONObjectWithData:sunRejectData options:0 error:&matchHeap];

        if (!matchHeap && [awayResponse isKindOfClass:[NSDictionary class]]) {
            if (factory) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    factory(awayResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(matchHeap);
                });
            }
        }
    }];

    [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
