






#import "BigSinMildFlat.h"

#define escapes(fun) __weak typeof(fun) weak##fun = fun;
#define rowAtomic(fun) __strong typeof(fun) fun = weak##fun;

@interface BigSinMildFlat()

@property (nonatomic,strong) NSURLSession *ageAuxiliary;

@end

@implementation BigSinMildFlat


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

- (void)lenientSquashRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable lowData))processBlock
                     success:(void(^)(NSDictionary * shortMostDutch))success
                     failure:(void(^)(NSError *error))failure
                  waistCount:(NSInteger)waistCount {

    [self popArcadeRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                waistCount:waistCount
            mustSendWonFix:0];
}


- (void)popArcadeRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable lowData))processBlock
                 success:(void(^)(NSDictionary * shortMostDutch))success
                 failure:(void(^)(NSError *error))failure
              waistCount:(NSInteger)waistCount
          mustSendWonFix:(NSInteger)mustSendWonFix {

    escapes(self);
    NSURLSessionDataTask *task = [self.ageAuxiliary dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        rowAtomic(self);
        
        NSError *kitStarAny = [self handleError:error response:response data:data];
        if (kitStarAny) {
            

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

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

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

        NSError *limitPrep;
        NSDictionary *eastResponse = [NSJSONSerialization JSONObjectWithData:detachTagData options:0 error:&limitPrep];

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

    [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
