#import "SealdAccelerator.h"
#import "seald/Seald.h"

@implementation SealdAccelerator

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(encryptFileAsString:(NSString *)fileContent :(NSString *)filename :(NSString *)messageId :(NSString *)serializedSymKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    NSData *fileByteArray = [fileContent dataUsingEncoding:NSUTF8StringEncoding];
    NSData *symKeyByteArray = [[NSData alloc] initWithBase64EncodedString:serializedSymKey options:0];

    NSError *error;
    NSData *outputByteArray = React_native_seald_acceleratorEncryptFileFromSerializedSymKey(fileByteArray, filename, messageId, symKeyByteArray, &error);

    if (error != nil)
    {
        reject(@"error", @"error encryptFileAsString", error);
    } else {
      NSString *base64Encoded = [outputByteArray base64EncodedStringWithOptions:0];
      resolve(base64Encoded);
    }
}

RCT_EXPORT_METHOD(decryptFileAsString:(NSString *)fileContent :(NSString *)serializedSymKey :(RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject)
{
    NSData *fileByteArray = [[NSData alloc] initWithBase64EncodedString:fileContent options:0];
    NSData *symKeyByteArray = [[NSData alloc] initWithBase64EncodedString:serializedSymKey options:0];

    NSError *error;
    Common_modelsClearFile *clearFile = React_native_seald_acceleratorDecryptFileFromSerializedSymKey(fileByteArray, symKeyByteArray, &error);

    if (error != nil)
    {
        reject(@"error", @"error decryptFileAsString", error);
    } else {
        // NSString *base64ClearContent = [clearFile.fileContent base64EncodedStringWithOptions:0];
        NSString *utf8ClearContent = [[NSString alloc] initWithData:clearFile.fileContent encoding:NSUTF8StringEncoding];

        NSMutableDictionary *returnDict = [[NSMutableDictionary alloc]init];
        [returnDict setValue:clearFile.filename forKey:@"filename"];
        [returnDict setValue:utf8ClearContent forKey:@"fileContent"];
        [returnDict setValue:clearFile.messageId forKey:@"messageId"];

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:returnDict
                                                           options:0
                                                             error:&error];
        if (error != nil)
        {
            NSLog(@"decryptFileAsString error %@", error);
            reject(@"error", @"error decryptFileAsString json error", error);
        } else {
            NSString *serializedJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            resolve(serializedJson);
        }
    }
}

RCT_EXPORT_METHOD(encryptURI:(NSString *)filepath :(NSString *)filename :(NSString *)messageId :(NSString *)serializedSymKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    NSData *symKeyByteArray = [[NSData alloc] initWithBase64EncodedString:serializedSymKey options:0];
    NSError *error;
    NSString *goURI = React_native_seald_acceleratorEncryptFileFromUriAndSerializedSymKey(filepath, filename, messageId, symKeyByteArray, &error);

    if (error != nil)
    {
        reject(@"error", @"error decryptFileAsString", error);
    } else {
      resolve(goURI);
    }
}

RCT_EXPORT_METHOD(decryptURI:(NSString *)filepath :(NSString *)serializedSymKey :(RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject)
{
    NSData *symKeyByteArray = [[NSData alloc] initWithBase64EncodedString:serializedSymKey options:0];
    NSError *error;
    Common_modelsClearPath *clearFile = React_native_seald_acceleratorDecryptFileFromUriAndSerializedSymKey(filepath, symKeyByteArray, &error);

    if (error != nil)
    {
        reject(@"error", @"error decryptURI", error);
    } else {
        NSMutableDictionary *returnDict = [[NSMutableDictionary alloc]init];
        [returnDict setValue:clearFile.filename forKey:@"filename"];
        [returnDict setValue:clearFile.path forKey:@"path"];
        [returnDict setValue:clearFile.messageId forKey:@"messageId"];

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:returnDict
                                                           options:0
                                                             error:&error];
        if (error != nil)
        {
            NSLog(@"decryptURI error %@", error);
            reject(@"error", @"error decryptURI json error", error);
        } else {
            NSString *serializedJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            resolve(serializedJson);
        }
    }
}




@end
