






#import "LinearRedoSink.h"

#define maleDog(odd) __weak typeof(odd) weak##odd = odd;
#define stakeQuit(odd) __strong typeof(odd) odd = weak##odd;

@interface LinearRedoSink()

@property (nonatomic,strong) NSURLSession *offsetsSmile;

@end

@implementation LinearRedoSink


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

- (void)nowDownMenOffRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable nowData))processBlock
                     success:(void(^)(NSDictionary * addPostMostDid))success
                     failure:(void(^)(NSError *error))failure
                  fullyCount:(NSInteger)fullyCount {

    [self instancesRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                fullyCount:fullyCount
            roomSelfHexHue:0];
}


- (void)instancesRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable nowData))processBlock
                 success:(void(^)(NSDictionary * addPostMostDid))success
                 failure:(void(^)(NSError *error))failure
              fullyCount:(NSInteger)fullyCount
          roomSelfHexHue:(NSInteger)roomSelfHexHue {

    maleDog(self);
    NSURLSessionDataTask *task = [self.offsetsSmile dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        stakeQuit(self);
        
        NSError *hexContact = [self handleError:error response:response data:data];
        if (hexContact) {
            

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

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

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

        NSError *minCamera;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:oldBounceData options:0 error:&minCamera];

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

    [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
