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

OpenAPI_wireline_area_t *OpenAPI_wireline_area_create(
    OpenAPI_list_t *global_line_ids,
    OpenAPI_list_t *hfc_n_ids,
    char *area_code_b,
    char *area_code_c,
    OpenAPI_list_t *comb_gci_and_hfc_n_ids
)
{
    OpenAPI_wireline_area_t *wireline_area_local_var = ogs_malloc(sizeof(OpenAPI_wireline_area_t));
    ogs_assert(wireline_area_local_var);

    wireline_area_local_var->global_line_ids = global_line_ids;
    wireline_area_local_var->hfc_n_ids = hfc_n_ids;
    wireline_area_local_var->area_code_b = area_code_b;
    wireline_area_local_var->area_code_c = area_code_c;
    wireline_area_local_var->comb_gci_and_hfc_n_ids = comb_gci_and_hfc_n_ids;

    return wireline_area_local_var;
}

void OpenAPI_wireline_area_free(OpenAPI_wireline_area_t *wireline_area)
{
    OpenAPI_lnode_t *node = NULL;

    if (NULL == wireline_area) {
        return;
    }
    if (wireline_area->global_line_ids) {
        OpenAPI_list_for_each(wireline_area->global_line_ids, node) {
            ogs_free(node->data);
        }
        OpenAPI_list_free(wireline_area->global_line_ids);
        wireline_area->global_line_ids = NULL;
    }
    if (wireline_area->hfc_n_ids) {
        OpenAPI_list_for_each(wireline_area->hfc_n_ids, node) {
            ogs_free(node->data);
        }
        OpenAPI_list_free(wireline_area->hfc_n_ids);
        wireline_area->hfc_n_ids = NULL;
    }
    if (wireline_area->area_code_b) {
        ogs_free(wireline_area->area_code_b);
        wireline_area->area_code_b = NULL;
    }
    if (wireline_area->area_code_c) {
        ogs_free(wireline_area->area_code_c);
        wireline_area->area_code_c = NULL;
    }
    if (wireline_area->comb_gci_and_hfc_n_ids) {
        OpenAPI_list_for_each(wireline_area->comb_gci_and_hfc_n_ids, node) {
            OpenAPI_comb_gci_and_hfc_n_ids_free(node->data);
        }
        OpenAPI_list_free(wireline_area->comb_gci_and_hfc_n_ids);
        wireline_area->comb_gci_and_hfc_n_ids = NULL;
    }
    ogs_free(wireline_area);
}

cJSON *OpenAPI_wireline_area_convertToJSON(OpenAPI_wireline_area_t *wireline_area)
{
    cJSON *item = NULL;
    OpenAPI_lnode_t *node = NULL;

    if (wireline_area == NULL) {
        ogs_error("OpenAPI_wireline_area_convertToJSON() failed [WirelineArea]");
        return NULL;
    }

    item = cJSON_CreateObject();
    if (wireline_area->global_line_ids) {
    cJSON *global_line_idsList = cJSON_AddArrayToObject(item, "globalLineIds");
    if (global_line_idsList == NULL) {
        ogs_error("OpenAPI_wireline_area_convertToJSON() failed [global_line_ids]");
        goto end;
    }
    OpenAPI_list_for_each(wireline_area->global_line_ids, node) {
        if (cJSON_AddStringToObject(global_line_idsList, "", (char*)node->data) == NULL) {
            ogs_error("OpenAPI_wireline_area_convertToJSON() failed [global_line_ids]");
            goto end;
        }
    }
    }

    if (wireline_area->hfc_n_ids) {
    cJSON *hfc_n_idsList = cJSON_AddArrayToObject(item, "hfcNIds");
    if (hfc_n_idsList == NULL) {
        ogs_error("OpenAPI_wireline_area_convertToJSON() failed [hfc_n_ids]");
        goto end;
    }
    OpenAPI_list_for_each(wireline_area->hfc_n_ids, node) {
        if (cJSON_AddStringToObject(hfc_n_idsList, "", (char*)node->data) == NULL) {
            ogs_error("OpenAPI_wireline_area_convertToJSON() failed [hfc_n_ids]");
            goto end;
        }
    }
    }

    if (wireline_area->area_code_b) {
    if (cJSON_AddStringToObject(item, "areaCodeB", wireline_area->area_code_b) == NULL) {
        ogs_error("OpenAPI_wireline_area_convertToJSON() failed [area_code_b]");
        goto end;
    }
    }

    if (wireline_area->area_code_c) {
    if (cJSON_AddStringToObject(item, "areaCodeC", wireline_area->area_code_c) == NULL) {
        ogs_error("OpenAPI_wireline_area_convertToJSON() failed [area_code_c]");
        goto end;
    }
    }

    if (wireline_area->comb_gci_and_hfc_n_ids) {
    cJSON *comb_gci_and_hfc_n_idsList = cJSON_AddArrayToObject(item, "combGciAndHfcNIds");
    if (comb_gci_and_hfc_n_idsList == NULL) {
        ogs_error("OpenAPI_wireline_area_convertToJSON() failed [comb_gci_and_hfc_n_ids]");
        goto end;
    }
    OpenAPI_list_for_each(wireline_area->comb_gci_and_hfc_n_ids, node) {
        cJSON *itemLocal = OpenAPI_comb_gci_and_hfc_n_ids_convertToJSON(node->data);
        if (itemLocal == NULL) {
            ogs_error("OpenAPI_wireline_area_convertToJSON() failed [comb_gci_and_hfc_n_ids]");
            goto end;
        }
        cJSON_AddItemToArray(comb_gci_and_hfc_n_idsList, itemLocal);
    }
    }

end:
    return item;
}

OpenAPI_wireline_area_t *OpenAPI_wireline_area_parseFromJSON(cJSON *wireline_areaJSON)
{
    OpenAPI_wireline_area_t *wireline_area_local_var = NULL;
    OpenAPI_lnode_t *node = NULL;
    cJSON *global_line_ids = NULL;
    OpenAPI_list_t *global_line_idsList = NULL;
    cJSON *hfc_n_ids = NULL;
    OpenAPI_list_t *hfc_n_idsList = NULL;
    cJSON *area_code_b = NULL;
    cJSON *area_code_c = NULL;
    cJSON *comb_gci_and_hfc_n_ids = NULL;
    OpenAPI_list_t *comb_gci_and_hfc_n_idsList = NULL;
    global_line_ids = cJSON_GetObjectItemCaseSensitive(wireline_areaJSON, "globalLineIds");
    if (global_line_ids) {
        cJSON *global_line_ids_local = NULL;
        if (!cJSON_IsArray(global_line_ids)) {
            ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [global_line_ids]");
            goto end;
        }

        global_line_idsList = OpenAPI_list_create();

        cJSON_ArrayForEach(global_line_ids_local, global_line_ids) {
            double *localDouble = NULL;
            int *localInt = NULL;
            if (!cJSON_IsString(global_line_ids_local)) {
                ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [global_line_ids]");
                goto end;
            }
            OpenAPI_list_add(global_line_idsList, ogs_strdup(global_line_ids_local->valuestring));
        }
    }

    hfc_n_ids = cJSON_GetObjectItemCaseSensitive(wireline_areaJSON, "hfcNIds");
    if (hfc_n_ids) {
        cJSON *hfc_n_ids_local = NULL;
        if (!cJSON_IsArray(hfc_n_ids)) {
            ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [hfc_n_ids]");
            goto end;
        }

        hfc_n_idsList = OpenAPI_list_create();

        cJSON_ArrayForEach(hfc_n_ids_local, hfc_n_ids) {
            double *localDouble = NULL;
            int *localInt = NULL;
            if (!cJSON_IsString(hfc_n_ids_local)) {
                ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [hfc_n_ids]");
                goto end;
            }
            OpenAPI_list_add(hfc_n_idsList, ogs_strdup(hfc_n_ids_local->valuestring));
        }
    }

    area_code_b = cJSON_GetObjectItemCaseSensitive(wireline_areaJSON, "areaCodeB");
    if (area_code_b) {
    if (!cJSON_IsString(area_code_b) && !cJSON_IsNull(area_code_b)) {
        ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [area_code_b]");
        goto end;
    }
    }

    area_code_c = cJSON_GetObjectItemCaseSensitive(wireline_areaJSON, "areaCodeC");
    if (area_code_c) {
    if (!cJSON_IsString(area_code_c) && !cJSON_IsNull(area_code_c)) {
        ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [area_code_c]");
        goto end;
    }
    }

    comb_gci_and_hfc_n_ids = cJSON_GetObjectItemCaseSensitive(wireline_areaJSON, "combGciAndHfcNIds");
    if (comb_gci_and_hfc_n_ids) {
        cJSON *comb_gci_and_hfc_n_ids_local = NULL;
        if (!cJSON_IsArray(comb_gci_and_hfc_n_ids)) {
            ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [comb_gci_and_hfc_n_ids]");
            goto end;
        }

        comb_gci_and_hfc_n_idsList = OpenAPI_list_create();

        cJSON_ArrayForEach(comb_gci_and_hfc_n_ids_local, comb_gci_and_hfc_n_ids) {
            if (!cJSON_IsObject(comb_gci_and_hfc_n_ids_local)) {
                ogs_error("OpenAPI_wireline_area_parseFromJSON() failed [comb_gci_and_hfc_n_ids]");
                goto end;
            }
            OpenAPI_comb_gci_and_hfc_n_ids_t *comb_gci_and_hfc_n_idsItem = OpenAPI_comb_gci_and_hfc_n_ids_parseFromJSON(comb_gci_and_hfc_n_ids_local);
            if (!comb_gci_and_hfc_n_idsItem) {
                ogs_error("No comb_gci_and_hfc_n_idsItem");
                goto end;
            }
            OpenAPI_list_add(comb_gci_and_hfc_n_idsList, comb_gci_and_hfc_n_idsItem);
        }
    }

    wireline_area_local_var = OpenAPI_wireline_area_create (
        global_line_ids ? global_line_idsList : NULL,
        hfc_n_ids ? hfc_n_idsList : NULL,
        area_code_b && !cJSON_IsNull(area_code_b) ? ogs_strdup(area_code_b->valuestring) : NULL,
        area_code_c && !cJSON_IsNull(area_code_c) ? ogs_strdup(area_code_c->valuestring) : NULL,
        comb_gci_and_hfc_n_ids ? comb_gci_and_hfc_n_idsList : NULL
    );

    return wireline_area_local_var;
end:
    if (global_line_idsList) {
        OpenAPI_list_for_each(global_line_idsList, node) {
            ogs_free(node->data);
        }
        OpenAPI_list_free(global_line_idsList);
        global_line_idsList = NULL;
    }
    if (hfc_n_idsList) {
        OpenAPI_list_for_each(hfc_n_idsList, node) {
            ogs_free(node->data);
        }
        OpenAPI_list_free(hfc_n_idsList);
        hfc_n_idsList = NULL;
    }
    if (comb_gci_and_hfc_n_idsList) {
        OpenAPI_list_for_each(comb_gci_and_hfc_n_idsList, node) {
            OpenAPI_comb_gci_and_hfc_n_ids_free(node->data);
        }
        OpenAPI_list_free(comb_gci_and_hfc_n_idsList);
        comb_gci_and_hfc_n_idsList = NULL;
    }
    return NULL;
}

OpenAPI_wireline_area_t *OpenAPI_wireline_area_copy(OpenAPI_wireline_area_t *dst, OpenAPI_wireline_area_t *src)
{
    cJSON *item = NULL;
    char *content = NULL;

    ogs_assert(src);
    item = OpenAPI_wireline_area_convertToJSON(src);
    if (!item) {
        ogs_error("OpenAPI_wireline_area_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_wireline_area_free(dst);
    dst = OpenAPI_wireline_area_parseFromJSON(item);
    cJSON_Delete(item);

    return dst;
}

