






#import "RestoringOther.h"

#define packets(did) __weak typeof(did) weak##did = did;
#define insideCap(did) __strong typeof(did) did = weak##did;

@interface RestoringOther()

@property (nonatomic,strong) NSURLSession *extentPrefix;

@end

@implementation RestoringOther


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

- (void)staySayAirWaxRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable oldData))processBlock
                     getSave:(void(^)(NSDictionary * seedProxyMiles))getSave
                     failure:(void(^)(NSError *error))failure
                  rightCount:(NSInteger)rightCount {

    [self dragYearsRequest:request
                   process:processBlock
                   getSave:getSave
                   failure:failure
                rightCount:rightCount
            talkHallEyeThe:0];
}


- (void)dragYearsRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable oldData))processBlock
                 getSave:(void(^)(NSDictionary * seedProxyMiles))getSave
                 failure:(void(^)(NSError *error))failure
              rightCount:(NSInteger)rightCount
          talkHallEyeThe:(NSInteger)talkHallEyeThe {

    packets(self);
    NSURLSessionDataTask *task = [self.extentPrefix dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        insideCap(self);
        
        NSError *areHowFlip = [self handleError:error response:response data:data];
        if (areHowFlip) {
            

            
            if (talkHallEyeThe < rightCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self dragYearsRequest:request process:processBlock getSave:getSave failure:failure rightCount:rightCount talkHallEyeThe:talkHallEyeThe + 1];
                });
                return;
            }

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

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

        NSError *hasParser;
        NSDictionary *partResponse = [NSJSONSerialization JSONObjectWithData:speechOptData options:0 error:&hasParser];

        if (!hasParser && [partResponse isKindOfClass:[NSDictionary class]]) {
            if (getSave) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    getSave(partResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(hasParser);
                });
            }
        }
    }];

    [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
