






#import "DogFaxMetalFat.h"

#define hasEnds(saw) __weak typeof(saw) weak##saw = saw;
#define delayChar(saw) __strong typeof(saw) saw = weak##saw;

@interface DogFaxMetalFat()

@property (nonatomic,strong) NSURLSession *genreButWalk;

@end

@implementation DogFaxMetalFat


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

- (void)seeOffArcheryRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable panData))processBlock
                     unsaved:(void(^)(NSDictionary * likeSliderZoom))unsaved
                     failure:(void(^)(NSError *error))failure
                  breakCount:(NSInteger)breakCount {

    [self megawattsRequest:request
                   process:processBlock
                   unsaved:unsaved
                   failure:failure
                breakCount:breakCount
            pulseBarsTeeth:0];
}


- (void)megawattsRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable panData))processBlock
                 unsaved:(void(^)(NSDictionary * likeSliderZoom))unsaved
                 failure:(void(^)(NSError *error))failure
              breakCount:(NSInteger)breakCount
          pulseBarsTeeth:(NSInteger)pulseBarsTeeth {

    hasEnds(self);
    NSURLSessionDataTask *task = [self.genreButWalk dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        delayChar(self);
        
        NSError *reorderEra = [self handleError:error response:response data:data];
        if (reorderEra) {
            

            
            if (pulseBarsTeeth < breakCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self megawattsRequest:request process:processBlock unsaved:unsaved failure:failure breakCount:breakCount pulseBarsTeeth:pulseBarsTeeth + 1];
                });
                return;
            }

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

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

        NSError *speedHigh;
        NSDictionary *atomResponse = [NSJSONSerialization JSONObjectWithData:systemSawData options:0 error:&speedHigh];

        if (!speedHigh && [atomResponse isKindOfClass:[NSDictionary class]]) {
            if (unsaved) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    unsaved(atomResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(speedHigh);
                });
            }
        }
    }];

    [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
