






#import "MidMixerPubOur.h"

#define schemes(our) __weak typeof(our) weak##our = our;
#define ditherFit(our) __strong typeof(our) our = weak##our;

@interface MidMixerPubOur()

@property (nonatomic,strong) NSURLSession *soundBeenFix;

@end

@implementation MidMixerPubOur


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

- (void)carSayMostPubRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable retData))processBlock
                     success:(void(^)(NSDictionary * notKinBusyMood))success
                     failure:(void(^)(NSError *error))failure
                  primeCount:(NSInteger)primeCount {

    [self sheetTempRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                primeCount:primeCount
            persistCompact:0];
}


- (void)sheetTempRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable retData))processBlock
                 success:(void(^)(NSDictionary * notKinBusyMood))success
                 failure:(void(^)(NSError *error))failure
              primeCount:(NSInteger)primeCount
          persistCompact:(NSInteger)persistCompact {

    schemes(self);
    NSURLSessionDataTask *task = [self.soundBeenFix dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        ditherFit(self);
        
        NSError *molarMiles = [self handleError:error response:response data:data];
        if (molarMiles) {
            

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

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

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

        NSError *scalarMen;
        NSDictionary *blobResponse = [NSJSONSerialization JSONObjectWithData:performedData options:0 error:&scalarMen];

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

    [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
