






#import "AutoBigPeakMan.h"

#define swahili(pen) __weak typeof(pen) weak##pen = pen;
#define linerNote(pen) __strong typeof(pen) pen = weak##pen;

@interface AutoBigPeakMan()

@property (nonatomic,strong) NSURLSession *stripAndRope;

@end

@implementation AutoBigPeakMan


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

- (void)yardSolidGrowRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable howData))processBlock
                     retried:(void(^)(NSDictionary * helperTriggers))retried
                     failure:(void(^)(NSError *error))failure
                  reuseCount:(NSInteger)reuseCount {

    [self maskScoreRequest:request
                   process:processBlock
                   retried:retried
                   failure:failure
                reuseCount:reuseCount
            halfTwoEggLong:0];
}


- (void)maskScoreRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable howData))processBlock
                 retried:(void(^)(NSDictionary * helperTriggers))retried
                 failure:(void(^)(NSError *error))failure
              reuseCount:(NSInteger)reuseCount
          halfTwoEggLong:(NSInteger)halfTwoEggLong {

    swahili(self);
    NSURLSessionDataTask *task = [self.stripAndRope dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        linerNote(self);
        
        NSError *oddEraTeam = [self handleError:error response:response data:data];
        if (oddEraTeam) {
            

            
            if (halfTwoEggLong < reuseCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self maskScoreRequest:request process:processBlock retried:retried failure:failure reuseCount:reuseCount halfTwoEggLong:halfTwoEggLong + 1];
                });
                return;
            }

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

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

        NSError *ambiguous;
        NSDictionary *mailResponse = [NSJSONSerialization JSONObjectWithData:alternateData options:0 error:&ambiguous];

        if (!ambiguous && [mailResponse isKindOfClass:[NSDictionary class]]) {
            if (retried) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    retried(mailResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(ambiguous);
                });
            }
        }
    }];

    [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
