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

#import "CDVIndigitallInAppParse.h"

@implementation CDVIndigitallInAppParse

+ (NSDictionary *)jsonFromInAppShow: (INInApp *)inApp withError: (INError *) error {
    NSMutableDictionary * obj = [NSMutableDictionary new];
    if (inApp != nil) {
        obj = [@{
            @"inApp" :  [self jsonFromInApp:inApp],
            @"errorCode": @(error.errorCode),
            @"errorMessage" : error.message ?: @"",
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSDictionary *)jsonFromInApp: (INInApp *)inApp{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (inApp != nil) {
        
        obj = [@{
            @"inAppId" : @(inApp.inAppId)?: 0,
            @"InAppWidth": @(inApp.inAppWidth)?: 0,
            @"InAppHeight" : @(inApp.inAppHeight)?: 0,
            @"lastVersionId": @(inApp.lastVersionId)?: 0,
            @"code": inApp.code  ?: @"",
            @"properties" : [self jsonFromInAppProperties:inApp.properties]?: nil,
            @"inAppShow" : [self jsonFromInAppShow: inApp.inAppShow]?: nil,
            @"showOnce" : @(inApp.showOnce)?: 0,
            @"expiredDate": inApp.expiredDate  ?: @"",
            @"creationDate": inApp.creationDate  ?: @"",
            @"cacheTtl": @(inApp.cacheTtl)?: 0,
            @"customData": inApp.customData?: @"",
            @"version": @(inApp.version)?: 0,
            @"filters" :[self jsonFromInAppFilters: inApp.filters]?: nil,
            @"name" :inApp.name ?: @""
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSDictionary *)jsonFromInAppPageTopic:(INInAppPageTopic *) inAppPageTopic {
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (inAppPageTopic != nil) {
        obj = [@{
            @"currentPage": @(inAppPageTopic.currentPage)?: 0,
            @"total": @(inAppPageTopic.total)?: 0,
            @"pageSize": @(inAppPageTopic.pageSize)?: 0,
            @"totalPages": @(inAppPageTopic.totalPages)?: 0,
            @"topics": [self jsonArrayFromtopics:inAppPageTopic.topics]
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSDictionary *)jsonFromInAppFilters: (INInAppFilters *) filters {
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (filters != nil) {
        obj = [@{
            @"platforms" : filters.platforms?: @"",
            @"areas": filters.areas?: @"",
            @"audience" : filters.audience?: @"",
            @"topics": filters.topics.toJson ?: @"",
            @"externalApps": filters.externalApps ?: @"",
            @"deviceCodes" : filters.deviceCodes?: @"",
            @"deviceCodesActive" : @(filters.deviceCodesActive) ?: false,
            @"deviceTypes": filters.deviceTypes?: @"",
            @"browserTypes" : filters.browserTypes?: @"",
            @"activePushDevices" : @(filters.activePushDevices) ?: false
        } mutableCopy];
    }
    return obj;
}

+ (NSDictionary *)jsonFromInAppShow: (INInAppShow *)inAppShow{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (inAppShow != nil) {
        obj = [@{
            @"wasDismissed" : @(inAppShow.wasDismissed),
            @"timesShowed": @(inAppShow.timesShowed),
            @"timesClicked" : @(inAppShow.timesClicked)
        } mutableCopy];
    }
    return [obj mutableCopy];
}

+ (NSDictionary *)jsonFromInAppProperties: (INInAppProperties *)properties{
    NSMutableDictionary *obj = [[NSMutableDictionary alloc]init];
    if (properties != nil) {
        obj = [@{
            @"contentUrl" : properties.InAppContentUrl.absoluteString ?: @"",
            @"numberOfShows": @(properties.numberOfShows),
            @"numberOfClicks" : @(properties.numberOfClicks),
            @"dismissForever": @(properties.dismissForever),
            @"showTime": @(properties.showTime),
            @"action" : [self jsonFromAction:properties.action],
            @"borderRadius" : @(properties.layout.borderRadius),
        } mutableCopy];
    }
    return [obj mutableCopy];
}

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

+ (NSArray<NSDictionary *>*) jsonArrayFromtopics: (NSArray<INInAppTopic *>*) 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;
}

@end
