/* Build against the matching public header and sqs_map library. */
#include "sqs_map.h"
#include <stdio.h>
#include <string.h>

static int edit(sqsm_map_t map, const char *json, int print) {
    sqsm_map_bytes_t out = {0};
    int status = sqsm_map_edit_json(map, (const uint8_t *)json, (uint64_t)strlen(json), &out);
    if (status != 0) { fprintf(stderr, "%s\n", sqsm_map_last_error()); return status; }
    if (print) { fwrite(out.data, 1, (size_t)out.len, stdout); putchar('\n'); }
    return sqsm_map_bytes_free(out.allocation);
}

int main(void) {
    sqsm_map_t map = 0;
    uint64_t layer = 0;
    const char *empty = "{\"type\":\"FeatureCollection\",\"features\":[]}";
    if (sqsm_map_create(480, 360, &map) != 0) return 1;
    int status = sqsm_map_add_geojson(map, (const uint8_t *)empty, (uint64_t)strlen(empty), &layer);
    char begin[128];
    snprintf(begin, sizeof begin, "{\"method\":\"begin\",\"layerId\":%llu}", (unsigned long long)layer);
    if (!status) status = edit(map, begin, 0);
    if (!status) status = edit(map, "{\"method\":\"draw\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[116.4,39.9],[116.41,39.91]]}}", 0);
    if (!status) status = edit(map, "{\"method\":\"state\"}", 1);
    if (!status) status = edit(map, "{\"method\":\"undo\"}", 0);
    if (!status) status = edit(map, "{\"method\":\"redo\"}", 0);
    if (!status) status = edit(map, "{\"method\":\"getData\"}", 1);
    if (!status) status = edit(map, "{\"method\":\"apply\"}", 0);
    if (status) fprintf(stderr, "editing failed: %d\n", status);
    sqsm_map_destroy(map);
    return status ? 1 : 0;
}
