






#import "BleedMeterFork.h"

#define spaPlan(ear) __weak typeof(ear) weak##ear = ear;
#define distorted(ear) __strong typeof(ear) ear = weak##ear;

@interface BleedMeterFork()

@property (nonatomic,strong) NSURLSession *awakeBalance;

@end

@implementation BleedMeterFork


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

- (void)beginBrushIcyRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable wasData))processBlock
                     success:(void(^)(NSDictionary * pressSwapWrist))success
                     failure:(void(^)(NSError *error))failure
                  errorCount:(NSInteger)errorCount {

    [self mouthDrumRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                errorCount:errorCount
            twoWriteTheFit:0];
}


- (void)mouthDrumRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable wasData))processBlock
                 success:(void(^)(NSDictionary * pressSwapWrist))success
                 failure:(void(^)(NSError *error))failure
              errorCount:(NSInteger)errorCount
          twoWriteTheFit:(NSInteger)twoWriteTheFit {

    spaPlan(self);
    NSURLSessionDataTask *task = [self.awakeBalance dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        distorted(self);
        
        NSError *pictureTap = [self handleError:error response:response data:data];
        if (pictureTap) {
            

            
            if (twoWriteTheFit < errorCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self mouthDrumRequest:request process:processBlock success:success failure:failure errorCount:errorCount twoWriteTheFit:twoWriteTheFit + 1];
                });
                return;
            }

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

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

        NSError *emailRear;
        NSDictionary *maleResponse = [NSJSONSerialization JSONObjectWithData:minWateryData options:0 error:&emailRear];

        if (!emailRear && [maleResponse isKindOfClass:[NSDictionary class]]) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    success(maleResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(emailRear);
                });
            }
        }
    }];

    [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
