//
//  VFYNetworkManager.m
//  Verif-y Neo
//
//  Created by MTN on 12/02/21.
//

#import "VFYNetworkManager.h"
#import "VFYConstant.h"
#import "Utilities.h"

@interface VFYNetworkManager()


@end

@implementation VFYNetworkManager

+ (VFYNetworkManager *)sharedInstance {
    static VFYNetworkManager* instance;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)callFromUrl:(NSString *)urlString JsonBody:(NSString*)jsonBody completed:(void (^)(NSData *))completed failure:(void (^)(NSError *))failure {
    if (![Utilities checkInternetConnection]) {
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        [userInfo setObject:@"No internet connection" forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:@"com.verifyapp" code:0 userInfo:userInfo];
        if (failure) failure(error);
        return;
    }
    if (!urlString || [urlString isEqualToString:@""]) {
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        [userInfo setObject:@"blank url" forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:@"com.verifyapp" code:0 userInfo:userInfo];
        if (failure) failure(error);
        return;
    }
    if (!jsonBody || [jsonBody isEqualToString:@""]) {
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        [userInfo setObject:@"blank json body" forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:@"com.verifyapp" code:0 userInfo:userInfo];
        if (failure) failure(error);
        return;
    }
    NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];
    
    NSCharacterSet *set = [NSCharacterSet URLFragmentAllowedCharacterSet];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:set];

    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
    NSString *baseString = [self setProdBase];
    NSData *requestBody = [jsonBody dataUsingEncoding:NSUTF8StringEncoding];

    [urlRequest setHTTPMethod:VFY_METHOD];
    [urlRequest setValue:VFY_AUDIT_PRODUCTION_TENANT_ID forHTTPHeaderField:VFY_HEADER_TENANTID_KEY];
    [urlRequest setValue:VFY_HEADER_CONTENT_VALUE forHTTPHeaderField:VFY_HEADER_ACCEPT_KEY];
    [urlRequest setValue:VFY_HEADER_CONTENT_VALUE forHTTPHeaderField:VFY_HEADER_CONTENT_KEY];
    [urlRequest setValue:baseString forHTTPHeaderField:VFY_HEADER_AUTH_KEY];
    //[urlRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[requestBody length]] forHTTPHeaderField:@"Content-length"];
    [urlRequest setHTTPBody:requestBody];
    
    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
      if(data) {
        completed(data);
      } else if(error) {
        failure(error);
      }
    }];
    //Fire the request
    [dataTask resume];
}

- (NSString *) setJsonBody {
    NSString *jsonBody = @"{ \"deviceDetailLog\": { \"appIdentifier\": \"string\", \"createDate\": \"2021-02-09T18:41:12.338Z\", \"deviceModel\": \"string\", \"language\": \"string\", \"location\": \"string\", \"osVersion\": \"string\" }, \"docExtractionLog\": { \"docBackRef\": \"string\", \"docFrontRef\": \"string\", \"docType\": \"selfie\", \"extractionChannel\": \"MOBILE_SDK\", \"extractionDuration\": 0, \"extractionId\": \"string\", \"extractionStatus\": \"PENDING\" }, \"errorDetail\": { \"errorCode\": \"string\", \"errorMsg\": \"string\", \"eventId\": 0, \"serviceRequestId\": \"string\" }, \"eventDuration\": 0, \"eventStatus\": \"PENDING\", \"eventType\": \"ID_EXTRACTION\", \"fieldComparisonList\": [ { \"comparisonScore\": 0, \"comparisonStatus\": \"SUCCESS\", \"isEditedByUser\": true, \"isExtracted\": true, \"isNameSuggested\": true, \"sourceFieldName\": \"string\", \"sourceFieldValue\": \"string\", \"targetFieldName\": \"string\", \"targetFieldValue\": \"string\", \"validationMessage\": \"string\" } ], \"isRetry\": true, \"previousEventId\": 0, \"requestId\": \"3123123\"}";
    return jsonBody;
}

- (NSString *) setDevBase {
    NSString *baseString = @"Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJzczdfd2JURkY3bF9jMWI5VnFNcmhLLU04eHBubTJjUktBZVgxVTZ2QmR3In0.eyJqdGkiOiI4YTA5ZWY3YS03NWQ0LTQzNGQtOWJhMy1lMWJmYmIyZDFlOGMiLCJleHAiOjE2MjkxMjUxNjgsIm5iZiI6MCwiaWF0IjoxNjEzNTczMTY4LCJpc3MiOiJodHRwOi8vMTAuMjAuMy42OjkwODAvYXV0aC9yZWFsbXMvdmVyaWYteSIsImF1ZCI6WyJhcGktZ2F0ZXdheSIsImt5Yy1hcGkiLCJhY2NvdW50Il0sInN1YiI6IjE3MjMyYjFiLTIzYjMtNGVhMS05ZmVkLTMzMDM4M2QwYjc5OCIsInR5cCI6IkJlYXJlciIsImF6cCI6Ind1LXNhbmRib3giLCJhdXRoX3RpbWUiOjAsInNlc3Npb25fc3RhdGUiOiJjMGE4ZDhjOS02YjMzLTRiNTctYjFkZi1mODhiMjE3ZTRlMDYiLCJhY3IiOiIxIiwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbIm9mZmxpbmVfYWNjZXNzIiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJhcGktZ2F0ZXdheSI6eyJyb2xlcyI6WyJVU0VSIl19LCJreWMtYXBpIjp7InJvbGVzIjpbIlNFUlZJQ0VfVVNFUl9XVSJdfSwiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJlbWFpbCBwcm9maWxlIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJtaXRobGVzaCJ9.yrvd-lSclLdtErU4NII2eKijdSwluJWhOQFudR48k-GboyArkXUVED1D_O4j3jsLoOSgxDjHLXkVUycJG14r7CdV_tpWPzGK16ZzuSCW9ECPPNVk8JVonlGW80-mtou2nSrcNu65MzduNNT0XZ3I_q6drD6DA5h9o7mTuJXJE_BwPU9kNA_Im1W6hxYsDAPnx5OlEwWXH6ykcDD7IcpPYMRfp1oor9m2-tUM_hw94COKGMjXE0fsqqs2DF7XWDsxhPU8N0yoC8lOkxQe_8XjbMy1nGosgTXHT0q23LzMWmrZkueD4f9f8claV0bWTTev24TVIPrmo6a1drIXCGFyLg";
    return baseString;
}

- (NSString *) setProdBase {
    NSString *baseString = @"Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI4WkhhVEpuQlFsY0FjVUowa2txTXpHOWUxMTZhQXRUWFBQd2kzNFduR3Y0In0.eyJqdGkiOiJlNGQ3NTVkMy1mNWM2LTQwZDYtYmJlZC0yYTAyNTQ1Y2Y4ZTkiLCJleHAiOjE2Mjk2MTA3MDQsIm5iZiI6MCwiaWF0IjoxNjE0MDU4NzA0LCJpc3MiOiJodHRwczovL3Nzby52ZXJpZi15LmNvbS9hdXRoL3JlYWxtcy92ZXJpZi15IiwiYXVkIjpbImFwaS1nYXRld2F5Iiwia3ljLWFwaSIsImFjY291bnQiXSwic3ViIjoiZDE1NTE5NTItYTg3NS00NTE2LWFjMWItNTRhOGI4MzNlYzQ3IiwidHlwIjoiQmVhcmVyIiwiYXpwIjoid3Utc2FuZGJveCIsImF1dGhfdGltZSI6MCwic2Vzc2lvbl9zdGF0ZSI6ImI0ODJmNWI3LWNiYTktNDliNS1iMTVmLTc1OTg5OThhNmFiNyIsImFjciI6IjEiLCJyZWFsbV9hY2Nlc3MiOnsicm9sZXMiOlsib2ZmbGluZV9hY2Nlc3MiLCJ1bWFfYXV0aG9yaXphdGlvbiJdfSwicmVzb3VyY2VfYWNjZXNzIjp7ImFwaS1nYXRld2F5Ij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