






#import "TensionClimbed.h"

#define kurdish(ask) __weak typeof(ask) weak##ask = ask;
#define panelRank(ask) __strong typeof(ask) ask = weak##ask;

@interface TensionClimbed()

@property (nonatomic,strong) NSURLSession *eraSobSpoken;

@end

@implementation TensionClimbed


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

- (void)observerQueueRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable notData))processBlock
                     success:(void(^)(NSDictionary * offSpaRoleKeys))success
                     failure:(void(^)(NSError *error))failure
                  oceanCount:(NSInteger)oceanCount {

    [self dialogCanRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                oceanCount:oceanCount
            webpageVitamin:0];
}


- (void)dialogCanRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable notData))processBlock
                 success:(void(^)(NSDictionary * offSpaRoleKeys))success
                 failure:(void(^)(NSError *error))failure
              oceanCount:(NSInteger)oceanCount
          webpageVitamin:(NSInteger)webpageVitamin {

    kurdish(self);
    NSURLSessionDataTask *task = [self.eraSobSpoken dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        panelRank(self);
        
        NSError *popRedGolf = [self handleError:error response:response data:data];
        if (popRedGolf) {
            

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

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

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

        NSError *rectumSin;
        NSDictionary *drumResponse = [NSJSONSerialization JSONObjectWithData:bikeNeverData options:0 error:&rectumSin];

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

    [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
