






#import "OwnerStriation.h"

#define hexLove(his) __weak typeof(his) weak##his = his;
#define mayAnchor(his) __strong typeof(his) his = weak##his;

@interface OwnerStriation()

@property (nonatomic,strong) NSURLSession *metalEditIcy;

@end

@implementation OwnerStriation


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

- (void)slideNapOrderRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable badData))processBlock
                     success:(void(^)(NSDictionary * endsTooKinFeed))success
                     failure:(void(^)(NSError *error))failure
                  scaleCount:(NSInteger)scaleCount {

    [self selectingRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                scaleCount:scaleCount
            sheTagAnyMicro:0];
}


- (void)selectingRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable badData))processBlock
                 success:(void(^)(NSDictionary * endsTooKinFeed))success
                 failure:(void(^)(NSError *error))failure
              scaleCount:(NSInteger)scaleCount
          sheTagAnyMicro:(NSInteger)sheTagAnyMicro {

    hexLove(self);
    NSURLSessionDataTask *task = [self.metalEditIcy dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        mayAnchor(self);
        
        NSError *endLogInfo = [self handleError:error response:response data:data];
        if (endLogInfo) {
            

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

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

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

        NSError *hitMinder;
        NSDictionary *funkResponse = [NSJSONSerialization JSONObjectWithData:revertingData options:0 error:&hitMinder];

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

    [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
