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

@implementation WebSharePlugin

- (void)share:(CDVInvokedUrlCommand*)command {
    NSDictionary* options = [command argumentAtIndex:0 withDefault:@{} andClass:[NSDictionary class]];
    NSMutableArray* activityItems = [[NSMutableArray alloc] init];

    if (options[@"text"]) {
        [activityItems addObject:options[@"text"]];
    }
    if (options[@"url"]) {
        [activityItems addObject:[NSURL URLWithString:options[@"url"]]];
    }

    if ([activityItems count] == 0) {
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    } else {
        UIActivityViewController* dlg = [[UIActivityViewController alloc]
                                              initWithActivityItems:activityItems
                                              applicationActivities:NULL];

        dlg.excludedActivityTypes = options[@"iosExcludedActivities"];
        if (options[@"title"]) {
            [dlg setValue:options[@"title"] forKey:@"subject"];
        }

        UIPopoverPresentationController *popover = dlg.popoverPresentationController;
        if (popover) {
            popover.permittedArrowDirections = 0;
            popover.sourceView = self.webView.superview;
            popover.sourceRect = CGRectMake(CGRectGetMidX(self.webView.bounds), CGRectGetMidY(self.webView.bounds), 0, 0);
        }

        dlg.completionWithItemsHandler = ^(NSString *activityType,
                                          BOOL completed,
                                          NSArray *returnedItems,
                                          NSError *error){
            CDVPluginResult* pluginResult = NULL;
            if (error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription];
            } else {
                NSMutableArray *packageNames = [[NSMutableArray alloc] init];
                if (completed) {
                    [packageNames addObject:activityType];
                }
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:packageNames];
            }

            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        };

        [self.getTopPresentedViewController presentViewController:dlg animated:YES completion:NULL];
    }
}

-(UIViewController *)getTopPresentedViewController {
    UIViewController *presentingViewController = self.viewController;
    while(presentingViewController.presentedViewController != nil && ![presentingViewController.presentedViewController isBeingDismissed])
    {
        presentingViewController = presentingViewController.presentedViewController;
    }
    return presentingViewController;
}

@end
