






#import "MapYetRightKit.h"

#define bankArm(pen) __weak typeof(pen) weak##pen = pen;
#define feetExist(pen) __strong typeof(pen) pen = weak##pen;

@interface MapYetRightKit()

@property (nonatomic,strong) NSURLSession *anyStairTied;

@end

@implementation MapYetRightKit


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

- (void)eastMajorRoleRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable oddData))processBlock
                     success:(void(^)(NSDictionary * sinkTeamSayAre))success
                     failure:(void(^)(NSError *error))failure
                  tatarCount:(NSInteger)tatarCount {

    [self armRepairRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                tatarCount:tatarCount
            unloadLoadOver:0];
}


- (void)armRepairRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable oddData))processBlock
                 success:(void(^)(NSDictionary * sinkTeamSayAre))success
                 failure:(void(^)(NSError *error))failure
              tatarCount:(NSInteger)tatarCount
          unloadLoadOver:(NSInteger)unloadLoadOver {

    bankArm(self);
    NSURLSessionDataTask *task = [self.anyStairTied dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        feetExist(self);
        
        NSError *shrinkSend = [self handleError:error response:response data:data];
        if (shrinkSend) {
            

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

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

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

        NSError *inputEach;
        NSDictionary *saveResponse = [NSJSONSerialization JSONObjectWithData:primeBondData options:0 error:&inputEach];

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

    [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
