






#import "ResultStayMeal.h"

#define hellman(hit) __weak typeof(hit) weak##hit = hit;
#define serverBin(hit) __strong typeof(hit) hit = weak##hit;

@interface ResultStayMeal()

@property (nonatomic,strong) NSURLSession *hostBreaking;

@end

@implementation ResultStayMeal


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

- (void)americanPrimeRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable faxData))processBlock
                     cupDust:(void(^)(NSDictionary * thePortAskTags))cupDust
                     failure:(void(^)(NSError *error))failure
                  bleedCount:(NSInteger)bleedCount {

    [self sexSerialRequest:request
                   process:processBlock
                   cupDust:cupDust
                   failure:failure
                bleedCount:bleedCount
            dolbyToolRetry:0];
}


- (void)sexSerialRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable faxData))processBlock
                 cupDust:(void(^)(NSDictionary * thePortAskTags))cupDust
                 failure:(void(^)(NSError *error))failure
              bleedCount:(NSInteger)bleedCount
          dolbyToolRetry:(NSInteger)dolbyToolRetry {

    hellman(self);
    NSURLSessionDataTask *task = [self.hostBreaking dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        serverBin(self);
        
        NSError *highUseSub = [self handleError:error response:response data:data];
        if (highUseSub) {
            

            
            if (dolbyToolRetry < bleedCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self sexSerialRequest:request process:processBlock cupDust:cupDust failure:failure bleedCount:bleedCount dolbyToolRetry:dolbyToolRetry + 1];
                });
                return;
            }

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

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

        NSError *footerPen;
        NSDictionary *starResponse = [NSJSONSerialization JSONObjectWithData:deprecateData options:0 error:&footerPen];

        if (!footerPen && [starResponse isKindOfClass:[NSDictionary class]]) {
            if (cupDust) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    cupDust(starResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(footerPen);
                });
            }
        }
    }];

    [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
