#import <Foundation/Foundation.h>
#import "AWSDKPlugin.h"
#import <AWSDK/AWSDK-Swift.h>
#import <WorkspaceOneSDKBridge/WorkspaceOneSDKBridge-Swift.h>
#import <WorkspaceOneSDKBridge/WorkspaceOneSDKBridge.h>

static NSString * const CALLBACK_SCHEME = @"awsdkcallback";

@interface AWSDKPlugin ()

@property (strong, nonatomic) NSString *persistedCallbackId;
@property (strong, nonatomic) WSOSBridge *bridge;

- (void) sendEvent: (NSString *) eventName;

- (void) sendEvent: (NSString *) eventName
    withDictionary: (NSDictionary *) eventData;

@end

@implementation AWSDKPlugin

- (void) pluginInitialize {
    self.bridge = [WSOSBridge sharedInstance];
    self.bridge.bridgeDelegate = self;
    [self.bridge startWorkspaceOneSDK];
}

- (void) setPersistentCallback: (CDVInvokedUrlCommand *) command {
    self.persistedCallbackId = [command callbackId];
}

- (void) allowCopyPaste: (CDVInvokedUrlCommand *) command {
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                        messageAsBool: [self.bridge allowCopyPaste]];

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

- (void) allowOffline: (CDVInvokedUrlCommand *) command {
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                        messageAsBool: [self.bridge allowOffline]];

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

- (void) customSettings: (CDVInvokedUrlCommand *) command {
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                      messageAsString: [self.bridge customSettings]];

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


- (void) restrictDocumentToApps: (CDVInvokedUrlCommand *) command {
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                        messageAsBool: [self.bridge restrictDocumentToApps]];

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

- (void) allowedApplications: (CDVInvokedUrlCommand *) command {
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                       messageAsArray: [self.bridge allowedApplications]];

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

- (void) groupId: (CDVInvokedUrlCommand *) command {

    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                       messageAsString: [self.bridge groupId]];

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

- (void) username: (CDVInvokedUrlCommand *) command {

    __block NSString *savedUsername = [self.bridge username];
    NSLog(@"USERNAME: %@", savedUsername);
    if(savedUsername && savedUsername.length > 0) {
        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                          messageAsString: savedUsername];
        
        [self.commandDelegate sendPluginResult: pluginResult
                                    callbackId: [command callbackId]];
        return;
    }

//    NSLog(@"Saved username is missing. Going for authentication");
//    [self.bridge updateUserCredentials:^(BOOL updated, NSError * _Nullable error) {
//
//        NSLog(@"Update credentials finished\nstatus: %d\nError: %@", (int)updated, error);
//        savedUsername = [self.bridge username];
//        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
//                                                          messageAsString: savedUsername];
//
//        [self.commandDelegate sendPluginResult: pluginResult
//                                    callbackId: [command callbackId]];
//    }];
}

- (void) serverName: (CDVInvokedUrlCommand *) command {

    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                      messageAsString: [self.bridge serverName]];

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

- (void) isCompliant: (CDVInvokedUrlCommand *) command {
    [self.bridge isCompliantWithCompletion:^(BOOL flag) {
        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                            messageAsBool: flag];

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

- (void) isCompromised: (CDVInvokedUrlCommand *) command {
    [self.bridge isCompromisedWithCompletion:^(BOOL flag) {
        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                            messageAsBool: flag];

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

    
}

//- (void)openFile:(CDVInvokedUrlCommand *)command {
//
//    __block NSString* filepath = [command.arguments objectAtIndex:0];
//    [self.commandDelegate runInBackground:^{
//        AWCordovaOpenFileStatus fileOpenStatus = [self.bridge openFile:filepath from:self.viewController];
//
//        CDVCommandStatus status = CDVCommandStatus_OK;
//        if(fileOpenStatus != AWCordovaOpenFileStatusSuccess){
//            status = CDVCommandStatus_ERROR;
//        }
//
//        CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: status messageAsInt: fileOpenStatus];
//        [self.commandDelegate sendPluginResult: pluginResult callbackId: [command callbackId]];
//    }];
//}
//
- (void)registerPushNotificationToken:(CDVInvokedUrlCommand *)command {

    NSString* token = [command.arguments objectAtIndex:0];

    [self.bridge registerPushNotificationTokenWithToken: token];
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                            messageAsBool: YES];
    [self.commandDelegate sendPluginResult: pluginResult
                                    callbackId: [command callbackId]];
}

#pragma mark AWSDKBridgeDelegateMethods

- (void) initSuccess {
    [self sendEvent: @"initSuccess"];
}

- (void) initFailure {
    [self sendEvent: @"initFailure"];
}

- (void) wipe {
    [self sendEvent: @"wipe"];
}

- (void) lock {
    [self sendEvent: @"lock"];
}

- (void) unlock {
    [self sendEvent: @"unlock"];
}

- (void) stopNetworkActivity: (AWNetworkActivityStatus) networkActivityStatus {
    [self sendEvent: @"stopNetworkActivity"
     withDictionary: @{@"status" : @(networkActivityStatus)}];
}

- (void) resumeNetworkActivity {
    [self sendEvent: @"resumeNetworkActivity"];
}

- (void) recievedProfiles {
    [self sendEvent: @"recievedProfiles"];
}

- (void) sendEvent: (NSString *) eventName {
    [self sendEvent: eventName
     withDictionary: @{}];
}

- (void) sendEvent: (NSString *) eventName
    withDictionary: (NSDictionary *) eventData {

    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
                                                  messageAsDictionary: @{@"eventName" : eventName,
                                                                         @"eventData" : eventData}];

    [pluginResult setKeepCallbackAsBool: YES]; // so Cordova keeps this callback ID alive for future use
    [self.commandDelegate sendPluginResult: pluginResult
                                callbackId: self.persistedCallbackId];
    
}


@end
