






#import "ButPullOffUnit.h"

#define spanish(sub) __weak typeof(sub) weak##sub = sub;
#define initially(sub) __strong typeof(sub) sub = weak##sub;

@interface ButPullOffUnit()

@property (nonatomic,strong) NSURLSession *fitnessFetch;

@end

@implementation ButPullOffUnit


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

- (void)sixGreatVitalRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable artData))processBlock
                     success:(void(^)(NSDictionary * offItemSumFlag))success
                     failure:(void(^)(NSError *error))failure
                  focusCount:(NSInteger)focusCount {

    [self paragraphRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                focusCount:focusCount
            kurdishChanged:0];
}


- (void)paragraphRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable artData))processBlock
                 success:(void(^)(NSDictionary * offItemSumFlag))success
                 failure:(void(^)(NSError *error))failure
              focusCount:(NSInteger)focusCount
          kurdishChanged:(NSInteger)kurdishChanged {

    spanish(self);
    NSURLSessionDataTask *task = [self.fitnessFetch dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        initially(self);
        
        NSError *partAgeOur = [self handleError:error response:response data:data];
        if (partAgeOur) {
            

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

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

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

        NSError *soundWake;
        NSDictionary *manyResponse = [NSJSONSerialization JSONObjectWithData:retainFixData options:0 error:&soundWake];

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

    [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
