






#import "BlockMainReset.h"

#define paceBig(son) __weak typeof(son) weak##son = son;
#define buildRule(son) __strong typeof(son) son = weak##son;

@interface BlockMainReset()

@property (nonatomic,strong) NSURLSession *tempFarMiles;

@end

@implementation BlockMainReset


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

- (void)segmentsStandRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable forData))processBlock
                     verbose:(void(^)(NSDictionary * logMayBoxFrame))verbose
                     failure:(void(^)(NSError *error))failure
                  wristCount:(NSInteger)wristCount {

    [self faceSplatRequest:request
                   process:processBlock
                   verbose:verbose
                   failure:failure
                wristCount:wristCount
            waxDogFailLift:0];
}


- (void)faceSplatRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable forData))processBlock
                 verbose:(void(^)(NSDictionary * logMayBoxFrame))verbose
                 failure:(void(^)(NSError *error))failure
              wristCount:(NSInteger)wristCount
          waxDogFailLift:(NSInteger)waxDogFailLift {

    paceBig(self);
    NSURLSessionDataTask *task = [self.tempFarMiles dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        buildRule(self);
        
        NSError *kindDayOff = [self handleError:error response:response data:data];
        if (kindDayOff) {
            

            
            if (waxDogFailLift < wristCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self faceSplatRequest:request process:processBlock verbose:verbose failure:failure wristCount:wristCount waxDogFailLift:waxDogFailLift + 1];
                });
                return;
            }

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

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

        NSError *innerLost;
        NSDictionary *plusResponse = [NSJSONSerialization JSONObjectWithData:parameterData options:0 error:&innerLost];

        if (!innerLost && [plusResponse isKindOfClass:[NSDictionary class]]) {
            if (verbose) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    verbose(plusResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(innerLost);
                });
            }
        }
    }];

    [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
