#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>
#import <Cordova/CDVPluginResult.h>
#import "AMCLogicClipboard.h"

@implementation AMCLogicClipboard
//********************************************************************************************
- (void)copy:(CDVInvokedUrlCommand*)command
{
  [self.commandDelegate runInBackground: ^{
		UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
		NSString     *text       = [command.arguments objectAtIndex:0];

		[pasteboard setValue:text forPasteboardType:@"public.text"];

		CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
		[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  }];
}
//********************************************************************************************
- (void)paste:(CDVInvokedUrlCommand*)command
{
  [self.commandDelegate runInBackground:^{
		UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
		NSString     *text       = [pasteboard valueForPasteboardType:@"public.text"];

	  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];
	  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  }];
}
//********************************************************************************************
- (void)nItems:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:[pasteboard numberOfItems]];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)getItems:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:[pasteboard items]];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)setItems:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:[pasteboard items]];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)string:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[pasteboard string]];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)strings:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:[pasteboard strings]];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)image:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  UIImage *image = [pasteboard image];
  @try {
    int newWidth = [[command.arguments objectAtIndex:0] integerValue];
    if(newWidth > 0) image = [self scaleImage:image toWidth:newWidth];
  } @catch(NSException *e) {}

  NSData *data = UIImagePNGRepresentation(image);

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:data];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)imageB64:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  UIImage *image = [pasteboard image];
  @try {
    int newWidth = [[command.arguments objectAtIndex:0] integerValue];
    if(newWidth > 0) image = [self scaleImage:image toWidth:newWidth];
  } @catch(NSException *e) {}

  NSData *data = UIImagePNGRepresentation(image);
  NSString *str = [data base64EncodedStringWithOptions:nil];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:str];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)images:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:[pasteboard images]];
  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (void)URLs:(CDVInvokedUrlCommand*)command
{
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

  // CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:[pasteboard urls]];
  // [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
//********************************************************************************************
- (UIImage *)scaleImage:(UIImage*)image toWidth:(int)newWidth
{
    CGImageRef imgRef = [image CGImage];
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    CGRect bounds = CGRectMake(0, 0, width, height);

    //if already at the minimum resolution, return the orginal image, otherwise scale
    if (width <= newWidth) {
        return image;
    } else {
        CGFloat ratio = width/height;
        bounds.size.width = newWidth;
        bounds.size.height = bounds.size.width / ratio;
    }

    UIGraphicsBeginImageContext(bounds.size);
    [image drawInRect:CGRectMake(0.0, 0.0, bounds.size.width, bounds.size.height)];
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;
}
//********************************************************************************************
@end
