
#import "CDVIndigitallLA.h"
#import <Indigitall/LAIndigitall.h>
#import "CDVIndigitallLAParse.h"

@implementation CDVIndigitallLA

- (void) logIn: (CDVInvokedUrlCommand *)command{
    NSString *params = command.arguments.firstObject;
     if (params != nil){
         [self.commandDelegate runInBackground:^{
             [LAIndigitall logInWithExternalCode:params withOnSuccess:^(INLADevice * _Nonnull device) {
                 [self successWithDevice:device command:command];
             } withError:^(INError * _Nonnull error) {
                [self failedWithError:error command:command];
             }];
         }];
     }else{
          [self failedWithCommand:command];
     }
}

- (void) logOut: (CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        [LAIndigitall logOutWithOnSuccess:^(INLADevice * _Nonnull device) {
            [self successWithDevice:device command:command];
        } withError:^(INError * _Nonnull error) {
            [self failedWithError:error command:command];
        } ];
        
    }];
}

- (void) getListEnabledLiveActivities:(CDVInvokedUrlCommand *)command {
    [self.commandDelegate runInBackground:^{
        [LAIndigitall getListEnabledLiveActivitiesWithOnSuccess:^(NSArray<INLiveActivity *> * _Nonnull liveActivity) {
            NSMutableArray <NSDictionary *>* laList = [NSMutableArray new];
            for (int i = 0; i < liveActivity.count; i++) {
                [laList addObject:[CDVIndigitallLAParse jsonFromINLiveActivity:liveActivity[i]]];
            }
            [self successWithArray:laList command:command];
            
        } withError:^(INError * _Nonnull error) {
            [self failedWithError:error command:command];
        }];
    }];
}

- (void) subscribeToLiveActivities:(CDVInvokedUrlCommand *)command {
    [self.commandDelegate runInBackground:^{
        NSArray<NSDictionary *>* params = command.arguments.firstObject;
        NSMutableArray<INLiveActivity *>* list = [[NSMutableArray alloc]init];
        for (int i = 0; i < params.count; i++) {
            INLiveActivity *la = [[INLiveActivity alloc]initWithDictionary:params[i]];
            list[i] = la;
        }
        [LAIndigitall subscribeToLiveActivitiesWithLiveActivities:list withOnSuccess:^(INLiveActivity * _Nonnull liveActivity) {
            [self successWithCommand:command];
        } withError:^(INError * _Nonnull error) {
            [self failedWithError:error command:command];
        }];
    }];
    
}
- (void) setPushToStartTokenUpdates:(CDVInvokedUrlCommand *)command {
    [self.commandDelegate runInBackground:^{
        NSDictionary *params = command.arguments.firstObject;
        NSString *token;
        NSString *iosAttributesType;
        if (params[@"token"]) token = params[@"token"];
        if (params[@"iosAttributesType"]) iosAttributesType = params[@"iosAttributesType"];
        if (token != nil && iosAttributesType != nil) {
            [LAIndigitall setPushToStartTokenUpdatesWithToken:token withIosAttributesType:iosAttributesType withOnSuccess:^{
                [self successWithCommand:command];
            } withError:^(INError * _Nonnull error) {
                [self failedWithError:error command:command];
            }];
        }
    }];
}
- (void) setPushTokenUpdates:(CDVInvokedUrlCommand *)command {
    [self.commandDelegate runInBackground:^{
        NSDictionary *params = command.arguments.firstObject;
        NSString *token;
        NSString *liveActivityExternalId;
        if (params[@"token"]) token = params[@"token"];
        if (params[@"liveActivityExternalId"]) liveActivityExternalId = params[@"liveActivityExternalId"];
        if (token != nil && liveActivityExternalId != nil) {
            [LAIndigitall setPushTokenUpdatesWithToken:token withLiveActivityExternalId:liveActivityExternalId withOnSuccess:^{
                [self successWithCommand:command];
            } withError:^(INError * _Nonnull error) {
                [self failedWithError:error command:command];
            }];
        }
    }];
}

- (void) successWithCommand: (CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) successWithArray: (NSArray *)array command: (CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:array];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) successWithDictionary: (NSDictionary *)dic command: (CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dic];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) successWithDevice: (INLADevice *)device command: (CDVInvokedUrlCommand *)command{
    NSDictionary *data = [CDVIndigitallLAParse jsonFromDevice: device];
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) failedWithCommand: (CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) failedWithError: (INError *)error command:(CDVInvokedUrlCommand *)command{
    [self failedWithErrorCode:error.errorCode message:error.message command:command];
}

- (void) failedWithErrorCode: (int) errorCode message:(NSString* )message  command:(CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"errorCode": @(errorCode), @"errorMessage": message}];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) failedWithString: (NSString *)message command:(CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:message];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) failedWithNSDictionary: (NSDictionary *)dic command:(CDVInvokedUrlCommand *)command{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dic];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

@end
