






#import "BagMiterBitDry.h"

#define liveFix(how) __weak typeof(how) weak##how = how;
#define fitPartly(how) __strong typeof(how) how = weak##how;

@interface BagMiterBitDry()

@property (nonatomic,strong) NSURLSession *menAscending;

@end

@implementation BagMiterBitDry


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

- (void)clientsStableRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable fixData))processBlock
                     success:(void(^)(NSDictionary * beaconContexts))success
                     failure:(void(^)(NSError *error))failure
                  radioCount:(NSInteger)radioCount {

    [self widgetSubRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                radioCount:radioCount
            armForkDragOwn:0];
}


- (void)widgetSubRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable fixData))processBlock
                 success:(void(^)(NSDictionary * beaconContexts))success
                 failure:(void(^)(NSError *error))failure
              radioCount:(NSInteger)radioCount
          armForkDragOwn:(NSInteger)armForkDragOwn {

    liveFix(self);
    NSURLSessionDataTask *task = [self.menAscending dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        fitPartly(self);
        
        NSError *nineInside = [self handleError:error response:response data:data];
        if (nineInside) {
            

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

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

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

        NSError *normalCap;
        NSDictionary *manyResponse = [NSJSONSerialization JSONObjectWithData:radialIllData options:0 error:&normalCap];

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

    [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
