#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pico/stdlib.h"

typedef struct RESTFUL_REQUEST_DATA_ {
    char *method;
    char *endpoint;
    char *json_body;
    char *key;
    char *on_value;
    char *off_value;
} RESTFUL_REQUEST_DATA;

typedef struct TILE_ {
    char *name;
    char image_idx;
    RESTFUL_REQUEST_DATA *action_request;
    RESTFUL_REQUEST_DATA *status_request;
} TILE;

static const char tile_data[102] = {
    0x06, 0x6f, 0x66, 0x66, 0x69, 0x63, 0x65, 0x01, 0x04, 0x50, 0x4f, 0x53, 0x54, 0x11, 0x2f, 0x76, 0x32, 0x2f, 0x6d, 0x65, 0x72, 0x6f, 0x73, 0x73, 0x2f, 0x6f, 0x66, 0x66, 0x69, 0x63, 0x65, 0x12, 0x7b, 0x22, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x3a, 0x20, 0x22, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x22, 0x7d, 0x04, 0x50, 0x4f, 0x53, 0x54, 0x11, 0x2f, 0x76, 0x32, 0x2f, 0x6d, 0x65, 0x72, 0x6f, 0x73, 0x73, 0x2f, 0x6f, 0x66, 0x66, 0x69, 0x63, 0x65, 0x12, 0x7b, 0x22, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x3a, 0x20, 0x22, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x7d, 0x05, 0x6f, 0x6e, 0x6f, 0x66, 0x66, 0x01, 0x31, 0x01, 0x30
};

char make_str(char **dest, const char *src) {
    uint8_t str_size = (uint8_t)*src++;
    *dest = (char *)malloc(str_size * sizeof(char) + 1);
    strncpy(*dest, src, str_size);
    (*dest)[str_size] = '\0';  // Null terminate
    return str_size + 1;
}

void make_tile() {
    uint ptr = 0;

    TILE *tile = (TILE *)malloc(sizeof(TILE));

    // Name
    ptr += make_str(&tile->name, (char *)&tile_data[ptr]);
    // Image idx
    tile->image_idx = tile_data[ptr++];

    // Action Request
    RESTFUL_REQUEST_DATA *action_request = (RESTFUL_REQUEST_DATA *)malloc(sizeof(RESTFUL_REQUEST_DATA));
    tile->action_request = action_request;

    ptr += make_str(&action_request->method, (char *)&tile_data[ptr]);
    ptr += make_str(&action_request->endpoint, (char *)&tile_data[ptr]);
    ptr += make_str(&action_request->json_body, (char *)&tile_data[ptr]);
    action_request->key = NULL;

    // Status Request
    RESTFUL_REQUEST_DATA *status_request = (RESTFUL_REQUEST_DATA *)malloc(sizeof(RESTFUL_REQUEST_DATA));
    tile->status_request = status_request;

    ptr += make_str(&status_request->method, (char *)&tile_data[ptr]);
    ptr += make_str(&status_request->endpoint, (char *)&tile_data[ptr]);
    ptr += make_str(&status_request->json_body, (char *)&tile_data[ptr]);
    ptr += make_str(&status_request->key, (char *)&tile_data[ptr]);
    ptr += make_str(&status_request->on_value, (char *)&tile_data[ptr]);
    ptr += make_str(&status_request->off_value, (char *)&tile_data[ptr]);

    printf("Name: %s\nImage Index: %d\nAction Request:\n    Method: %s\n    Endpoint: %s\n    JSON Body: %s\nStatus Request:\n    Method: %s\n    Endpoint: %s\n    JSON Body: %s\n    Key: %s\n    On Value: %s\n    Off Value: %s\n",
        tile->name,
        tile->image_idx,
        tile->action_request->method, tile->action_request->endpoint, tile->action_request->json_body,
        tile->status_request->method, tile->status_request->endpoint, tile->status_request->json_body, tile->status_request->key, tile->status_request->on_value, tile->status_request->off_value
    );
}

int main() {
    stdio_init_all();
    make_tile();
    return 0;
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT