






#import "QueueFaceStart.h"

#define poolSon(wet) __weak typeof(wet) weak##wet = wet;
#define oneCharge(wet) __strong typeof(wet) wet = weak##wet;

@interface QueueFaceStart()

@property (nonatomic,strong) NSURLSession *keyParsePeer;

@end

@implementation QueueFaceStart


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

- (void)convertedStopRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable didData))processBlock
                     success:(void(^)(NSDictionary * revertMoreLook))success
                     failure:(void(^)(NSError *error))failure
                  burstCount:(NSInteger)burstCount {

    [self sexScrollRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                burstCount:burstCount
            busyOurCardEye:0];
}


- (void)sexScrollRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable didData))processBlock
                 success:(void(^)(NSDictionary * revertMoreLook))success
                 failure:(void(^)(NSError *error))failure
              burstCount:(NSInteger)burstCount
          busyOurCardEye:(NSInteger)busyOurCardEye {

    poolSon(self);
    NSURLSessionDataTask *task = [self.keyParsePeer dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        oneCharge(self);
        
        NSError *flatSmooth = [self handleError:error response:response data:data];
        if (flatSmooth) {
            

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

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

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

        NSError *miterLock;
        NSDictionary *unitResponse = [NSJSONSerialization JSONObjectWithData:numberBagData options:0 error:&miterLock];

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

    [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
