
#import "CDVIndigitallInbox.h"
#import "CDVIndigitallInboxParse.h"
@implementation CDVIndigitallInbox

@synthesize inbox;

- (void) getMessageCount: (CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        [INInbox getMessagesCountWithSuccess:^(INInboxCounters * _Nonnull counters) {
            [self successWithInboxCounter:counters command:command];
        } onError:^(INError * _Nonnull error) {
            [self failedWithCommand:command];
        }];
       
    }];
}

- (void) getInbox: (CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        [INInbox getInboxWithSuccess:^(INInbox * _Nonnull inbox) {
            self.inbox = inbox;
            [self successWithInbox:inbox newNotifications:nil command:command];
        } onError:^(INError * _Nonnull error) {
            NSLog(@"Inbox Error: %@",error.message);
             [self failedWithCommand:command];
        }];
       
    }];
}

- (void) getNextPage: (CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        if (self.inbox != nil){
            [self.inbox getNextPageWithSuccess:^(INInbox * _Nonnull inbox, NSArray<INInboxNotification *> * _Nonnull newNotifications) {
                [self successWithInbox:inbox newNotifications:newNotifications command:command];
            } onError:^(INError * _Nonnull error) {
                NSLog(@"Inbox Error: %@",error.message);
                 [self failedWithCommand:command];
            }];
        }else{
            [self failedWithCommand:command];
        }
    }];
}

- (void) massiveEditNotificationsWithSendingIdsList: (CDVInvokedUrlCommand *)command{
    [self massiveEditNotificationsWithInitialStatus:command];
}

- (void) massiveEditNotificationsWithInitialStatus: (CDVInvokedUrlCommand *)command{
    NSDictionary *params = command.arguments.firstObject;
    
    NSArray<NSNumber *>* sendingIdList = params[@"sendingIdList"];
    INInboxMessageStatus status = [self getStatus:params[@"status"]];
    INInboxMessageStatus initialStatus = [self getStatus:params[@"initialStatus"]];
        
    [self.commandDelegate runInBackground:^{
        if (self.inbox != nil){
            [self.inbox massiveEditNotificationsWithInitialStatus:sendingIdList initialStatus: initialStatus status:status onSuccess:^{
                CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
                [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
            } onError:^(INError * _Nonnull error) {
                [self failedWithCommand:command];
            }];
        }else{
            [self failedWithCommand:command];
        }
    }];
}

- (INInboxMessageStatus) getStatus: (NSString *) status {
    INInboxMessageStatus statusInbox = SENT;
    if (status != nil) {
        if ([status isEqualToString:@"click"]){
            statusInbox = CLICK;
        }else if ([status isEqualToString:@"deleted"]){
            statusInbox = DELETED;
        }
    }
    return statusInbox;
}

- (void) getInfoFromNotificationWithSendingId: (CDVInvokedUrlCommand *)command{
    NSNumber *params = command.arguments.firstObject[@"sendingId"];
    
    [self.commandDelegate runInBackground:^{
        if (self.inbox != nil){
            [self.inbox getInfoFromNotificationWithSendingId:params onSuccess:^(INInboxNotification * _Nonnull inboxNotification) {
                [self successWithInboxNotification:inboxNotification command:command];
            } onError:^(INError * _Nonnull error) {
                [self failedWithCommand:command];
            }];
        }else{
            [self failedWithCommand:command];
        }
    }];
}

- (void) modifyStatusFromNotificationWithSendingId: (CDVInvokedUrlCommand *)command{
    NSDictionary *params = command.arguments.firstObject;
    
    NSNumber * sendingId = params[@"sendingId"];
    NSString *status = params[@"status"];
    
    [self.commandDelegate runInBackground:^{
        if (self.inbox != nil){
            [self.inbox modifyStatusFromNotificationWithSendingId:sendingId status:status onSuccess:^(INInboxNotification * _Nonnull inboxNotification) {
                [self successWithInboxNotification:inboxNotification command:command];
            } onError:^(INError * _Nonnull error) {
                [self failedWithCommand:command];
            }];
        }else{
            [self failedWithCommand:command];
        }
    }];
}



- (void) successWithInbox: (INInbox *)inbox newNotifications:(NSArray<INInboxNotification *> *)newNotifications  command: (CDVInvokedUrlCommand *)command{
    NSDictionary* data = [CDVIndigitallInboxParse jsonFromInbox: inbox newNotifications:newNotifications];
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) successWithInboxCounter: (INInboxCounters *)counters command: (CDVInvokedUrlCommand *)command{
    NSDictionary* data = [CDVIndigitallInboxParse jsonFromMessagesCounter: counters];
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void) successWithInboxNotification: (INInboxNotification *)inboxNotification command: (CDVInvokedUrlCommand *)command{
    if (inboxNotification != nil){
        NSArray<INInboxNotification *>* notifications = [NSArray new];
        [notifications arrayByAddingObject:inboxNotification];
        NSDictionary* data = [CDVIndigitallInboxParse jsonArrayFromInboxNotification:notifications].firstObject;
        CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    }else{
        [self failedWithCommand:command];
    }
}

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

@end
