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

OpenAPI_local_origin_t *OpenAPI_local_origin_create(
    char *coordinate_id,
    OpenAPI_geographical_coordinates_t *point,
    OpenAPI_geographic_area_t *area,
    bool is_horiz_axes_orientation,
    int horiz_axes_orientation
)
{
    OpenAPI_local_origin_t *local_origin_local_var = ogs_malloc(sizeof(OpenAPI_local_origin_t));
    ogs_assert(local_origin_local_var);

    local_origin_local_var->coordinate_id = coordinate_id;
    local_origin_local_var->point = point;
    local_origin_local_var->area = area;
    local_origin_local_var->is_horiz_axes_orientation = is_horiz_axes_orientation;
    local_origin_local_var->horiz_axes_orientation = horiz_axes_orientation;

    return local_origin_local_var;
}

void OpenAPI_local_origin_free(OpenAPI_local_origin_t *local_origin)
{
    OpenAPI_lnode_t *node = NULL;

    if (NULL == local_origin) {
        return;
    }
    if (local_origin->coordinate_id) {
        ogs_free(local_origin->coordinate_id);
        local_origin->coordinate_id = NULL;
    }
    if (local_origin->point) {
        OpenAPI_geographical_coordinates_free(local_origin->point);
        local_origin->point = NULL;
    }
    if (local_origin->area) {
        OpenAPI_geographic_area_free(local_origin->area);
        local_origin->area = NULL;
    }
    ogs_free(local_origin);
}

cJSON *OpenAPI_local_origin_convertToJSON(OpenAPI_local_origin_t *local_origin)
{
    cJSON *item = NULL;
    OpenAPI_lnode_t *node = NULL;

    if (local_origin == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [LocalOrigin]");
        return NULL;
    }

    item = cJSON_CreateObject();
    if (!local_origin->coordinate_id) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [coordinate_id]");
        return NULL;
    }
    if (cJSON_AddStringToObject(item, "coordinateId", local_origin->coordinate_id) == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [coordinate_id]");
        goto end;
    }

    if (local_origin->point) {
    cJSON *point_local_JSON = OpenAPI_geographical_coordinates_convertToJSON(local_origin->point);
    if (point_local_JSON == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [point]");
        goto end;
    }
    cJSON_AddItemToObject(item, "point", point_local_JSON);
    if (item->child == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [point]");
        goto end;
    }
    }

    if (local_origin->area) {
    cJSON *area_local_JSON = OpenAPI_geographic_area_convertToJSON(local_origin->area);
    if (area_local_JSON == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [area]");
        goto end;
    }
    cJSON_AddItemToObject(item, "area", area_local_JSON);
    if (item->child == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [area]");
        goto end;
    }
    }

    if (local_origin->is_horiz_axes_orientation) {
    if (cJSON_AddNumberToObject(item, "horizAxesOrientation", local_origin->horiz_axes_orientation) == NULL) {
        ogs_error("OpenAPI_local_origin_convertToJSON() failed [horiz_axes_orientation]");
        goto end;
    }
    }

end:
    return item;
}

OpenAPI_local_origin_t *OpenAPI_local_origin_parseFromJSON(cJSON *local_originJSON)
{
    OpenAPI_local_origin_t *local_origin_local_var = NULL;
    OpenAPI_lnode_t *node = NULL;
    cJSON *coordinate_id = NULL;
    cJSON *point = NULL;
    OpenAPI_geographical_coordinates_t *point_local_nonprim = NULL;
    cJSON *area = NULL;
    OpenAPI_geographic_area_t *area_local_nonprim = NULL;
    cJSON *horiz_axes_orientation = NULL;
    coordinate_id = cJSON_GetObjectItemCaseSensitive(local_originJSON, "coordinateId");
    if (!coordinate_id) {
        ogs_error("OpenAPI_local_origin_parseFromJSON() failed [coordinate_id]");
        goto end;
    }
    if (!cJSON_IsString(coordinate_id)) {
        ogs_error("OpenAPI_local_origin_parseFromJSON() failed [coordinate_id]");
        goto end;
    }

    point = cJSON_GetObjectItemCaseSensitive(local_originJSON, "point");
    if (point) {
    point_local_nonprim = OpenAPI_geographical_coordinates_parseFromJSON(point);
    if (!point_local_nonprim) {
        ogs_error("OpenAPI_geographical_coordinates_parseFromJSON failed [point]");
        goto end;
    }
    }

    area = cJSON_GetObjectItemCaseSensitive(local_originJSON, "area");
    if (area) {
    area_local_nonprim = OpenAPI_geographic_area_parseFromJSON(area);
    if (!area_local_nonprim) {
        ogs_error("OpenAPI_geographic_area_parseFromJSON failed [area]");
        goto end;
    }
    }

    horiz_axes_orientation = cJSON_GetObjectItemCaseSensitive(local_originJSON, "horizAxesOrientation");
    if (horiz_axes_orientation) {
    if (!cJSON_IsNumber(horiz_axes_orientation)) {
        ogs_error("OpenAPI_local_origin_parseFromJSON() failed [horiz_axes_orientation]");
        goto end;
    }
    }

    local_origin_local_var = OpenAPI_local_origin_create (
        ogs_strdup(coordinate_id->valuestring),
        point ? point_local_nonprim : NULL,
        area ? area_local_nonprim : NULL,
        horiz_axes_orientation ? true : false,
        horiz_axes_orientation ? horiz_axes_orientation->valuedouble : 0
    );

    return local_origin_local_var;
end:
    if (point_local_nonprim) {
        OpenAPI_geographical_coordinates_free(point_local_nonprim);
        point_local_nonprim = NULL;
    }
    if (area_local_nonprim) {
        OpenAPI_geographic_area_free(area_local_nonprim);
        area_local_nonprim = NULL;
    }
    return NULL;
}

OpenAPI_local_origin_t *OpenAPI_local_origin_copy(OpenAPI_local_origin_t *dst, OpenAPI_local_origin_t *src)
{
    cJSON *item = NULL;
    char *content = NULL;

    ogs_assert(src);
    item = OpenAPI_local_origin_convertToJSON(src);
    if (!item) {
        ogs_error("OpenAPI_local_origin_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_local_origin_free(dst);
    dst = OpenAPI_local_origin_parseFromJSON(item);
    cJSON_Delete(item);

    return dst;
}

