#include "restcurl.h"
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h"

struct url_data {
    size_t size;
    char* data;
};

static size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) {
    size_t index = data->size;
    size_t n = (size * nmemb);
    char* tmp;

    data->size += (size * nmemb);

    tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */

    if(tmp) {
        data->data = tmp;
    } else {
        if(data->data) {
            free(data->data);
        }
        fprintf(stderr, "Failed to allocate memory.\n");
        return 0;
    }

    memcpy((data->data + index), ptr, n);
    data->data[data->size] = '\0';

    return size * nmemb;
}

static char *handle_url(char *url) {
    CURL *curl;

    struct url_data data;
    data.size = 0;
    data.data = malloc(4096); /* reasonable size initial buffer */
    if(NULL == data.data) {
        fprintf(stderr, "Failed to allocate memory.\n");
        return NULL;
    }

    data.data[0] = '\0';

    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
                fprintf(stderr, "curl_easy_perform() failed: %s\n",  
                        curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);

    }
    return data.data;
}

int getRestSensor(sensor *sensor) {
    char url[25];
    strcpy(url, "http://");
    strcat(url, sensor->ip);
    char *data = handle_url(url);
    if (data) {
        cJSON *jsondata = cJSON_Parse(data);
        cJSON *variables = cJSON_GetObjectItemCaseSensitive(jsondata, "variables");
        
        //These two are in the catergory variables
        cJSON *temperature = cJSON_GetObjectItemCaseSensitive(variables, "temperature");
        cJSON *humidity = cJSON_GetObjectItemCaseSensitive(variables, "humidity");
        
        //The ID is stored in the JSON root
        cJSON *id = cJSON_GetObjectItemCaseSensitive(jsondata, "id");
        
        if (temperature != NULL && humidity != NULL && id != NULL) {
            sensor->temperature = (float)temperature->valuedouble;
            sensor->humidity = (float)humidity->valuedouble;
            sensor->node_id = atoi(id->valuestring);
        } else {
            //free
            cJSON_Delete(jsondata);
            free(data);
            return 0;
        }
        //free
        cJSON_Delete(jsondata);
        free(data);
        return 1;
    }
    return 0;
}

