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

char *OpenAPI_condition_typenf_group_list_cond_ToString(OpenAPI_nf_group_list_cond_condition_type_e condition_type)
{
    const char *condition_typeArray[] =  { "NULL", "NF_GROUP_LIST_COND" };
    size_t sizeofArray = sizeof(condition_typeArray) / sizeof(condition_typeArray[0]);
    if (condition_type < sizeofArray)
        return (char *)condition_typeArray[condition_type];
    else
        return (char *)"Unknown";
}

OpenAPI_nf_group_list_cond_condition_type_e OpenAPI_condition_typenf_group_list_cond_FromString(char* condition_type)
{
    int stringToReturn = 0;
    const char *condition_typeArray[] =  { "NULL", "NF_GROUP_LIST_COND" };
    size_t sizeofArray = sizeof(condition_typeArray) / sizeof(condition_typeArray[0]);
    while (stringToReturn < sizeofArray) {
        if (strcmp(condition_type, condition_typeArray[stringToReturn]) == 0) {
            return stringToReturn;
        }
        stringToReturn++;
    }
    return 0;
}
char *OpenAPI_nf_typenf_group_list_cond_ToString(OpenAPI_nf_group_list_cond_nf_type_e nf_type)
{
    const char *nf_typeArray[] =  { "NULL", "UDM", "AUSF", "UDR", "PCF", "CHF", "HSS" };
    size_t sizeofArray = sizeof(nf_typeArray) / sizeof(nf_typeArray[0]);
    if (nf_type < sizeofArray)
        return (char *)nf_typeArray[nf_type];
    else
        return (char *)"Unknown";
}

OpenAPI_nf_group_list_cond_nf_type_e OpenAPI_nf_typenf_group_list_cond_FromString(char* nf_type)
{
    int stringToReturn = 0;
    const char *nf_typeArray[] =  { "NULL", "UDM", "AUSF", "UDR", "PCF", "CHF", "HSS" };
    size_t sizeofArray = sizeof(nf_typeArray) / sizeof(nf_typeArray[0]);
    while (stringToReturn < sizeofArray) {
        if (strcmp(nf_type, nf_typeArray[stringToReturn]) == 0) {
            return stringToReturn;
        }
        stringToReturn++;
    }
    return 0;
}
OpenAPI_nf_group_list_cond_t *OpenAPI_nf_group_list_cond_create(
    OpenAPI_nf_group_list_cond_condition_type_e condition_type,
    OpenAPI_nf_group_list_cond_nf_type_e nf_type,
    OpenAPI_list_t *nf_group_id_list
)
{
    OpenAPI_nf_group_list_cond_t *nf_group_list_cond_local_var = ogs_malloc(sizeof(OpenAPI_nf_group_list_cond_t));
    ogs_assert(nf_group_list_cond_local_var);

    nf_group_list_cond_local_var->condition_type = condition_type;
    nf_group_list_cond_local_var->nf_type = nf_type;
    nf_group_list_cond_local_var->nf_group_id_list = nf_group_id_list;

    return nf_group_list_cond_local_var;
}

void OpenAPI_nf_group_list_cond_free(OpenAPI_nf_group_list_cond_t *nf_group_list_cond)
{
    OpenAPI_lnode_t *node = NULL;

    if (NULL == nf_group_list_cond) {
        return;
    }
    if (nf_group_list_cond->nf_group_id_list) {
        OpenAPI_list_for_each(nf_group_list_cond->nf_group_id_list, node) {
            ogs_free(node->data);
        }
        OpenAPI_list_free(nf_group_list_cond->nf_group_id_list);
        nf_group_list_cond->nf_group_id_list = NULL;
    }
    ogs_free(nf_group_list_cond);
}

cJSON *OpenAPI_nf_group_list_cond_convertToJSON(OpenAPI_nf_group_list_cond_t *nf_group_list_cond)
{
    cJSON *item = NULL;
    OpenAPI_lnode_t *node = NULL;

    if (nf_group_list_cond == NULL) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [NfGroupListCond]");
        return NULL;
    }

    item = cJSON_CreateObject();
    if (nf_group_list_cond->condition_type == OpenAPI_nf_group_list_cond_CONDITIONTYPE_NULL) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [condition_type]");
        return NULL;
    }
    if (cJSON_AddStringToObject(item, "conditionType", OpenAPI_condition_typenf_group_list_cond_ToString(nf_group_list_cond->condition_type)) == NULL) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [condition_type]");
        goto end;
    }

    if (nf_group_list_cond->nf_type == OpenAPI_nf_group_list_cond_NFTYPE_NULL) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [nf_type]");
        return NULL;
    }
    if (cJSON_AddStringToObject(item, "nfType", OpenAPI_nf_typenf_group_list_cond_ToString(nf_group_list_cond->nf_type)) == NULL) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [nf_type]");
        goto end;
    }

    if (!nf_group_list_cond->nf_group_id_list) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [nf_group_id_list]");
        return NULL;
    }
    cJSON *nf_group_id_listList = cJSON_AddArrayToObject(item, "nfGroupIdList");
    if (nf_group_id_listList == NULL) {
        ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [nf_group_id_list]");
        goto end;
    }
    OpenAPI_list_for_each(nf_group_list_cond->nf_group_id_list, node) {
        if (cJSON_AddStringToObject(nf_group_id_listList, "", (char*)node->data) == NULL) {
            ogs_error("OpenAPI_nf_group_list_cond_convertToJSON() failed [nf_group_id_list]");
            goto end;
        }
    }

end:
    return item;
}

OpenAPI_nf_group_list_cond_t *OpenAPI_nf_group_list_cond_parseFromJSON(cJSON *nf_group_list_condJSON)
{
    OpenAPI_nf_group_list_cond_t *nf_group_list_cond_local_var = NULL;
    OpenAPI_lnode_t *node = NULL;
    cJSON *condition_type = NULL;
    OpenAPI_nf_group_list_cond_condition_type_e condition_typeVariable = 0;
    cJSON *nf_type = NULL;
    OpenAPI_nf_group_list_cond_nf_type_e nf_typeVariable = 0;
    cJSON *nf_group_id_list = NULL;
    OpenAPI_list_t *nf_group_id_listList = NULL;
    condition_type = cJSON_GetObjectItemCaseSensitive(nf_group_list_condJSON, "conditionType");
    if (!condition_type) {
        ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [condition_type]");
        goto end;
    }
    if (!cJSON_IsString(condition_type)) {
        ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [condition_type]");
        goto end;
    }
    condition_typeVariable = OpenAPI_condition_typenf_group_list_cond_FromString(condition_type->valuestring);

    nf_type = cJSON_GetObjectItemCaseSensitive(nf_group_list_condJSON, "nfType");
    if (!nf_type) {
        ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [nf_type]");
        goto end;
    }
    if (!cJSON_IsString(nf_type)) {
        ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [nf_type]");
        goto end;
    }
    nf_typeVariable = OpenAPI_nf_typenf_group_list_cond_FromString(nf_type->valuestring);

    nf_group_id_list = cJSON_GetObjectItemCaseSensitive(nf_group_list_condJSON, "nfGroupIdList");
    if (!nf_group_id_list) {
        ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [nf_group_id_list]");
        goto end;
    }
        cJSON *nf_group_id_list_local = NULL;
        if (!cJSON_IsArray(nf_group_id_list)) {
            ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [nf_group_id_list]");
            goto end;
        }

        nf_group_id_listList = OpenAPI_list_create();

        cJSON_ArrayForEach(nf_group_id_list_local, nf_group_id_list) {
            double *localDouble = NULL;
            int *localInt = NULL;
            if (!cJSON_IsString(nf_group_id_list_local)) {
                ogs_error("OpenAPI_nf_group_list_cond_parseFromJSON() failed [nf_group_id_list]");
                goto end;
            }
            OpenAPI_list_add(nf_group_id_listList, ogs_strdup(nf_group_id_list_local->valuestring));
        }

    nf_group_list_cond_local_var = OpenAPI_nf_group_list_cond_create (
        condition_typeVariable,
        nf_typeVariable,
        nf_group_id_listList
    );

    return nf_group_list_cond_local_var;
end:
    if (nf_group_id_listList) {
        OpenAPI_list_for_each(nf_group_id_listList, node) {
            ogs_free(node->data);
        }
        OpenAPI_list_free(nf_group_id_listList);
        nf_group_id_listList = NULL;
    }
    return NULL;
}

OpenAPI_nf_group_list_cond_t *OpenAPI_nf_group_list_cond_copy(OpenAPI_nf_group_list_cond_t *dst, OpenAPI_nf_group_list_cond_t *src)
{
    cJSON *item = NULL;
    char *content = NULL;

    ogs_assert(src);
    item = OpenAPI_nf_group_list_cond_convertToJSON(src);
    if (!item) {
        ogs_error("OpenAPI_nf_group_list_cond_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_nf_group_list_cond_free(dst);
    dst = OpenAPI_nf_group_list_cond_parseFromJSON(item);
    cJSON_Delete(item);

    return dst;
}

