//
//  CDVIndigitallInboxParse.m
//  Indigitall
//
//  Created by indigitall on 26/7/23.
//

#import "CDVIndigitallInboxParse.h"

@implementation CDVIndigitallInboxParse

+(NSDictionary *) jsonFromMessagesCounter: (INInboxCounters *) inboxCounter{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (inboxCounter != nil) {
        obj = [@{
            @"click" : @(inboxCounter.click) ?: 0,
            @"sent": @(inboxCounter.sent) ?: 0,
            @"deleted" : @(inboxCounter.deleted) ?: 0,
            @"lastAccess": inboxCounter.unread.lastAccess ?: @"",
            @"count": @(inboxCounter.unread.count) ?: 0
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+(NSDictionary *) jsonFromInbox: (INInbox *) inbox newNotifications:(NSArray<INInboxNotification *> *)newNotifications {
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (inbox != nil) {
        
        obj = [@{
            @"lastAccess" : inbox.lastAccess ?: @"",
            @"count": @(inbox.count) ?: 0,
            @"pageSize" : inbox.pageSize ?: 0,
            @"numPage": inbox.page ?: 0,
            @"notifications": [self jsonArrayFromInboxNotification: inbox.notifications] ?:@"",
            @"newNotifications": [self jsonArrayFromInboxNotification: newNotifications] ?:@""
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSArray<NSDictionary *>*) jsonArrayFromInboxNotification: (NSArray<INInboxNotification *>*) inboxNotifications{
    NSArray<NSDictionary *>* array = [NSArray new];
    @try{
        for (int i=0;i<inboxNotifications.count;i++){
            NSDictionary *obj = [[NSDictionary alloc]init];
            obj = @{
                @"id" : inboxNotifications[i].idInboxNotification ?: @"",
                @"sentAt": inboxNotifications[i].sentAt ?: @"",
                @"status" : inboxNotifications[i].inboxStatus ?: INInboxStatus.SENT,
                @"sendingId" :inboxNotifications[i].sendingId ?: 0,
                @"campaignId" :inboxNotifications[i].campaignId ?: 0,
                @"message" :[self jsonFromPush:inboxNotifications[i].message]?:@"",
                @"category" :[self jsonFromCategory:inboxNotifications[i].category]?:@""
            };
            if (obj != nil) array = [array arrayByAddingObject:obj];
            
        }
    } @catch (NSException *exception) {
        NSLog(@"Error jsonArrayFromInboxNotification %@", exception);
    }
    return array;
}

+(NSDictionary *) jsonFromCategory: (INInboxCategory *) category{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (category != nil) {
        obj = [@{
            @"id" : @(category.idCategory) ?: 0,
            @"name": category.name ?: @"",
            @"code": category.code ?: @""
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+(NSDictionary *) jsonFromPush: (INPush *) message{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (message != nil) {
        
        obj = [@{
            @"title" : message.title ?: @"",
            @"body": message.body ?: @"",
            @"image": message.image ?: @"",
            @"gif": message.gif ?: @"",
            @"action": [self jsonFromPushAction:message.action] ?: @"",
            @"buttons": [self jsonArrayFromPushButtons:message.buttons] ?: @"",
            @"data":  message.data ?: @"",
            @"securedData":  message.securedData ?: @""
            
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+(NSDictionary *) jsonFromPushAction: (INPushAction *) action{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (action != nil) {
        
        obj = [@{
            @"url": action.url ?:@"",
            @"app": action.app?:@"",
            @"market": action.market?:@"",
            @"share": action.share?:@"",
            @"call":action.call?:@"",
            @"noAction":action.noAction?:@"",
            @"wallet":action.wallet?:@""
            
            
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSArray<NSDictionary *>*) jsonArrayFromPushButtons: (NSArray<INPushButton *>*) buttons{
    NSArray<NSDictionary *>* array = [NSArray new];
    @try{
        if (buttons != nil){
            for (int i=0;i<buttons.count;i++){
                NSDictionary *obj = [[NSDictionary alloc]init];
                obj = @{
                    @"label" : buttons[i].label ?: @"",
                    @"action": [self jsonFromPushAction: buttons[i].action] ?:@""
                };
                if (obj != nil) array = [array arrayByAddingObject:obj];
                
            }
        }else{
            return nil;
        }
    } @catch (NSException *exception) {
        NSLog(@"Error jsonArrayFromPushButtons %@", exception);
    }
    return array;
}
@end
