//
//  JsonConstructor.m
//  test08
//
//  Created by Pavel Masuk on 4/29/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "JsonConstructor.h"

@implementation JsonConstructor

+ (NSString*)resultsToJsonString:(DocumentReaderResults*) results{
  NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
    myDictionary[@"highResolution"] = [NSNumber numberWithInteger:results.resolutionType];
    myDictionary[@"overallResult"] = [NSNumber numberWithInteger:results.overallResult];
    myDictionary[@"authenticityCheckList"] = @"empty";
    myDictionary[@"imageQualityGroup"] = [self generateImageQualityGroup:results.imageQualityGroup];
    myDictionary[@"barcodePosition"] = @"empty";
    myDictionary[@"documentPosition"] = @"empty";
    myDictionary[@"jsonResult"] = [self generateDocumentReaderJsonResult:results.jsonResult];
    myDictionary[@"graphicResult"] = [self generateDocumentReaderGraphicResult:results.graphicResult];
    myDictionary[@"textResult"] = [self generateDocumentReaderTextResult:results.textResult];
    myDictionary[@"documentType"] = [self generateDocumentReaderDocumentType:results.documentType];
    
  NSError *error;
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionary
                                                     options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                       error:&error];
  
  if (! jsonData) {
    //NSLog(@"Got an error: %@", error);
    return @"error";
  } else {
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
  }
}

+ (NSMutableDictionary*)generateImageQualityGroup:(ImageQualityGroup*) imageQualityGroup{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"count"] = [NSNumber numberWithInteger:imageQualityGroup.count];
    output[@"result"] = [NSNumber numberWithInteger:imageQualityGroup.result];
    output[@"imageQualityList"] = [self generateImageQualityList:imageQualityGroup.imageQualityList];
    
    return output;
}

+(NSMutableArray*)generateImageQualityList:(NSArray<ImageQuality *> * _Nonnull) imageQualityList{
    NSMutableArray *output = [[NSMutableArray alloc] init];
    for(ImageQuality* imageQuality in imageQualityList){
        [output addObject:[self generateImageQuality:imageQuality]];
    }
    
    return output;
}

+(NSMutableDictionary*)generateImageQuality:(ImageQuality*) imageQuality{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"type"] = [NSNumber numberWithInteger:imageQuality.type];
    output[@"result"] = [NSNumber numberWithInteger:imageQuality.result];
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderJsonResult:(DocumentReaderJsonResult*) documentReaderJsonResult{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"results"] = [self generateNSArrayDocumentReaderJsonResultGroup:documentReaderJsonResult.results];
    
    return output;
}

+(NSMutableArray*)generateNSArrayDocumentReaderJsonResultGroup:(NSArray<DocumentReaderJsonResultGroup *> * _Nonnull) list{
    NSMutableArray *output = [[NSMutableArray alloc] init];
    for(DocumentReaderJsonResultGroup* documentReaderJsonResultGroup in list){
        [output addObject:[self generateDocumentReaderJsonResultGroup:documentReaderJsonResultGroup]];
    }
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderJsonResultGroup:(DocumentReaderJsonResultGroup*) documentReaderJsonResultGroup{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"resultType"] = [NSNumber numberWithInteger:documentReaderJsonResultGroup.resultType];
    output[@"lightType"] = [NSNumber numberWithInteger:documentReaderJsonResultGroup.lightType];
    output[@"pageIdx"] = [NSNumber numberWithInteger:documentReaderJsonResultGroup.pageIdx];
    NSData *jsonResult = [documentReaderJsonResultGroup.jsonResult dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResult options:0 error:&error];
    output[@"jsonResult"] = jsonDict;
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderGraphicResult:(DocumentReaderGraphicResult*) documentReaderGraphicResult{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"fields"] = [self generateNSArrayDocumentReaderGraphicResultGroup:documentReaderGraphicResult.fields];
    
    return output;
}

+(NSMutableArray*)generateNSArrayDocumentReaderGraphicResultGroup:(NSArray<DocumentReaderGraphicField *> * _Nonnull) list{
    NSMutableArray *output = [[NSMutableArray alloc] init];
    for(DocumentReaderGraphicField* documentReaderGraphicField in list){
        [output addObject:[self generateDocumentReaderGraphicField:documentReaderGraphicField]];
    }
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderGraphicField:(DocumentReaderGraphicField*) documentReaderGraphicField{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"sourceType"] = [NSNumber numberWithInteger:documentReaderGraphicField.sourceType];
    output[@"fieldType"] = [NSNumber numberWithInteger:documentReaderGraphicField.fieldType];
    output[@"fieldName"] = documentReaderGraphicField.fieldName;
    output[@"fieldRect"] = [self generateCGRect:documentReaderGraphicField.boundRect];
    output[@"width"] = @(documentReaderGraphicField.boundRect.size.width);
    output[@"hieght"] = @(documentReaderGraphicField.boundRect.size.height);
    NSData *imageData = UIImageJPEGRepresentation(documentReaderGraphicField.value, 1.0);
    NSString * base64String = [imageData base64EncodedStringWithOptions:0];
    output[@"value"] = base64String;
    NSString* lightType = [NSNumber numberWithInteger:documentReaderGraphicField.lightType];
    output[@"light"] = [NSString stringWithFormat: @"%@%@", documentReaderGraphicField.lightName, lightType];
    
    return output;
}

+(NSMutableDictionary*)generateCGRect:(CGRect) cgRect{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"top"] = @(cgRect.origin.y);
    output[@"left"] = @(cgRect.origin.x);
    output[@"bottom"] = @(cgRect.origin.y+cgRect.size.height);
    output[@"right"] = @(cgRect.origin.x+cgRect.size.width);
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderTextResult:(DocumentReaderTextResult*) documentReaderTextResult{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"fields"] = [self generateNSArrayDocumentReaderTextField:documentReaderTextResult.fields];
    output[@"status"] = [NSNumber numberWithInteger:documentReaderTextResult.status];
    
    return output;
}

+(NSMutableArray*)generateNSArrayDocumentReaderTextField:(NSArray<DocumentReaderTextField *> * _Nonnull) list{
    NSMutableArray *output = [[NSMutableArray alloc] init];
    for(DocumentReaderTextField* documentReaderTextField in list){
        [output addObject:[self generateDocumentReaderTextField:documentReaderTextField]];
    }
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderTextField:(DocumentReaderTextField*) documentReaderTextField{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"fieldType"] = [NSNumber numberWithInteger:documentReaderTextField.fieldType];
    output[@"fieldName"] = documentReaderTextField.fieldName;
    output[@"lcid"] = [NSNumber numberWithInteger:documentReaderTextField.lcid];
    output[@"lcidName"] = documentReaderTextField.lcidName;
    output[@"values"] = [self generateNSArrayDocumentReaderValue:documentReaderTextField.values];
    output[@"status"] = [NSNumber numberWithInteger:documentReaderTextField.status];
    output[@"value"] = [self generateDocumentReaderValue:documentReaderTextField.value];
    
    return output;
}

+(NSMutableArray*)generateNSArrayDocumentReaderValue:(NSArray<DocumentReaderValue *> * _Nonnull) list{
    NSMutableArray *output = [[NSMutableArray alloc] init];
    for(DocumentReaderValue* documentReaderValue in list){
        [output addObject:[self generateDocumentReaderValue:documentReaderValue]];
    }
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderValue:(DocumentReaderValue*) documentReaderValue{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"sourceType"] = [NSNumber numberWithInteger:documentReaderValue.sourceType];
    output[@"value"] = documentReaderValue.value;
    output[@"originalValue"] = documentReaderValue.originalValue;
    output[@"boundRect"] = [self generateCGRect:documentReaderValue.boundRect];
    output[@"validity"] = [NSNumber numberWithInteger:documentReaderValue.validity];
    
    return output;
}

+(NSMutableDictionary*)generateDocumentReaderDocumentType:(DocumentReaderDocumentType*) documentReaderDocumentType{
    NSMutableDictionary *output = [[NSMutableDictionary alloc] init];
    output[@"name"] = documentReaderDocumentType.name;
    output[@"documentID"] = [NSNumber numberWithInteger:documentReaderDocumentType.documentID];
    output[@"ICAOCode"] = documentReaderDocumentType.ICAOCode;
    output[@"FDSID"] = [self generateNSArrayNSNumber:documentReaderDocumentType.FDSID];
    output[@"dType"] = [NSNumber numberWithInteger:documentReaderDocumentType.dType];
    output[@"dFormat"] = [NSNumber numberWithInteger:documentReaderDocumentType.dFormat];
    output[@"dMRZ"] = [NSNumber numberWithBool:documentReaderDocumentType.dMRZ]; 
    output[@"dDescription"] = documentReaderDocumentType.dDescription;
    output[@"dYear"] = documentReaderDocumentType.dYear;
    output[@"dCountryName"] = documentReaderDocumentType.dCountryName;
    
    return output;
}

+(NSMutableArray*)generateNSArrayNSNumber:(NSArray<NSNumber *> * _Nonnull) list{
    NSMutableArray *output = [[NSMutableArray alloc] init];
    for(NSNumber* number in list){
        [output addObject:number];
    }
    
    return output;
}


@end
