#import "IosFileChooser.h"
#import <Cordova/CDV.h>

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@implementation IosFileChooser
{   
    CDVInvokedUrlCommand* initialInvokingCommand;
}

- (void)open:(CDVInvokedUrlCommand*)command
{
    initialInvokingCommand = command;

    NSArray *types = @[@"com.adobe.pdf"];

    NSArray *coords = [command.arguments objectAtIndex: 1];    
    CGRect rect = CGRectMake(0, 0, 0, 0);
    if ([command.arguments count] >= 2) {
        rect = CGRectMake([[coords objectAtIndex:0] floatValue],
                          [[coords objectAtIndex:1] floatValue],
                          [[coords objectAtIndex:2] floatValue],
                          [[coords objectAtIndex:3] floatValue]);
    }

    @try {
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")) { 
            UIDocumentPickerViewController *importMenu = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
            importMenu.delegate = self;
            [self.viewController presentViewController:importMenu animated:YES completion:nil];
        }
        else {
            UIDocumentMenuViewController *importMenu = [[UIDocumentMenuViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
            importMenu.delegate = self;
            importMenu.popoverPresentationController.sourceRect = rect;
            importMenu.popoverPresentationController.sourceView = self.viewController.view;
            importMenu.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
            [self.viewController presentViewController:importMenu animated:YES completion:nil];
        }
    }
    @catch ( NSException *e ) {
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"%@", e]];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:initialInvokingCommand.callbackId];
    }
}

-(void)documentMenuWasCancelled:(UIDocumentMenuViewController *)documentMenu {
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"documentMenuWasCancelled"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:initialInvokingCommand.callbackId];
}

- (void)documentMenu:(nonnull UIDocumentMenuViewController *)documentMenu didPickDocumentPicker:(nonnull UIDocumentPickerViewController *)documentPicker {
    @try {
        documentPicker.delegate = self;
        [self.viewController presentViewController:documentPicker animated:YES completion:nil];
    }
    @catch ( NSException *e ) {
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"%@", e]];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:initialInvokingCommand.callbackId];
    }
}

-(void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"documentPickerWasCancelled"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:initialInvokingCommand.callbackId];
}

-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[url absoluteString]];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:initialInvokingCommand.callbackId];
}

@end