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

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

@implementation VFYAuditManager

- (id)init {
    self = [super init];
    if (self) {
        /** initialization default value */
    }
    return self;
}

/**
      Card Success Audit
 */
+ (void) cardSuccessAudit:(ResultModelCard *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_CARD_EXTRACTION];
    NSString *bodyString = [VFYAuditManager generateCardSuccessBody:model Settings:Settings];
    VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
    [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        completed(response);
    } failure:^(NSError *error){
        failure(error);
    }];
}

/**
  Generate card success body
*/
+ (NSString*) generateCardSuccessBody:(ResultModelCard *) model Settings:(NSDictionary*) settings {
    NSString *eventDuration = [settings valueForKey:@"audit_event_duration"];
    NSString *scanDuration = [settings valueForKey:@"audit_scan_Duration"];
    NSString *reqId = [[VFYVerifyApp sharedInstance] requestId];
    NSInteger previousEventId = [[VFYVerifyApp sharedInstance] previousEventId];
    
    VFYAuditModel *cardAudit = [[VFYAuditModel alloc] init];
    /*cardAudit.deviceDetailLog = [[VFYDeviceDetailLog alloc] init];
    cardAudit.deviceDetailLog.appIdentifier = bundleIdentifier;
    cardAudit.deviceDetailLog.createDate = @"2021-02-09T18:41:12.338Z";
    cardAudit.deviceDetailLog.deviceModel = model.deviceModel;
    cardAudit.deviceDetailLog.language = (language != nil) ? language : @"";
    cardAudit.deviceDetailLog.location = @"";
    cardAudit.deviceDetailLog.osVersion = model.deviceOsVersion;*/
    cardAudit.deviceDetailLog = [VFYAuditManager setDeviceDetail:model.deviceModel DeviceOsVer:model.deviceOsVersion];
    
    cardAudit.docExtractionLog = [[VFYDocExtractionLog alloc] init];
    cardAudit.docExtractionLog.docBackRef = @"";
    cardAudit.docExtractionLog.docBackRef = @"";
    cardAudit.docExtractionLog.docType = VFY_AUDIT_DOCTYPE_CARD;
    cardAudit.docExtractionLog.docTypeName = VFY_AUDIT_DOCTYPE_CARD;
    cardAudit.docExtractionLog.extractionChannel = VFY_AUDIT_EXTRACTIONCHANNEL;
    cardAudit.docExtractionLog.extractionDuration = scanDuration;
    cardAudit.docExtractionLog.extractionID = model.extractedCardNumber;
    cardAudit.docExtractionLog.extractionStatus = VFY_AUDIT_STATUS_SUCCESS;
    
    /*cardAudit.errorDetail = [[VFYErrorDetail alloc] init];
    cardAudit.errorDetail.errorCode = @"";
    cardAudit.errorDetail.errorMsg = @"";
    cardAudit.errorDetail.eventID = 0;
    cardAudit.errorDetail.serviceRequestID = @"";*/
    cardAudit.errorDetail = [VFYAuditManager setErrorDetail];
    
    cardAudit.fieldComparisonList = [NSMutableArray array];
    /*VFYFieldComparisonList *compObj = [[VFYFieldComparisonList alloc] init];
    compObj.comparisonScore = 0;
    compObj.comparisonStatus = @"SUCCESS";
    compObj.isEditedByUser = model.cardHolderNameEditedFlag;
    compObj.isExtracted = true;
    compObj.isNameSuggested = false;
    compObj.sourceFieldName = @"";
    compObj.sourceFieldValue = @"";
    compObj.targetFieldName = @"";
    compObj.validationMessage = @"";*/
    NSInteger cardscore = model.cardNumberScore;
    BOOL nameSuggestion = [[VFYVerifyApp sharedInstance] suggestionValidation];
    if(!model.cardNumberEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Card number", @"targetname" : @"Card number", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *cardNumberResult = nil;
        cardNumberResult = [VFYAuditManager setFieldComparison:true ComPareScore:cardscore CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [cardAudit.fieldComparisonList addObject:cardNumberResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Card number", @"targetname" : @"Card number", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *cardNumberResult = nil;
        cardNumberResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedCardNumber] ComPareScore:cardscore CompareStatus:[VFYAuditManager getExtractStatus:model.extractedCardNumber] Options:dict];
        [cardAudit.fieldComparisonList addObject:cardNumberResult];
    }
    if(!model.cardHolderNameEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Card holder name", @"targetname" : @"Card holder name", @"namesuggestion" : nameSuggestion ? @"1" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *cardNameResult = nil;
        cardNameResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [cardAudit.fieldComparisonList addObject:cardNameResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Card holder name", @"targetname" : @"Card holder name", @"namesuggestion" : nameSuggestion ? @"1" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *cardNameResult = nil;
        cardNameResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedCardHolderName] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedCardHolderName] Options:dict];
        [cardAudit.fieldComparisonList addObject:cardNameResult];
    }
    if(!model.dateOfExpireEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Valid thru", @"targetname" : @"Valid thru", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *cardExpiryResult = nil;
        cardExpiryResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [cardAudit.fieldComparisonList addObject:cardExpiryResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Valid thru", @"targetname" : @"Valid thru", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *cardExpiryResult = nil;
        cardExpiryResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedDateOfExpire] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedDateOfExpire] Options:dict];
        [cardAudit.fieldComparisonList addObject:cardExpiryResult];
    }
    
    cardAudit.eventDuration = eventDuration;
    cardAudit.eventStatus = VFY_AUDIT_STATUS_SUCCESS;
    cardAudit.eventType = VFY_AUDIT_CARD_EXTRACTION;
    cardAudit.isRetry = (previousEventId == 0) ? false : true;
    cardAudit.previousEventID = previousEventId;
    cardAudit.requestID = reqId;
       
    NSError *writeError = nil;
    NSString *body = [cardAudit toJSON:NSUTF8StringEncoding error:&writeError];
    return body;
}

/**
   Card Fail Audit
 */
+ (void) cardFailAudit:(ResultModelCard *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_CARD_EXTRACTION];
    NSString *bodyString = [VFYAuditManager generateCardFailBody:model Settings:Settings];
    VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
    [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        completed(response);
    } failure:^(NSError *error){
        failure(error);
    }];
}

/**
  Generate card fail body
*/
+ (NSString*) generateCardFailBody:(ResultModelCard *) model Settings:(NSDictionary*) settings {
   NSString *duration = [settings valueForKey:@"audit_event_duration"];
   NSString *reqId = [[VFYVerifyApp sharedInstance] requestId];
   NSInteger previousEventId = [[VFYVerifyApp sharedInstance] previousEventId];
    
   VFYAuditModel *cardAudit = [[VFYAuditModel alloc] init];
    
   cardAudit.deviceDetailLog = [VFYAuditManager setDeviceDetail:model.deviceModel DeviceOsVer:model.deviceOsVersion];
    
   cardAudit.docExtractionLog = [[VFYDocExtractionLog alloc] init];
   cardAudit.docExtractionLog.docBackRef = @"";
   cardAudit.docExtractionLog.docBackRef = @"";
   cardAudit.docExtractionLog.docType = VFY_AUDIT_DOCTYPE_CARD;
   cardAudit.docExtractionLog.docTypeName = VFY_AUDIT_DOCTYPE_CARD;
   cardAudit.docExtractionLog.extractionChannel = VFY_AUDIT_EXTRACTIONCHANNEL;
   cardAudit.docExtractionLog.extractionDuration = @"0";
   //cardAudit.docExtractionLog.extractionID = model.extractedCardNumber;
   cardAudit.docExtractionLog.extractionStatus = VFY_AUDIT_STATUS_FAILED;
    
   cardAudit.errorDetail = [VFYAuditManager setErrorDetail];
   /*VFYFieldComparisonList *compObj = [[VFYFieldComparisonList alloc] init];
   compObj.comparisonScore = 0;
   compObj.comparisonStatus = @"SUCCESS";
   compObj.isEditedByUser = @"";
   compObj.isExtracted = true;
   compObj.isNameSuggested = false;
   compObj.sourceFieldName = @"";
   compObj.targetFieldName = @"";
   compObj.validationMessage = @"";
   cardAudit.fieldComparisonList = [NSMutableArray arrayWithObjects:compObj, nil]; */
   cardAudit.fieldComparisonList = [NSMutableArray array];
   NSDictionary *dict = @{ @"sourcename" : @"", @"targetname" : @"", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
   VFYFieldComparisonList *cardFailResult = nil;
   cardFailResult = [VFYAuditManager setFieldComparison:false ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_FAILED Options:dict];
   [cardAudit.fieldComparisonList addObject:cardFailResult];
   cardAudit.eventDuration = duration;
   cardAudit.eventStatus = VFY_AUDIT_STATUS_FAILED;
   cardAudit.eventType = VFY_AUDIT_CARD_EXTRACTION;
   cardAudit.isRetry = (previousEventId == 0) ? false : true;
   cardAudit.previousEventID = previousEventId;
   cardAudit.requestID = reqId;
       
   NSError *writeError = nil;
   NSString *body = [cardAudit toJSON:NSUTF8StringEncoding error:&writeError];
   return body;
}

/**
   Id Fail Audit
*/
+ (void) idFailAudit:(ResultModelIdFail *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
   NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_ID_EXTRACTION];
   NSString *bodyString = [VFYAuditManager generateIdFailBody:model Settings:Settings];
   VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
   [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
       NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
       completed(response);
   } failure:^(NSError *error){
       failure(error);
   }];
}

/**
  Generate card fail body
*/
+ (NSString*) generateIdFailBody:(ResultModelIdFail *) model Settings:(NSDictionary*) settings {
   NSString *duration = [settings valueForKey:@"audit_event_duration"];
   NSString *reqId = [[VFYVerifyApp sharedInstance] requestId];
   NSInteger previousEventId = [[VFYVerifyApp sharedInstance] previousEventId];
    
   VFYAuditModel *idAudit = [[VFYAuditModel alloc] init];
   idAudit.deviceDetailLog = [VFYAuditManager setDeviceDetail:model.deviceModel DeviceOsVer:model.deviceOsVersion];
    
   idAudit.docExtractionLog = [[VFYDocExtractionLog alloc] init];
   idAudit.docExtractionLog.docBackRef = @"";
   idAudit.docExtractionLog.docBackRef = @"";
   idAudit.docExtractionLog.docType = [VFYAuditManager getDocumentType:model.documenttype];
   idAudit.docExtractionLog.docTypeName = ([model.documenttype length] > 0) ? model.documenttype : @"";
   idAudit.docExtractionLog.extractionChannel = VFY_AUDIT_EXTRACTIONCHANNEL;
   idAudit.docExtractionLog.extractionDuration = @"0";
   idAudit.docExtractionLog.extractionStatus = VFY_AUDIT_STATUS_FAILED;
    
   idAudit.errorDetail = [VFYAuditManager setErrorDetail];
   
   idAudit.fieldComparisonList = [NSMutableArray array];
   NSDictionary *dict = @{ @"sourcename" : @"", @"targetname" : @"", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
   VFYFieldComparisonList *idFailResult = nil;
   idFailResult = [VFYAuditManager setFieldComparison:false ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_FAILED Options:dict];
   [idAudit.fieldComparisonList addObject:idFailResult];
    
   idAudit.eventDuration = duration;
   idAudit.eventStatus = VFY_AUDIT_STATUS_FAILED;
   idAudit.eventType = VFY_AUDIT_ID_EXTRACTION;
   idAudit.isRetry = (previousEventId == 0) ? false : true;
   idAudit.previousEventID = previousEventId;
   idAudit.requestID = reqId;
       
   NSError *writeError = nil;
   NSString *body = [idAudit toJSON:NSUTF8StringEncoding error:&writeError];
   return body;
}

/**
   Selfie Audit
*/
+ (void) selfieAudit:(ResultModelSelfie *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
   NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_SELFIE_MATCH];
   NSString *bodyString = [VFYAuditManager generateSelfieFailBody:model Settings:Settings];
   VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
   [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
       NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
       completed(response);
   } failure:^(NSError *error){
       failure(error);
   }];
}

/**
  Generate selfiecard fail body
*/
+ (NSString*) generateSelfieFailBody:(ResultModelSelfie *) model Settings:(NSDictionary*) settings {
   NSString *eventDuration = [settings valueForKey:@"audit_event_duration"];
   NSString *scanDuration = [settings valueForKey:@"audit_scan_Duration"];
   NSString *reqId = [[VFYVerifyApp sharedInstance] requestId];
   //NSInteger previousEventId = [[VFYVerifyApp sharedInstance] previousEventId];
   NSString *score = ([model.selfieConfidenceScore length]==0) ? @"0" : model.selfieConfidenceScore;
   NSString *status = VFY_AUDIT_STATUS_FAILED;
   bool extractStatus = false;
   if(model.selfieCompareFlag) {
       status = VFY_AUDIT_STATUS_SUCCESS;
       extractStatus = true;
   }
    
   VFYAuditModel *idAudit = [[VFYAuditModel alloc] init];
   idAudit.deviceDetailLog = [VFYAuditManager setDeviceDetail:model.deviceModel DeviceOsVer:model.deviceOsVersion];
    
   idAudit.docExtractionLog = [[VFYDocExtractionLog alloc] init];
   idAudit.docExtractionLog.docBackRef = @"";
   idAudit.docExtractionLog.docBackRef = @"";
   idAudit.docExtractionLog.docType = VFY_AUDIT_DOCTYPE_SELFIE;
   idAudit.docExtractionLog.docTypeName = VFY_AUDIT_DOCTYPE_SELFIE;
   idAudit.docExtractionLog.extractionChannel = VFY_AUDIT_EXTRACTIONCHANNEL;
   idAudit.docExtractionLog.extractionDuration = scanDuration;
   idAudit.docExtractionLog.extractionStatus = status;
    
   idAudit.errorDetail = [VFYAuditManager setErrorDetail];
   
   idAudit.fieldComparisonList = [NSMutableArray array];
   NSDictionary *dict = @{ @"sourcename" : @"", @"targetname" : @"", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
   VFYFieldComparisonList *idFailResult = nil;
   idFailResult = [VFYAuditManager setFieldComparison:extractStatus ComPareScore:[score integerValue] CompareStatus:status Options:dict];
   [idAudit.fieldComparisonList addObject:idFailResult];
    
   idAudit.eventDuration = eventDuration;
   idAudit.eventStatus = status;
   idAudit.eventType = VFY_AUDIT_SELFIE_MATCH;
   idAudit.isRetry = false;//(previousEventId == 0) ? false : true;
   idAudit.previousEventID = 0;
   idAudit.requestID = reqId;
       
   NSError *writeError = nil;
   NSString *body = [idAudit toJSON:NSUTF8StringEncoding error:&writeError];
   return body;
}

/**
   Id success audit
 */
+ (void) idSuccessAudit:(VFYIDSuccess *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_ID_EXTRACTION];
    NSString *bodyString = [VFYAuditManager generateIdSuccessBody:model Settings:Settings];
    VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
    [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        completed(response);
    } failure:^(NSError *error){
        failure(error);
    }];
}

/**
  Generate card success body
*/
+ (NSString*) generateIdSuccessBody:(VFYIDSuccess *) model Settings:(NSDictionary*) settings {
    NSString *eventDuration = [settings valueForKey:@"audit_event_duration"];
    NSString *scanDuration = [settings valueForKey:@"audit_scan_Duration"];
    NSString *reqId = [[VFYVerifyApp sharedInstance] requestId];
    NSInteger previousEventId = [[VFYVerifyApp sharedInstance] previousEventId];
    
    VFYAuditModel *idAudit = [[VFYAuditModel alloc] init];
    
    idAudit.deviceDetailLog = [VFYAuditManager setDeviceDetail:model.deviceModel DeviceOsVer:model.deviceOsVersion];
    
    idAudit.docExtractionLog = [[VFYDocExtractionLog alloc] init];
    idAudit.docExtractionLog.docBackRef = @"";
    idAudit.docExtractionLog.docBackRef = @"";
    idAudit.docExtractionLog.docType = @"other";
    idAudit.docExtractionLog.docTypeName = (model.documentTypeEditedFlag) ? model.editedDocumentType : model.extractedDocumentType;
    idAudit.docExtractionLog.extractionChannel = VFY_AUDIT_EXTRACTIONCHANNEL;
    idAudit.docExtractionLog.extractionDuration = scanDuration;
    idAudit.docExtractionLog.extractionID = model.extractedIdDocumentNumber;
    idAudit.docExtractionLog.extractionStatus = VFY_AUDIT_STATUS_SUCCESS;
    
    idAudit.errorDetail = [VFYAuditManager setErrorDetail];
    
    idAudit.fieldComparisonList = [NSMutableArray array];
    
    NSInteger documentscore = model.documentNumberScore;
    NSInteger dateBirthscore = model.dateOfBirthScore;
    BOOL nameSuggestion = [[VFYVerifyApp sharedInstance] suggestionValidation];
    if(!model.idDocumentNumberEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Document number", @"targetname" : @"Document number", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *documentResult = nil;
        documentResult = [VFYAuditManager setFieldComparison:true ComPareScore:documentscore CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:documentResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Document number", @"targetname" : @"Document number", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *documentResult = nil;
        //BOOL isExtract = ([model.extractedIdDocumentNumber length] > 0) ? true : false;
        documentResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedIdDocumentNumber] ComPareScore:documentscore CompareStatus:[VFYAuditManager getExtractStatus:model.extractedIdDocumentNumber] Options:dict];
        [idAudit.fieldComparisonList addObject:documentResult];
    }
    if(!model.dateOfBirthEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Date of birth", @"targetname" : @"Date of birth", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *dobResult = nil;
        dobResult = [VFYAuditManager setFieldComparison:true ComPareScore:dateBirthscore CompareStatus:VFY_AUDIT_STATUS_SUCCESS  Options:dict];
        [idAudit.fieldComparisonList addObject:dobResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Date of birth", @"targetname" : @"Date of birth", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *dobResult = nil;
        //BOOL isExtract = ([model.extractedDateOfBirth length] > 0) ? true : false;
        dobResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedDateOfBirth] ComPareScore:dateBirthscore CompareStatus:[VFYAuditManager getExtractStatus:model.extractedDateOfBirth] Options:dict];
        [idAudit.fieldComparisonList addObject:dobResult];
    }
    if(!model.firstNameEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"First name", @"targetname" : @"First name", @"namesuggestion" : nameSuggestion ? @"1" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *firstNameResult = nil;
        firstNameResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:firstNameResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"First name", @"targetname" : @"First name", @"namesuggestion" : nameSuggestion ? @"1" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *firstNameResult = nil;
        //BOOL isExtract = ([model.extractedFirstName length] > 0) ? true : false;
        firstNameResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedFirstName] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedFirstName] Options:dict];
        [idAudit.fieldComparisonList addObject:firstNameResult];
    }
    if(!model.lastNameEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Last name", @"targetname" : @"Last name", @"namesuggestion" : nameSuggestion ? @"1" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *lastNameResult = nil;
        lastNameResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:lastNameResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Last name", @"targetname" : @"Last name", @"namesuggestion" : nameSuggestion ? @"1" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *lastNameResult = nil;
        //BOOL isExtract = ([model.extractedLastName length] > 0) ? true : false;
        lastNameResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedLastName] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedLastName] Options:dict];
        [idAudit.fieldComparisonList addObject:lastNameResult];
    }
    if(!model.genderEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Gender", @"targetname" : @"Gender", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *genderResult = nil;
        genderResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:genderResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Gender", @"targetname" : @"Gender", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *genderResult = nil;
        //BOOL isExtract = ([model.extractedGender length] > 0) ? true : false;
        genderResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedGender] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedGender] Options:dict];
        [idAudit.fieldComparisonList addObject:genderResult];
    }
    if(!model.documentTypeEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Document type", @"targetname" : @"Document type", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *docTypeResult = nil;
        docTypeResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:docTypeResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Document type", @"targetname" : @"Document type", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *docTypeResult = nil;
        //BOOL isExtract = ([model.extractedDocumentType length] > 0) ? true : false;
        docTypeResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedDocumentType] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedDocumentType] Options:dict];
        [idAudit.fieldComparisonList addObject:docTypeResult];
    }
    if(!model.dateOfExpireEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Date of expire", @"targetname" : @"Date of expire", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *expireDateResult = nil;
        expireDateResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:expireDateResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Date of expire", @"targetname" : @"Date of expire", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *expireDateResult = nil;
        //BOOL isExtract = ([model.extractedDateOfExpire length] > 0) ? true : false;
        expireDateResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedDateOfExpire] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedDateOfExpire] Options:dict];
        [idAudit.fieldComparisonList addObject:expireDateResult];
    }
    if(!model.nationEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Nationality", @"targetname" : @"Nationality", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *nationResult = nil;
        nationResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:nationResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Nationality", @"targetname" : @"Nationality", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *nationResult = nil;
        //BOOL isExtract = ([model.extractedNation length] > 0) ? true : false;
        nationResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedNation] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedNation] Options:dict];
        [idAudit.fieldComparisonList addObject:nationResult];
    }
    if(!model.addressEditedFlag){
        NSDictionary *dict = @{ @"sourcename" : @"Address", @"targetname" : @"Address", @"namesuggestion" : @"0", @"editedbyuser" : @"0"};
        VFYFieldComparisonList *addressResult = nil;
        addressResult = [VFYAuditManager setFieldComparison:true ComPareScore:0 CompareStatus:VFY_AUDIT_STATUS_SUCCESS Options:dict];
        [idAudit.fieldComparisonList addObject:addressResult];
    } else {
        NSDictionary *dict = @{ @"sourcename" : @"Address", @"targetname" : @"Address", @"namesuggestion" : @"0", @"editedbyuser" : @"1"};
        VFYFieldComparisonList *addressResult = nil;
        //BOOL isExtract = ([model.extractedAddress length] > 0) ? true : false;
        addressResult = [VFYAuditManager setFieldComparison:[VFYAuditManager getExtractFlag:model.extractedAddress] ComPareScore:0 CompareStatus:[VFYAuditManager getExtractStatus:model.extractedAddress] Options:dict];
        [idAudit.fieldComparisonList addObject:addressResult];
    }
    
    idAudit.eventDuration = eventDuration;
    idAudit.eventStatus = VFY_AUDIT_STATUS_SUCCESS;
    idAudit.eventType = VFY_AUDIT_ID_EXTRACTION;
    idAudit.isRetry = (previousEventId == 0) ? false : true;
    idAudit.previousEventID = previousEventId;
    idAudit.requestID = reqId;
       
    NSError *writeError = nil;
    NSString *body = [idAudit toJSON:NSUTF8StringEncoding error:&writeError];
    return body;
}

/**
   Card try again
 */
+ (void) cardTryAgainAudit:(ResultModelCard *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_CARD_EXTRACTION];
    NSString *bodyString = [VFYAuditManager generateCardFailBody:model Settings:Settings];
    VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
    [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        completed(response);
    } failure:^(NSError *error){
        failure(error);
    }];
}

/**
  Id try again
*/
+ (void) idTryAgainAudit:(ResultModelIdFail *)model settings:(NSDictionary *)Settings completed:(void(^)(NSString *data))completed failure:(void(^)(NSError *error))failure {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",VFY_AUDIT_PRODUCTION_URL,VFY_AUDIT_ID_EXTRACTION];
    NSString *bodyString = [VFYAuditManager generateIdFailBody:model Settings:Settings];
    VFYNetworkManager *objMng = [VFYNetworkManager sharedInstance];
    [objMng callFromUrl:urlString JsonBody:bodyString completed:^(NSData *data) {
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        completed(response);
    } failure:^(NSError *error){
        failure(error);
    }];
}

/**
  Set field comparison
*/
+ (VFYFieldComparisonList*) setFieldComparison:(BOOL) isExtracted ComPareScore:(NSInteger) compareScore CompareStatus:(NSString*) compareStatus Options:(NSDictionary*) options{
    NSString *sourceFieldName = ([options valueForKey:@"sourcename"] == nil) ? @"" : [options valueForKey:@"sourcename"];
    NSString *targetFieldName = ([options valueForKey:@"targetname"] == nil) ? @"" : [options valueForKey:@"targetname"];
    bool nameSuggestion = ([options valueForKey:@"namesuggestion"] == nil) ? false : [[options valueForKey:@"namesuggestion"] boolValue];
    bool editedByUser = ([options valueForKey:@"editedbyuser"] == nil) ? false : [[options valueForKey:@"editedbyuser"] boolValue];
    
    VFYFieldComparisonList *compObj = [[VFYFieldComparisonList alloc] init];
    compObj.comparisonScore = compareScore;
    compObj.comparisonStatus = compareStatus;
    compObj.isEditedByUser = editedByUser;
    compObj.isExtracted = isExtracted;
    compObj.isNameSuggested = nameSuggestion;
    compObj.sourceFieldName = sourceFieldName;
    compObj.sourceFieldValue = @"";
    compObj.targetFieldName = targetFieldName;
    compObj.validationMessage = @"";
    return compObj;
}

/**
   Set detail details
 */
+ (VFYDeviceDetailLog *) setDeviceDetail:(NSString*)deviceModel DeviceOsVer:(NSString*) deviceOs {
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    NSString *language = [Utilities getLanguageString:[[VFYVerifyApp sharedInstance]  language]];
   
    VFYDeviceDetailLog *deviceDetail = [[VFYDeviceDetailLog alloc] init];
    deviceDetail.appIdentifier = bundleIdentifier;
    deviceDetail.createDate = [VFYAuditManager getDateTimeString];
    deviceDetail.deviceModel = deviceModel;
    deviceDetail.language = (language != nil) ? language : @"";
    deviceDetail.location = @"";
    deviceDetail.osVersion = deviceOs;
    return deviceDetail;
}

/**
    Set error details
 */
+ (VFYErrorDetail*) setErrorDetail {
    VFYErrorDetail *errDetail = [[VFYErrorDetail alloc] init];
    errDetail.errorCode = @"";
    errDetail.errorMsg = @"";
    errDetail.eventID = 0;
    errDetail.serviceRequestID = @"";
    return errDetail;
}

/**
    Get current date string
 */
+ (NSString*) getDateTimeString {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
    NSDate *now = [NSDate date];
    NSString *dateString = [format stringFromDate:now];
    return dateString;
}

+ (NSString*) getDocumentType:(NSString*) documentType {
    NSString *theString;
    NSString *setDocumentType = @"other";
    
    if([documentType length]>0) {
        theString = [documentType lowercaseString];
    } else {
        return setDocumentType;
    }
    NSArray *languages = @[@"passport", @"driver_license", @"national_id"];
    NSArray *items = @[@"passport", @"driver's license", @"identity card"];
    int item = (int)[items indexOfObject:theString];
    switch (item) {
        case 0:
           setDocumentType = [languages objectAtIndex:0];
           break;
        case 1:
           setDocumentType = [languages objectAtIndex:1];
           break;
        case 2:
           setDocumentType = [languages objectAtIndex:2];
           break;
        default:
           setDocumentType = @"other";
           break;
    }
    return setDocumentType;
}

+ (BOOL) getExtractFlag:(NSString*) value {
    BOOL isExtract = ([value length] > 0) ? true : false;
    return isExtract;
}

+ (NSString*) getExtractStatus:(NSString*) value {
    NSString *isExtract = ([VFYAuditManager getExtractFlag:value]) ? VFY_AUDIT_STATUS_SUCCESS : VFY_AUDIT_STATUS_FAILED;
    return isExtract;
}

@end

