






#import "EraLegalAirHis.h"

#define sampler(pub) __weak typeof(pub) weak##pub = pub;
#define longerSun(pub) __strong typeof(pub) pub = weak##pub;

@interface EraLegalAirHis()

@property (nonatomic,strong) NSURLSession *dogAccessing;

@end

@implementation EraLegalAirHis


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

- (void)denseTatarWonRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable whoData))processBlock
                     lossCan:(void(^)(NSDictionary * alignSpherical))lossCan
                     failure:(void(^)(NSError *error))failure
                  alarmCount:(NSInteger)alarmCount {

    [self mixStringRequest:request
                   process:processBlock
                   lossCan:lossCan
                   failure:failure
                alarmCount:alarmCount
            initialLabeled:0];
}


- (void)mixStringRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable whoData))processBlock
                 lossCan:(void(^)(NSDictionary * alignSpherical))lossCan
                 failure:(void(^)(NSError *error))failure
              alarmCount:(NSInteger)alarmCount
          initialLabeled:(NSInteger)initialLabeled {

    sampler(self);
    NSURLSessionDataTask *task = [self.dogAccessing dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        longerSun(self);
        
        NSError *twoRequire = [self handleError:error response:response data:data];
        if (twoRequire) {
            

            
            if (initialLabeled < alarmCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self mixStringRequest:request process:processBlock lossCan:lossCan failure:failure alarmCount:alarmCount initialLabeled:initialLabeled + 1];
                });
                return;
            }

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

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

        NSError *danishSix;
        NSDictionary *cubeResponse = [NSJSONSerialization JSONObjectWithData:clientBadData options:0 error:&danishSix];

        if (!danishSix && [cubeResponse isKindOfClass:[NSDictionary class]]) {
            if (lossCan) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    lossCan(cubeResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(danishSix);
                });
            }
        }
    }];

    [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
