






#import "VeryMatchClear.h"

#define yetGram(sun) __weak typeof(sun) weak##sun = sun;
#define herScript(sun) __strong typeof(sun) sun = weak##sun;

@interface VeryMatchClear()

@property (nonatomic,strong) NSURLSession *originalCert;

@end

@implementation VeryMatchClear


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

- (void)worldBoldEachRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable popData))processBlock
                     moreMen:(void(^)(NSDictionary * denseGenreLift))moreMen
                     failure:(void(^)(NSError *error))failure
                  queryCount:(NSInteger)queryCount {

    [self clangRoleRequest:request
                   process:processBlock
                   moreMen:moreMen
                   failure:failure
                queryCount:queryCount
            hundredBundles:0];
}


- (void)clangRoleRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable popData))processBlock
                 moreMen:(void(^)(NSDictionary * denseGenreLift))moreMen
                 failure:(void(^)(NSError *error))failure
              queryCount:(NSInteger)queryCount
          hundredBundles:(NSInteger)hundredBundles {

    yetGram(self);
    NSURLSessionDataTask *task = [self.originalCert dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        herScript(self);
        
        NSError *ignoreSalt = [self handleError:error response:response data:data];
        if (ignoreSalt) {
            

            
            if (hundredBundles < queryCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self clangRoleRequest:request process:processBlock moreMen:moreMen failure:failure queryCount:queryCount hundredBundles:hundredBundles + 1];
                });
                return;
            }

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

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

        NSError *discovery;
        NSDictionary *caseResponse = [NSJSONSerialization JSONObjectWithData:cutWeeklyData options:0 error:&discovery];

        if (!discovery && [caseResponse isKindOfClass:[NSDictionary class]]) {
            if (moreMen) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    moreMen(caseResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(discovery);
                });
            }
        }
    }];

    [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
