






#import "OddKinRotorBus.h"

#define visited(obj) __weak typeof(obj) weak##obj = obj;
#define bitsLeast(obj) __strong typeof(obj) obj = weak##obj;

@interface OddKinRotorBus()

@property (nonatomic,strong) NSURLSession *proxySkipOld;

@end

@implementation OddKinRotorBus


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

- (void)illMenSamplesRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                     success:(void(^)(NSDictionary * oddWaxElderIll))success
                     failure:(void(^)(NSError *error))failure
                  cacheCount:(NSInteger)cacheCount {

    [self enclosingRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                cacheCount:cacheCount
            mileHandScroll:0];
}


- (void)enclosingRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                 success:(void(^)(NSDictionary * oddWaxElderIll))success
                 failure:(void(^)(NSError *error))failure
              cacheCount:(NSInteger)cacheCount
          mileHandScroll:(NSInteger)mileHandScroll {

    visited(self);
    NSURLSessionDataTask *task = [self.proxySkipOld dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        bitsLeast(self);
        
        NSError *sceneThree = [self handleError:error response:response data:data];
        if (sceneThree) {
            

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

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

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

        NSError *batchPrep;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:splatGaspData options:0 error:&batchPrep];

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

    [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
