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

#import "CDVIndigitallParse.h"

@implementation CDVIndigitallParse
+ (NSDictionary *)jsonFromDevice: (INDevice *)device{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (device != nil) {
        
        obj = [@{
            @"enable" : device.enabled,
            @"deviceId": device.deviceID ?: @"",
            @"pushToken" : device.pushToken ?: @"",
            @"platform": device.platform ?: @"",
            @"version": device.sdkVersion  ?: @"",
            @"productName" : device.productName  ?: @"",
            @"productVersion" : device.productVersion  ?: @"",
            @"osName": device.osName  ?: @"",
            @"osVersion": device.osVersion  ?: @"",
            @"deviceBrand": device.deviceBrand ?: @"",
            @"deviceModel": device.deviceModel ?: @"",
            @"operator": device.carrier ?: @"",
            @"deviceType": device.deviceType ?: @"",
            @"appVersion": device.appVersion ?: @"",
            @"locale": device.locale ?: @"",
            @"timeZone": device.timeZone ?: @"",
            @"timeOffset": @(device.timeOffset) ?: 0L,
            @"externalApps": [self jsonArrayFromExternalApps: device.externalApplications],
            @"externalCode": device.externalCode ?: @""
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSArray<NSDictionary *>*) jsonArrayFromExternalApps: (NSArray<INCoreExternalApp *>*)externalApps{
    NSArray<NSDictionary *>* array = [NSArray new];
    @try{
        if (externalApps != nil){
            for (int i=0;i<externalApps.count;i++){
                NSDictionary *obj = [[NSDictionary alloc]init];
                obj = @{
                @"id" : @(externalApps[i].iNExternalAppId) ?: 0,
                @"name": externalApps[i].name ?: @"",
                @"code" : externalApps[i].iosCode ?: @"",
                };
                array = [array arrayByAddingObject:obj];
            }
        }
    } @catch (NSException *exception) {
        NSLog(@"Error jsonArrayFromExternalApps ");
    }
    return array;
}

+ (NSArray<NSDictionary *>*) jsonArrayFromtopics: (NSArray<INTopic *>*) topics{
    NSArray<NSDictionary *>* array = [NSArray new];
    @try{
        for (int i=0;i<topics.count;i++){
            NSDictionary *obj = [[NSDictionary alloc]init];
            obj = @{
            @"code" : topics[i].code ?: @"",
            @"name": topics[i].name ?: @"",
            @"parentCode" : topics[i].parentCode ?: @"",
            @"visible" :@(topics[i].visible) ?: false,
            @"subscribed" :@(topics[i].subscribed) ?: false
            };
            array = [array arrayByAddingObject:obj];
            
        }
    } @catch (NSException *exception) {
        NSLog(@"Error jsonArrayFromtopics %@", exception);
    }
    return array;
}

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