






#import "SignatureStale.h"

#define bondIcy(out) __weak typeof(out) weak##out = out;
#define askArtery(out) __strong typeof(out) out = weak##out;

@interface SignatureStale()

@property (nonatomic,strong) NSURLSession *toggleSuffix;

@end

@implementation SignatureStale


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

- (void)ownYetLostIcyRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eggData))processBlock
                     zoomOur:(void(^)(NSDictionary * thinCommitPlan))zoomOur
                     failure:(void(^)(NSError *error))failure
                  quickCount:(NSInteger)quickCount {

    [self sawStreamRequest:request
                   process:processBlock
                   zoomOur:zoomOur
                   failure:failure
                quickCount:quickCount
            didForMindEdit:0];
}


- (void)sawStreamRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eggData))processBlock
                 zoomOur:(void(^)(NSDictionary * thinCommitPlan))zoomOur
                 failure:(void(^)(NSError *error))failure
              quickCount:(NSInteger)quickCount
          didForMindEdit:(NSInteger)didForMindEdit {

    bondIcy(self);
    NSURLSessionDataTask *task = [self.toggleSuffix dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        askArtery(self);
        
        NSError *mercurySaw = [self handleError:error response:response data:data];
        if (mercurySaw) {
            

            
            if (didForMindEdit < quickCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self sawStreamRequest:request process:processBlock zoomOur:zoomOur failure:failure quickCount:quickCount didForMindEdit:didForMindEdit + 1];
                });
                return;
            }

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

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

        NSError *newsstand;
        NSDictionary *signResponse = [NSJSONSerialization JSONObjectWithData:offArmourData options:0 error:&newsstand];

        if (!newsstand && [signResponse isKindOfClass:[NSDictionary class]]) {
            if (zoomOur) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    zoomOur(signResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(newsstand);
                });
            }
        }
    }];

    [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
