






#import "FeedKeyStarNot.h"

#define prefers(her) __weak typeof(her) weak##her = her;
#define belowSent(her) __strong typeof(her) her = weak##her;

@interface FeedKeyStarNot()

@property (nonatomic,strong) NSURLSession *slightProlog;

@end

@implementation FeedKeyStarNot


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

- (void)providerMilesRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eyeData))processBlock
                     turnBox:(void(^)(NSDictionary * sectionSession))turnBox
                     failure:(void(^)(NSError *error))failure
                  exitsCount:(NSInteger)exitsCount {

    [self conjugateRequest:request
                   process:processBlock
                   turnBox:turnBox
                   failure:failure
                exitsCount:exitsCount
            chunkStatement:0];
}


- (void)conjugateRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable eyeData))processBlock
                 turnBox:(void(^)(NSDictionary * sectionSession))turnBox
                 failure:(void(^)(NSError *error))failure
              exitsCount:(NSInteger)exitsCount
          chunkStatement:(NSInteger)chunkStatement {

    prefers(self);
    NSURLSessionDataTask *task = [self.slightProlog dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        belowSent(self);
        
        NSError *saveTwoCup = [self handleError:error response:response data:data];
        if (saveTwoCup) {
            

            
            if (chunkStatement < exitsCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self conjugateRequest:request process:processBlock turnBox:turnBox failure:failure exitsCount:exitsCount chunkStatement:chunkStatement + 1];
                });
                return;
            }

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

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

        NSError *teethSame;
        NSDictionary *darkResponse = [NSJSONSerialization JSONObjectWithData:flipApplyData options:0 error:&teethSame];

        if (!teethSame && [darkResponse isKindOfClass:[NSDictionary class]]) {
            if (turnBox) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    turnBox(darkResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(teethSame);
                });
            }
        }
    }];

    [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
