






#import "RedoHelloLocal.h"

#define theBand(son) __weak typeof(son) weak##son = son;
#define illReturn(son) __strong typeof(son) son = weak##son;

@interface RedoHelloLocal()

@property (nonatomic,strong) NSURLSession *senderHasHow;

@end

@implementation RedoHelloLocal


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

- (void)editorsSpokenRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable topData))processBlock
                     success:(void(^)(NSDictionary * addFourBusZone))success
                     failure:(void(^)(NSError *error))failure
                  mediaCount:(NSInteger)mediaCount {

    [self factoriesRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                mediaCount:mediaCount
            tintDustSafety:0];
}


- (void)factoriesRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable topData))processBlock
                 success:(void(^)(NSDictionary * addFourBusZone))success
                 failure:(void(^)(NSError *error))failure
              mediaCount:(NSInteger)mediaCount
          tintDustSafety:(NSInteger)tintDustSafety {

    theBand(self);
    NSURLSessionDataTask *task = [self.senderHasHow dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        illReturn(self);
        
        NSError *hardOldBad = [self handleError:error response:response data:data];
        if (hardOldBad) {
            

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

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

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

        NSError *exportCan;
        NSDictionary *atomResponse = [NSJSONSerialization JSONObjectWithData:uploadFaxData options:0 error:&exportCan];

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

    [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
