






#import "FlushPanEndAge.h"

#define lawRows(fat) __weak typeof(fat) weak##fat = fat;
#define veryAngle(fat) __strong typeof(fat) fat = weak##fat;

@interface FlushPanEndAge()

@property (nonatomic,strong) NSURLSession *teamTagPause;

@end

@implementation FlushPanEndAge


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

- (void)lessPurchasedRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable badData))processBlock
                     success:(void(^)(NSDictionary * finalSolveRear))success
                     failure:(void(^)(NSError *error))failure
                  ratioCount:(NSInteger)ratioCount {

    [self yardGenreRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                ratioCount:ratioCount
            pageKeyRedSign:0];
}


- (void)yardGenreRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable badData))processBlock
                 success:(void(^)(NSDictionary * finalSolveRear))success
                 failure:(void(^)(NSError *error))failure
              ratioCount:(NSInteger)ratioCount
          pageKeyRedSign:(NSInteger)pageKeyRedSign {

    lawRows(self);
    NSURLSessionDataTask *task = [self.teamTagPause dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        veryAngle(self);
        
        NSError *fairCupSay = [self handleError:error response:response data:data];
        if (fairCupSay) {
            

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

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

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

        NSError *learnDust;
        NSDictionary *cityResponse = [NSJSONSerialization JSONObjectWithData:outExtendData options:0 error:&learnDust];

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

    [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
