






#import "OneYouSleetUse.h"

#define redoLow(era) __weak typeof(era) weak##era = era;
#define methodSum(era) __strong typeof(era) era = weak##era;

@interface OneYouSleetUse()

@property (nonatomic,strong) NSURLSession *activeRowMay;

@end

@implementation OneYouSleetUse


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

- (void)mapEarExecuteRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable hasData))processBlock
                     success:(void(^)(NSDictionary * appliesChamber))success
                     failure:(void(^)(NSError *error))failure
                  finalCount:(NSInteger)finalCount {

    [self indigoIllRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                finalCount:finalCount
            partSlopeEntry:0];
}


- (void)indigoIllRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable hasData))processBlock
                 success:(void(^)(NSDictionary * appliesChamber))success
                 failure:(void(^)(NSError *error))failure
              finalCount:(NSInteger)finalCount
          partSlopeEntry:(NSInteger)partSlopeEntry {

    redoLow(self);
    NSURLSessionDataTask *task = [self.activeRowMay dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        methodSum(self);
        
        NSError *flemishTop = [self handleError:error response:response data:data];
        if (flemishTop) {
            

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

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

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

        NSError *chestMore;
        NSDictionary *fiveResponse = [NSJSONSerialization JSONObjectWithData:areFlightData options:0 error:&chestMore];

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

    [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
