<commit_before>/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>

#include <kaa_error.h>
#include <platform/kaa_client.h>
#include <utilities/kaa_log.h>
#include <kaa_notification_manager.h>

#define KAA_DEMO_UNUSED(x) (void)(x);

static kaa_client_t *kaa_client = NULL;

enum color {RED, YELLOW, GREEN};

static const char *color_to_str(enum color code_color)
{
    switch(code_color) {
        case RED:
            return "CodeRed";
            break;
        case YELLOW:
            return "CodeYellow";
            break;
        case GREEN:
            return "CodeGreen";
            break;
        default:
            return "Incorrect value";
    }
}

void on_notification(void *context, uint64_t *topic_id, kaa_notification_t *notification)
{
    KAA_DEMO_UNUSED(context);
    if (notification->alert_message) {
            kaa_string_t *message = (kaa_string_t *)notification->alert_message;
            printf("Notification for topic id '%llu' received\n", *topic_id);
            printf("Notification body: %s\n", message->data);
            printf("Message alert type: %s\n", color_to_str(notification->alert_type));
    } else {
        printf("Error:Received notification's body is null\n");
    }
}

void show_topics(kaa_list_t *topics)
{
    if (!topics || !kaa_list_get_size(topics)) {
        printf("Topic list is empty");
        return;
    }

    kaa_list_node_t *it = kaa_list_begin(topics);
    while (it) {
        kaa_topic_t *topic = (kaa_topic_t *)kaa_list_get_data(it);
        printf("Topic: id '%llu', name: %s, type: ", topic->id, topic->name);
        if (topic->subscription_type == MANDATORY_SUBSCRIPTION) {
            printf("MANDATORY\n");
        } else {
            printf("OPTIONAL\n");
        }
        it = kaa_list_next(it);
    }
}

void on_topics_received(void *context, kaa_list_t *topics)
{
    printf("Topic list was updated\n");
    show_topics(topics);

    printf("Type topic ID in order to subscribe on one:");
    size_t topic_id;
    scanf("%zu", &topic_id);

    kaa_error_t err = KAA_ERR_NONE;
    kaa_client_t *client = (kaa_client_t *)context;
    kaa_list_node_t *it = kaa_list_begin(topics);
    while (it) {
        kaa_topic_t *topic = (kaa_topic_t *) kaa_list_get_data(it);
        if (topic->subscription_type == OPTIONAL_SUBSCRIPTION && topic->id == topic_id) {
            printf("Subscribing to optional topic '%llu'\n", topic->id);
            err = kaa_subscribe_to_topic(kaa_client_get_context(client)->notification_manager, &topic->id, false);
            if (err) {
                printf("Failed to subscribe.\n");
            }
        }
        it = kaa_list_next(it);
    }
    err = kaa_sync_topic_subscriptions(kaa_client_get_context(kaa_client)->notification_manager);
    if (err) {
        printf("Failed to sync subscriptions\n");
    }
}

int main(/*int argc, char *argv[]*/)<commit_msg>APP-248 Fix warnings in notification demo<commit_after>/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>

#include <kaa_error.h>
#include <platform/kaa_client.h>
#include <utilities/kaa_log.h>
#include <kaa_notification_manager.h>

#define KAA_DEMO_UNUSED(x) (void)(x);

static kaa_client_t *kaa_client = NULL;

enum color {RED, YELLOW, GREEN};

static const char *color_to_str(enum color code_color)
{
    switch(code_color) {
        case RED:
            return "CodeRed";
            break;
        case YELLOW:
            return "CodeYellow";
            break;
        case GREEN:
            return "CodeGreen";
            break;
        default:
            return "Incorrect value";
    }
}

void on_notification(void *context, uint64_t *topic_id, kaa_notification_t *notification)
{
    KAA_DEMO_UNUSED(context);
    if (notification->alert_message) {
            kaa_string_t *message = (kaa_string_t *)notification->alert_message;
            printf("Notification for topic id '%lu' received\n", *topic_id);
            printf("Notification body: %s\n", message->data);
            printf("Message alert type: %s\n", color_to_str(notification->alert_type));
    } else {
        printf("Error:Received notification's body is null\n");
    }
}

void show_topics(kaa_list_t *topics)
{
    if (!topics || !kaa_list_get_size(topics)) {
        printf("Topic list is empty");
        return;
    }

    kaa_list_node_t *it = kaa_list_begin(topics);
    while (it) {
        kaa_topic_t *topic = (kaa_topic_t *)kaa_list_get_data(it);
        printf("Topic: id '%lu', name: %s, type: ", topic->id, topic->name);
        if (topic->subscription_type == MANDATORY_SUBSCRIPTION) {
            printf("MANDATORY\n");
        } else {
            printf("OPTIONAL\n");
        }
        it = kaa_list_next(it);
    }
}

void on_topics_received(void *context, kaa_list_t *topics)
{
    printf("Topic list was updated\n");
    show_topics(topics);

    printf("Type topic ID in order to subscribe on one:");
    size_t topic_id;
    scanf("%zu", &topic_id);

    kaa_error_t err = KAA_ERR_NONE;
    kaa_client_t *client = (kaa_client_t *)context;
    kaa_list_node_t *it = kaa_list_begin(topics);
    while (it) {
        kaa_topic_t *topic = (kaa_topic_t *) kaa_list_get_data(it);
        if (topic->subscription_type == OPTIONAL_SUBSCRIPTION && topic->id == topic_id) {
            printf("Subscribing to optional topic '%lu'\n", topic->id);
            err = kaa_subscribe_to_topic(kaa_client_get_context(client)->notification_manager, &topic->id, false);
            if (err) {
                printf("Failed to subscribe.\n");
            }
        }
        it = kaa_list_next(it);
    }
    err = kaa_sync_topic_subscriptions(kaa_client_get_context(kaa_client)->notification_manager);
    if (err) {
        printf("Failed to sync subscriptions\n");
    }
}

int main(/*int argc, char *argv[]*/)
{'fraction_non_alphanumeric': 0.11907774497399129, 'fraction_numerical': 0.004358217348516801, 'mean_word_length': 2.928216454997239, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7544878', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2327, 'n_words': 574}