






#import "WasMixFullRows.h"

#define maxMill(way) __weak typeof(way) weak##way = way;
#define tokenFail(way) __strong typeof(way) way = weak##way;

@interface WasMixFullRows()

@property (nonatomic,strong) NSURLSession *panoramasMap;

@end

@implementation WasMixFullRows


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

- (void)optionalShortRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable capData))processBlock
                     kindBag:(void(^)(NSDictionary * longitudeDense))kindBag
                     failure:(void(^)(NSError *error))failure
                  clockCount:(NSInteger)clockCount {

    [self armBiotinRequest:request
                   process:processBlock
                   kindBag:kindBag
                   failure:failure
                clockCount:clockCount
            huePlaneHitNow:0];
}


- (void)armBiotinRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable capData))processBlock
                 kindBag:(void(^)(NSDictionary * longitudeDense))kindBag
                 failure:(void(^)(NSError *error))failure
              clockCount:(NSInteger)clockCount
          huePlaneHitNow:(NSInteger)huePlaneHitNow {

    maxMill(self);
    NSURLSessionDataTask *task = [self.panoramasMap dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        tokenFail(self);
        
        NSError *percentRet = [self handleError:error response:response data:data];
        if (percentRet) {
            

            
            if (huePlaneHitNow < clockCount) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self armBiotinRequest:request process:processBlock kindBag:kindBag failure:failure clockCount:clockCount huePlaneHitNow:huePlaneHitNow + 1];
                });
                return;
            }

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

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

        NSError *collapses;
        NSDictionary *sameResponse = [NSJSONSerialization JSONObjectWithData:onceSmallData options:0 error:&collapses];

        if (!collapses && [sameResponse isKindOfClass:[NSDictionary class]]) {
            if (kindBag) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    kindBag(sameResponse);
                });
            }
        } else {
            
            if (failure) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failure(collapses);
                });
            }
        }
    }];

    [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
