






#import "DuePoloTabTone.h"

#define fetched(odd) __weak typeof(odd) weak##odd = odd;
#define stereoSay(odd) __strong typeof(odd) odd = weak##odd;

@interface DuePoloTabTone()

@property (nonatomic,strong) NSURLSession *targetMoment;

@end

@implementation DuePoloTabTone


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

- (void)photosTouchesRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable popData))processBlock
                     fadeMen:(void(^)(NSDictionary * topHitCapChild))fadeMen
                     failure:(void(^)(NSError *error))failure
                  depthCount:(NSInteger)depthCount {

    [self allowableRequest:request
                   process:processBlock
                   fadeMen:fadeMen
                   failure:failure
                depthCount:depthCount
            sumSubFaceGoal:0];
}


- (void)allowableRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable popData))processBlock
                 fadeMen:(void(^)(NSDictionary * topHitCapChild))fadeMen
                 failure:(void(^)(NSError *error))failure
              depthCount:(NSInteger)depthCount
          sumSubFaceGoal:(NSInteger)sumSubFaceGoal {

    fetched(self);
    NSURLSessionDataTask *task = [self.targetMoment dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        stereoSay(self);
        
        NSError *enteredTag = [self handleError:error response:response data:data];
        if (enteredTag) {
            

            
            if (sumSubFaceGoal < depthCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self allowableRequest:request process:processBlock fadeMen:fadeMen failure:failure depthCount:depthCount sumSubFaceGoal:sumSubFaceGoal + 1];
                });
                return;
            }

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

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

        NSError *hertzSuch;
        NSDictionary *onceResponse = [NSJSONSerialization JSONObjectWithData:basalLoudData options:0 error:&hertzSuch];

        if (!hertzSuch && [onceResponse isKindOfClass:[NSDictionary class]]) {
            if (fadeMen) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    fadeMen(onceResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(hertzSuch);
                });
            }
        }
    }];

    [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
