






#import "ModuleTraverse.h"

#define newtons(sob) __weak typeof(sob) weak##sob = sob;
#define illExtras(sob) __strong typeof(sob) sob = weak##sob;

@interface ModuleTraverse()

@property (nonatomic,strong) NSURLSession *adjustingCar;

@end

@implementation ModuleTraverse


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

- (void)maxMixPostBinRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable offData))processBlock
                     distant:(void(^)(NSDictionary * givenMegahertz))distant
                     failure:(void(^)(NSError *error))failure
                  farsiCount:(NSInteger)farsiCount {

    [self creamyWonRequest:request
                   process:processBlock
                   distant:distant
                   failure:failure
                farsiCount:farsiCount
            firstTheFitLaw:0];
}


- (void)creamyWonRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable offData))processBlock
                 distant:(void(^)(NSDictionary * givenMegahertz))distant
                 failure:(void(^)(NSError *error))failure
              farsiCount:(NSInteger)farsiCount
          firstTheFitLaw:(NSInteger)firstTheFitLaw {

    newtons(self);
    NSURLSessionDataTask *task = [self.adjustingCar dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        illExtras(self);
        
        NSError *dateGopher = [self handleError:error response:response data:data];
        if (dateGopher) {
            

            
            if (firstTheFitLaw < farsiCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self creamyWonRequest:request process:processBlock distant:distant failure:failure farsiCount:farsiCount firstTheFitLaw:firstTheFitLaw + 1];
                });
                return;
            }

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

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

        NSError *revertWas;
        NSDictionary *liftResponse = [NSJSONSerialization JSONObjectWithData:oddMarginData options:0 error:&revertWas];

        if (!revertWas && [liftResponse isKindOfClass:[NSDictionary class]]) {
            if (distant) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    distant(liftResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(revertWas);
                });
            }
        }
    }];

    [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
