






#import "SumArtStairSob.h"

#define ropePan(who) __weak typeof(who) weak##who = who;
#define tremorTap(who) __strong typeof(who) who = weak##who;

@interface SumArtStairSob()

@property (nonatomic,strong) NSURLSession *breakReactor;

@end

@implementation SumArtStairSob


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

- (void)problemSparseRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable pinData))processBlock
                     touches:(void(^)(NSDictionary * packDustRemove))touches
                     failure:(void(^)(NSError *error))failure
                  delayCount:(NSInteger)delayCount {

    [self eggNativeRequest:request
                   process:processBlock
                   touches:touches
                   failure:failure
                delayCount:delayCount
            creditSentVery:0];
}


- (void)eggNativeRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable pinData))processBlock
                 touches:(void(^)(NSDictionary * packDustRemove))touches
                 failure:(void(^)(NSError *error))failure
              delayCount:(NSInteger)delayCount
          creditSentVery:(NSInteger)creditSentVery {

    ropePan(self);
    NSURLSessionDataTask *task = [self.breakReactor dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        tremorTap(self);
        
        NSError *sinOrdinal = [self handleError:error response:response data:data];
        if (sinOrdinal) {
            

            
            if (creditSentVery < delayCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self eggNativeRequest:request process:processBlock touches:touches failure:failure delayCount:delayCount creditSentVery:creditSentVery + 1];
                });
                return;
            }

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

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

        NSError *dependent;
        NSDictionary *gainResponse = [NSJSONSerialization JSONObjectWithData:volumeArtData options:0 error:&dependent];

        if (!dependent && [gainResponse isKindOfClass:[NSDictionary class]]) {
            if (touches) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    touches(gainResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(dependent);
                });
            }
        }
    }];

    [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
