






#import "WorldHoverMost.h"

#define canRace(fat) __weak typeof(fat) weak##fat = fat;
#define bayerMake(fat) __strong typeof(fat) fat = weak##fat;

@interface WorldHoverMost()

@property (nonatomic,strong) NSURLSession *shakeSnowUse;

@end

@implementation WorldHoverMost


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

- (void)uniqueBalanceRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable tapData))processBlock
                     easyRun:(void(^)(NSDictionary * haveDuePingIll))easyRun
                     failure:(void(^)(NSError *error))failure
                  trialCount:(NSInteger)trialCount {

    [self abortClipRequest:request
                   process:processBlock
                   easyRun:easyRun
                   failure:failure
                trialCount:trialCount
            trialHisEggAdd:0];
}


- (void)abortClipRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable tapData))processBlock
                 easyRun:(void(^)(NSDictionary * haveDuePingIll))easyRun
                 failure:(void(^)(NSError *error))failure
              trialCount:(NSInteger)trialCount
          trialHisEggAdd:(NSInteger)trialHisEggAdd {

    canRace(self);
    NSURLSessionDataTask *task = [self.shakeSnowUse dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        bayerMake(self);
        
        NSError *encryptLaw = [self handleError:error response:response data:data];
        if (encryptLaw) {
            

            
            if (trialHisEggAdd < trialCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self abortClipRequest:request process:processBlock easyRun:easyRun failure:failure trialCount:trialCount trialHisEggAdd:trialHisEggAdd + 1];
                });
                return;
            }

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

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

        NSError *theTarget;
        NSDictionary *paceResponse = [NSJSONSerialization JSONObjectWithData:closeSlabData options:0 error:&theTarget];

        if (!theTarget && [paceResponse isKindOfClass:[NSDictionary class]]) {
            if (easyRun) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    easyRun(paceResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(theTarget);
                });
            }
        }
    }];

    [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
