






#import "ProxiesSlashes.h"

#define verySum(fun) __weak typeof(fun) weak##fun = fun;
#define debugging(fun) __strong typeof(fun) fun = weak##fun;

@interface ProxiesSlashes()

@property (nonatomic,strong) NSURLSession *spineHueHang;

@end

@implementation ProxiesSlashes


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

- (void)hexExactPinchRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable yetData))processBlock
                     success:(void(^)(NSDictionary * findEraAtomTap))success
                     failure:(void(^)(NSError *error))failure
                  digitCount:(NSInteger)digitCount {

    [self penRecentRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                digitCount:digitCount
            depthDigitized:0];
}


- (void)penRecentRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable yetData))processBlock
                 success:(void(^)(NSDictionary * findEraAtomTap))success
                 failure:(void(^)(NSError *error))failure
              digitCount:(NSInteger)digitCount
          depthDigitized:(NSInteger)depthDigitized {

    verySum(self);
    NSURLSessionDataTask *task = [self.spineHueHang dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        debugging(self);
        
        NSError *napForever = [self handleError:error response:response data:data];
        if (napForever) {
            

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

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

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

        NSError *largeHead;
        NSDictionary *bodyResponse = [NSJSONSerialization JSONObjectWithData:standViewData options:0 error:&largeHead];

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

    [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
