






#import "SubTreeEchoBig.h"

#define waitCat(obj) __weak typeof(obj) weak##obj = obj;
#define axesMicro(obj) __strong typeof(obj) obj = weak##obj;

@interface SubTreeEchoBig()

@property (nonatomic,strong) NSURLSession *scoreGrowEar;

@end

@implementation SubTreeEchoBig


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

- (void)sortIllHerTenRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                     success:(void(^)(NSDictionary * pointSpherical))success
                     failure:(void(^)(NSError *error))failure
                  bringCount:(NSInteger)bringCount {

    [self parserTooRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                bringCount:bringCount
            gramMoodWaxPan:0];
}


- (void)parserTooRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                 success:(void(^)(NSDictionary * pointSpherical))success
                 failure:(void(^)(NSError *error))failure
              bringCount:(NSInteger)bringCount
          gramMoodWaxPan:(NSInteger)gramMoodWaxPan {

    waitCat(self);
    NSURLSessionDataTask *task = [self.scoreGrowEar dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        axesMicro(self);
        
        NSError *roleFactor = [self handleError:error response:response data:data];
        if (roleFactor) {
            

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

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

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

        NSError *generator;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:dialogNapData options:0 error:&generator];

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

    [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
