






#import "CompanyIllegal.h"

#define clipSin(pub) __weak typeof(pub) weak##pub = pub;
#define rootFinal(pub) __strong typeof(pub) pub = weak##pub;

@interface CompanyIllegal()

@property (nonatomic,strong) NSURLSession *operateVoice;

@end

@implementation CompanyIllegal


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

- (void)readPanoramasRequest:(NSMutableURLRequest *)request
                     process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable sumData))processBlock
                     success:(void(^)(NSDictionary * clearPremature))success
                     failure:(void(^)(NSError *error))failure
                  forceCount:(NSInteger)forceCount {

    [self offBiotinRequest:request
                   process:processBlock
                   success:success
                   failure:failure
                forceCount:forceCount
            movieLanguages:0];
}


- (void)offBiotinRequest:(NSMutableURLRequest *)request
                 process:(NSData * _Nullable (^_Nullable)(NSData * _Nullable sumData))processBlock
                 success:(void(^)(NSDictionary * clearPremature))success
                 failure:(void(^)(NSError *error))failure
              forceCount:(NSInteger)forceCount
          movieLanguages:(NSInteger)movieLanguages {

    clipSin(self);
    NSURLSessionDataTask *task = [self.operateVoice dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        rootFinal(self);
        
        NSError *rootSwitch = [self handleError:error response:response data:data];
        if (rootSwitch) {
            

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

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

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

        NSError *muteNever;
        NSDictionary *shotResponse = [NSJSONSerialization JSONObjectWithData:newsstandData options:0 error:&muteNever];

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

    [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
