//
//  PhotoPluginVC.m
//  PhotoPlugin
//
//  Created by ApliteCustom2 on 11/4/14.
//
//

#import "MultiPartPlugin.h"
#import "Base64.h"

@interface MultiPartPlugin ()
{
    UIImageView *imageView;
    NSString *callBackID;
}

@end

@implementation MultiPartPlugin

- (void) uploadProfileImageToServer:(CDVInvokedUrlCommand *)command{
    
    callBackID = command.callbackId;
    BOOL isCamera = [[command.arguments objectAtIndex:1] boolValue];

    UIImagePickerController *picker= [[UIImagePickerController alloc] init];
    picker.allowsEditing = YES;
    picker.delegate = self;
    
    if(isCamera){
        [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }else{
        [picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }
    [self.viewController presentViewController:picker animated:YES completion:nil];
    
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData* data = UIImageJPEGRepresentation(image, 1.0f);
    NSString *strEncoded = [Base64 encode:data];

    if (image){
        [picker dismissViewControllerAnimated:YES completion:^{
            CDVPluginResult *pluginResult = [ CDVPluginResult resultWithStatus :CDVCommandStatus_OK messageAsString:strEncoded];
             [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
             [self.commandDelegate sendPluginResult:pluginResult callbackId:callBackID];
        }];
    }
}



@end
