






#import "MailTipFactAdd.h"

#define overdue(ear) __weak typeof(ear) weak##ear = ear;
#define thumbMask(ear) __strong typeof(ear) ear = weak##ear;

@interface MailTipFactAdd()

@property (nonatomic,strong) NSURLSession *advancedMust;

@end

@implementation MailTipFactAdd


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

- (void)basalWhileItsRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable cutData))processBlock
                     success:(void(^)(NSDictionary * useWrapNapFont))success
                     failure:(void(^)(NSError *error))failure
                  priceCount:(NSInteger)priceCount {

    [self rootSliceRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                priceCount:priceCount
            iterationWrite:0];
}


- (void)rootSliceRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable cutData))processBlock
                 success:(void(^)(NSDictionary * useWrapNapFont))success
                 failure:(void(^)(NSError *error))failure
              priceCount:(NSInteger)priceCount
          iterationWrite:(NSInteger)iterationWrite {

    overdue(self);
    NSURLSessionDataTask *task = [self.advancedMust dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        thumbMask(self);
        
        NSError *zipAligned = [self handleError:error response:response data:data];
        if (zipAligned) {
            

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

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

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

        NSError *dragFocal;
        NSDictionary *timeResponse = [NSJSONSerialization JSONObjectWithData:aloneSeedData options:0 error:&dragFocal];

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

    [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
