
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "snssai_upf_info_item.h"

OpenAPI_snssai_upf_info_item_t *OpenAPI_snssai_upf_info_item_create(
    OpenAPI_ext_snssai_t *s_nssai,
    OpenAPI_list_t *dnn_upf_info_list,
    bool is_redundant_transport,
    int redundant_transport,
    OpenAPI_list_t *interface_upf_info_list,
    bool is_dnn_upf_info_list_id,
    int dnn_upf_info_list_id
)
{
    OpenAPI_snssai_upf_info_item_t *snssai_upf_info_item_local_var = ogs_malloc(sizeof(OpenAPI_snssai_upf_info_item_t));
    ogs_assert(snssai_upf_info_item_local_var);

    snssai_upf_info_item_local_var->s_nssai = s_nssai;
    snssai_upf_info_item_local_var->dnn_upf_info_list = dnn_upf_info_list;
    snssai_upf_info_item_local_var->is_redundant_transport = is_redundant_transport;
    snssai_upf_info_item_local_var->redundant_transport = redundant_transport;
    snssai_upf_info_item_local_var->interface_upf_info_list = interface_upf_info_list;
    snssai_upf_info_item_local_var->is_dnn_upf_info_list_id = is_dnn_upf_info_list_id;
    snssai_upf_info_item_local_var->dnn_upf_info_list_id = dnn_upf_info_list_id;

    return snssai_upf_info_item_local_var;
}

void OpenAPI_snssai_upf_info_item_free(OpenAPI_snssai_upf_info_item_t *snssai_upf_info_item)
{
    OpenAPI_lnode_t *node = NULL;

    if (NULL == snssai_upf_info_item) {
        return;
    }
    if (snssai_upf_info_item->s_nssai) {
        OpenAPI_ext_snssai_free(snssai_upf_info_item->s_nssai);
        snssai_upf_info_item->s_nssai = NULL;
    }
    if (snssai_upf_info_item->dnn_upf_info_list) {
        OpenAPI_list_for_each(snssai_upf_info_item->dnn_upf_info_list, node) {
            OpenAPI_dnn_upf_info_item_free(node->data);
        }
        OpenAPI_list_free(snssai_upf_info_item->dnn_upf_info_list);
        snssai_upf_info_item->dnn_upf_info_list = NULL;
    }
    if (snssai_upf_info_item->interface_upf_info_list) {
        OpenAPI_list_for_each(snssai_upf_info_item->interface_upf_info_list, node) {
            OpenAPI_interface_upf_info_item_free(node->data);
        }
        OpenAPI_list_free(snssai_upf_info_item->interface_upf_info_list);
        snssai_upf_info_item->interface_upf_info_list = NULL;
    }
    ogs_free(snssai_upf_info_item);
}

cJSON *OpenAPI_snssai_upf_info_item_convertToJSON(OpenAPI_snssai_upf_info_item_t *snssai_upf_info_item)
{
    cJSON *item = NULL;
    OpenAPI_lnode_t *node = NULL;

    if (snssai_upf_info_item == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [SnssaiUpfInfoItem]");
        return NULL;
    }

    item = cJSON_CreateObject();
    if (!snssai_upf_info_item->s_nssai) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [s_nssai]");
        return NULL;
    }
    cJSON *s_nssai_local_JSON = OpenAPI_ext_snssai_convertToJSON(snssai_upf_info_item->s_nssai);
    if (s_nssai_local_JSON == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [s_nssai]");
        goto end;
    }
    cJSON_AddItemToObject(item, "sNssai", s_nssai_local_JSON);
    if (item->child == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [s_nssai]");
        goto end;
    }

    if (snssai_upf_info_item->dnn_upf_info_list) {
    cJSON *dnn_upf_info_listList = cJSON_AddArrayToObject(item, "dnnUpfInfoList");
    if (dnn_upf_info_listList == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [dnn_upf_info_list]");
        goto end;
    }
    OpenAPI_list_for_each(snssai_upf_info_item->dnn_upf_info_list, node) {
        cJSON *itemLocal = OpenAPI_dnn_upf_info_item_convertToJSON(node->data);
        if (itemLocal == NULL) {
            ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [dnn_upf_info_list]");
            goto end;
        }
        cJSON_AddItemToArray(dnn_upf_info_listList, itemLocal);
    }
    }

    if (snssai_upf_info_item->is_redundant_transport) {
    if (cJSON_AddBoolToObject(item, "redundantTransport", snssai_upf_info_item->redundant_transport) == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [redundant_transport]");
        goto end;
    }
    }

    if (snssai_upf_info_item->interface_upf_info_list) {
    cJSON *interface_upf_info_listList = cJSON_AddArrayToObject(item, "interfaceUpfInfoList");
    if (interface_upf_info_listList == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [interface_upf_info_list]");
        goto end;
    }
    OpenAPI_list_for_each(snssai_upf_info_item->interface_upf_info_list, node) {
        cJSON *itemLocal = OpenAPI_interface_upf_info_item_convertToJSON(node->data);
        if (itemLocal == NULL) {
            ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [interface_upf_info_list]");
            goto end;
        }
        cJSON_AddItemToArray(interface_upf_info_listList, itemLocal);
    }
    }

    if (snssai_upf_info_item->is_dnn_upf_info_list_id) {
    if (cJSON_AddNumberToObject(item, "dnnUpfInfoListId", snssai_upf_info_item->dnn_upf_info_list_id) == NULL) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed [dnn_upf_info_list_id]");
        goto end;
    }
    }

end:
    return item;
}

OpenAPI_snssai_upf_info_item_t *OpenAPI_snssai_upf_info_item_parseFromJSON(cJSON *snssai_upf_info_itemJSON)
{
    OpenAPI_snssai_upf_info_item_t *snssai_upf_info_item_local_var = NULL;
    OpenAPI_lnode_t *node = NULL;
    cJSON *s_nssai = NULL;
    OpenAPI_ext_snssai_t *s_nssai_local_nonprim = NULL;
    cJSON *dnn_upf_info_list = NULL;
    OpenAPI_list_t *dnn_upf_info_listList = NULL;
    cJSON *redundant_transport = NULL;
    cJSON *interface_upf_info_list = NULL;
    OpenAPI_list_t *interface_upf_info_listList = NULL;
    cJSON *dnn_upf_info_list_id = NULL;
    s_nssai = cJSON_GetObjectItemCaseSensitive(snssai_upf_info_itemJSON, "sNssai");
    if (!s_nssai) {
        ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [s_nssai]");
        goto end;
    }
    s_nssai_local_nonprim = OpenAPI_ext_snssai_parseFromJSON(s_nssai);
    if (!s_nssai_local_nonprim) {
        ogs_error("OpenAPI_ext_snssai_parseFromJSON failed [s_nssai]");
        goto end;
    }

    dnn_upf_info_list = cJSON_GetObjectItemCaseSensitive(snssai_upf_info_itemJSON, "dnnUpfInfoList");
    if (dnn_upf_info_list) {
        cJSON *dnn_upf_info_list_local = NULL;
        if (!cJSON_IsArray(dnn_upf_info_list)) {
            ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [dnn_upf_info_list]");
            goto end;
        }

        dnn_upf_info_listList = OpenAPI_list_create();

        cJSON_ArrayForEach(dnn_upf_info_list_local, dnn_upf_info_list) {
            if (!cJSON_IsObject(dnn_upf_info_list_local)) {
                ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [dnn_upf_info_list]");
                goto end;
            }
            OpenAPI_dnn_upf_info_item_t *dnn_upf_info_listItem = OpenAPI_dnn_upf_info_item_parseFromJSON(dnn_upf_info_list_local);
            if (!dnn_upf_info_listItem) {
                ogs_error("No dnn_upf_info_listItem");
                goto end;
            }
            OpenAPI_list_add(dnn_upf_info_listList, dnn_upf_info_listItem);
        }
    }

    redundant_transport = cJSON_GetObjectItemCaseSensitive(snssai_upf_info_itemJSON, "redundantTransport");
    if (redundant_transport) {
    if (!cJSON_IsBool(redundant_transport)) {
        ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [redundant_transport]");
        goto end;
    }
    }

    interface_upf_info_list = cJSON_GetObjectItemCaseSensitive(snssai_upf_info_itemJSON, "interfaceUpfInfoList");
    if (interface_upf_info_list) {
        cJSON *interface_upf_info_list_local = NULL;
        if (!cJSON_IsArray(interface_upf_info_list)) {
            ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [interface_upf_info_list]");
            goto end;
        }

        interface_upf_info_listList = OpenAPI_list_create();

        cJSON_ArrayForEach(interface_upf_info_list_local, interface_upf_info_list) {
            if (!cJSON_IsObject(interface_upf_info_list_local)) {
                ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [interface_upf_info_list]");
                goto end;
            }
            OpenAPI_interface_upf_info_item_t *interface_upf_info_listItem = OpenAPI_interface_upf_info_item_parseFromJSON(interface_upf_info_list_local);
            if (!interface_upf_info_listItem) {
                ogs_error("No interface_upf_info_listItem");
                goto end;
            }
            OpenAPI_list_add(interface_upf_info_listList, interface_upf_info_listItem);
        }
    }

    dnn_upf_info_list_id = cJSON_GetObjectItemCaseSensitive(snssai_upf_info_itemJSON, "dnnUpfInfoListId");
    if (dnn_upf_info_list_id) {
    if (!cJSON_IsNumber(dnn_upf_info_list_id)) {
        ogs_error("OpenAPI_snssai_upf_info_item_parseFromJSON() failed [dnn_upf_info_list_id]");
        goto end;
    }
    }

    snssai_upf_info_item_local_var = OpenAPI_snssai_upf_info_item_create (
        s_nssai_local_nonprim,
        dnn_upf_info_list ? dnn_upf_info_listList : NULL,
        redundant_transport ? true : false,
        redundant_transport ? redundant_transport->valueint : 0,
        interface_upf_info_list ? interface_upf_info_listList : NULL,
        dnn_upf_info_list_id ? true : false,
        dnn_upf_info_list_id ? dnn_upf_info_list_id->valuedouble : 0
    );

    return snssai_upf_info_item_local_var;
end:
    if (s_nssai_local_nonprim) {
        OpenAPI_ext_snssai_free(s_nssai_local_nonprim);
        s_nssai_local_nonprim = NULL;
    }
    if (dnn_upf_info_listList) {
        OpenAPI_list_for_each(dnn_upf_info_listList, node) {
            OpenAPI_dnn_upf_info_item_free(node->data);
        }
        OpenAPI_list_free(dnn_upf_info_listList);
        dnn_upf_info_listList = NULL;
    }
    if (interface_upf_info_listList) {
        OpenAPI_list_for_each(interface_upf_info_listList, node) {
            OpenAPI_interface_upf_info_item_free(node->data);
        }
        OpenAPI_list_free(interface_upf_info_listList);
        interface_upf_info_listList = NULL;
    }
    return NULL;
}

OpenAPI_snssai_upf_info_item_t *OpenAPI_snssai_upf_info_item_copy(OpenAPI_snssai_upf_info_item_t *dst, OpenAPI_snssai_upf_info_item_t *src)
{
    cJSON *item = NULL;
    char *content = NULL;

    ogs_assert(src);
    item = OpenAPI_snssai_upf_info_item_convertToJSON(src);
    if (!item) {
        ogs_error("OpenAPI_snssai_upf_info_item_convertToJSON() failed");
        return NULL;
    }

    content = cJSON_Print(item);
    cJSON_Delete(item);

    if (!content) {
        ogs_error("cJSON_Print() failed");
        return NULL;
    }

    item = cJSON_Parse(content);
    ogs_free(content);
    if (!item) {
        ogs_error("cJSON_Parse() failed");
        return NULL;
    }

    OpenAPI_snssai_upf_info_item_free(dst);
    dst = OpenAPI_snssai_upf_info_item_parseFromJSON(item);
    cJSON_Delete(item);

    return dst;
}

