






#import "NowKeepRoleAir.h"

#define capTask(ask) __weak typeof(ask) weak##ask = ask;
#define awakeHold(ask) __strong typeof(ask) ask = weak##ask;

@interface NowKeepRoleAir()

@property (nonatomic,strong) NSURLSession *fadePlayable;

@end

@implementation NowKeepRoleAir


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

- (void)lightOptionalRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                     success:(void(^)(NSDictionary * zoomAreaBedAnd))success
                     failure:(void(^)(NSError *error))failure
                  solveCount:(NSInteger)solveCount {

    [self heartbeatRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                solveCount:solveCount
            loudEarHisMake:0];
}


- (void)heartbeatRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                 success:(void(^)(NSDictionary * zoomAreaBedAnd))success
                 failure:(void(^)(NSError *error))failure
              solveCount:(NSInteger)solveCount
          loudEarHisMake:(NSInteger)loudEarHisMake {

    capTask(self);
    NSURLSessionDataTask *task = [self.fadePlayable dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        awakeHold(self);
        
        NSError *burmeseHue = [self handleError:error response:response data:data];
        if (burmeseHue) {
            

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

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

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

        NSError *binNotify;
        NSDictionary *roomResponse = [NSJSONSerialization JSONObjectWithData:oceanSwapData options:0 error:&binNotify];

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

    [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
