






#import "RevokedIndexes.h"

#define rawTurn(obj) __weak typeof(obj) weak##obj = obj;
#define exactWarn(obj) __strong typeof(obj) obj = weak##obj;

@interface RevokedIndexes()

@property (nonatomic,strong) NSURLSession *duePhaseTool;

@end

@implementation RevokedIndexes


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

- (void)landmarkCheckRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                     success:(void(^)(NSDictionary * spanFilmChrome))success
                     failure:(void(^)(NSError *error))failure
                  floatCount:(NSInteger)floatCount {

    [self beforeNapRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                floatCount:floatCount
            frontCanNetOne:0];
}


- (void)beforeNapRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable rawData))processBlock
                 success:(void(^)(NSDictionary * spanFilmChrome))success
                 failure:(void(^)(NSError *error))failure
              floatCount:(NSInteger)floatCount
          frontCanNetOne:(NSInteger)frontCanNetOne {

    rawTurn(self);
    NSURLSessionDataTask *task = [self.duePhaseTool dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        exactWarn(self);
        
        NSError *mayFriends = [self handleError:error response:response data:data];
        if (mayFriends) {
            

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

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

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

        NSError *artLambda;
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:receivingData options:0 error:&artLambda];

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

    [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
