#include "rpc_job_parser.h"
#include "cJSON.h"
#include <string.h>

bool rpc_job_parse(const char *json, ota_job_t *job)
{
    cJSON *root = cJSON_Parse(json);
    if (!root) return false;

    cJSON *state   = cJSON_GetObjectItem(root, "state");
    cJSON *desired = cJSON_GetObjectItem(state, "desired");
    cJSON *info    = cJSON_GetObjectItem(desired, "otaInfo");
    cJSON *meta    = cJSON_GetObjectItem(desired, "metadata");

    job->l_ota_status = cJSON_GetObjectItem(info, "l_ota_status")->valueint;
    job->deviceType   = cJSON_GetObjectItem(info, "deviceType")->valueint;
    job->otaType      = cJSON_GetObjectItem(info, "otaType")->valueint;

    strcpy(job->commitHash,
           cJSON_GetObjectItem(info, "commitHash")->valuestring);

    strcpy(job->jobId,
           cJSON_GetObjectItem(meta, "jobId")->valuestring);

    cJSON_Delete(root);
    return true;
}
