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

OpenAPI_nid_info_t *OpenAPI_nid_info_create(
    OpenAPI_list_t *nid_list
)
{
    OpenAPI_nid_info_t *nid_info_local_var = ogs_malloc(sizeof(OpenAPI_nid_info_t));
    ogs_assert(nid_info_local_var);

    nid_info_local_var->nid_list = nid_list;

    return nid_info_local_var;
}

void OpenAPI_nid_info_free(OpenAPI_nid_info_t *nid_info)
{
    OpenAPI_lnode_t *node = NULL;

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

cJSON *OpenAPI_nid_info_convertToJSON(OpenAPI_nid_info_t *nid_info)
{
    cJSON *item = NULL;
    OpenAPI_lnode_t *node = NULL;

    if (nid_info == NULL) {
        ogs_error("OpenAPI_nid_info_convertToJSON() failed [NidInfo]");
        return NULL;
    }

    item = cJSON_CreateObject();
    if (!nid_info->nid_list) {
        ogs_error("OpenAPI_nid_info_convertToJSON() failed [nid_list]");
        return NULL;
    }
    cJSON *nid_listList = cJSON_AddArrayToObject(item, "nidList");
    if (nid_listList == NULL) {
        ogs_error("OpenAPI_nid_info_convertToJSON() failed [nid_list]");
        goto end;
    }
    OpenAPI_list_for_each(nid_info->nid_list, node) {
        if (cJSON_AddStringToObject(nid_listList, "", (char*)node->data) == NULL) {
            ogs_error("OpenAPI_nid_info_convertToJSON() failed [nid_list]");
            goto end;
        }
    }

end:
    return item;
}

OpenAPI_nid_info_t *OpenAPI_nid_info_parseFromJSON(cJSON *nid_infoJSON)
{
    OpenAPI_nid_info_t *nid_info_local_var = NULL;
    OpenAPI_lnode_t *node = NULL;
    cJSON *nid_list = NULL;
    OpenAPI_list_t *nid_listList = NULL;
    nid_list = cJSON_GetObjectItemCaseSensitive(nid_infoJSON, "nidList");
    if (!nid_list) {
        ogs_error("OpenAPI_nid_info_parseFromJSON() failed [nid_list]");
        goto end;
    }
        cJSON *nid_list_local = NULL;
        if (!cJSON_IsArray(nid_list)) {
            ogs_error("OpenAPI_nid_info_parseFromJSON() failed [nid_list]");
            goto end;
        }

        nid_listList = OpenAPI_list_create();

        cJSON_ArrayForEach(nid_list_local, nid_list) {
            double *localDouble = NULL;
            int *localInt = NULL;
            if (!cJSON_IsString(nid_list_local)) {
                ogs_error("OpenAPI_nid_info_parseFromJSON() failed [nid_list]");
                goto end;
            }
            OpenAPI_list_add(nid_listList, ogs_strdup(nid_list_local->valuestring));
        }

    nid_info_local_var = OpenAPI_nid_info_create (
        nid_listList
    );

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

OpenAPI_nid_info_t *OpenAPI_nid_info_copy(OpenAPI_nid_info_t *dst, OpenAPI_nid_info_t *src)
{
    cJSON *item = NULL;
    char *content = NULL;

    ogs_assert(src);
    item = OpenAPI_nid_info_convertToJSON(src);
    if (!item) {
        ogs_error("OpenAPI_nid_info_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_nid_info_free(dst);
    dst = OpenAPI_nid_info_parseFromJSON(item);
    cJSON_Delete(item);

    return dst;
}

