#include <stdio.h>
#include <string.h>
#include "mlogger.h"
#include "ocpp16_ChargingPrice.h"

void ocpp16_ChargingPrice_save(const char* filename, DataTransferData_t* data)
{
    int i;
    char cmdbuff[128];
    char strtime[128];

    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "type", data->type);
    if (strlen(data->timeZone) > 0) cJSON_AddStringToObject(root, "timeZone", data->timeZone);
    cJSON_AddStringToObject(root, "minAllowBalance", data->minAllowBalance);
    cJSON* price = cJSON_AddArrayToObject(root, "price");
    for (i=0; i<data->price.count; i++)
    {
        cJSON* items = cJSON_CreateObject();
        cJSON_AddStringToObject(items, "startTime", data->price.items[i].startTime);
        cJSON_AddStringToObject(items, "stopTime", data->price.items[i].stopTime);
        if (strlen(data->price.items[i].electrPrice) > 0) cJSON_AddStringToObject(items, "electrPrice", data->price.items[i].electrPrice);
        if (strlen(data->price.items[i].servicePrice) > 0) cJSON_AddStringToObject(items, "servicePrice", data->price.items[i].servicePrice);
        if (strlen(data->price.items[i].durationPrice) > 0) cJSON_AddStringToObject(items, "durationPrice", data->price.items[i].durationPrice);

        cJSON_AddItemToArray(price, items);
    }
    
    char *fbuff = cJSON_Print(root);
    FILE *fp = fopen(filename, "wb");
    if (fp != NULL)
    {
        fwrite(fbuff, 1, strlen(fbuff), fp);
        fclose(fp);
    }else{
        printd("open(%s, wb) failed.\n", filename);
    }
    free(fbuff);
    cJSON_Delete(root);
}

void ocpp16_ChargingPrice_load(const char* filename, DataTransferData_t* data)
{
    int i;
    memset(data, 0, sizeof(DataTransferData_t));
    FILE* file = fopen(filename, "rb");
    if (file)
    {
        fseek(file, 0, SEEK_END);
        int flen = ftell(file);
        fseek(file, 0, SEEK_SET);
        char* fbuff = malloc(flen+1);
        if (fbuff)
        {
            fread(fbuff, flen, 1, file);
            cJSON* root = cJSON_Parse(fbuff);
            if (root)
            {
                sprintf(data->type, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "type")));
                if (cJSON_HasObjectItem(root, "timeZone")) sprintf(data->timeZone, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "timeZone")));
                sprintf(data->minAllowBalance, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "minAllowBalance")));
                cJSON* price = cJSON_GetObjectItem(root, "price");
                data->price.count = cJSON_GetArraySize(price);
                for (i=0; i<data->price.count; i++)
                {
                    cJSON *items = cJSON_GetArrayItem(price, i);
                    sprintf(data->price.items[i].startTime, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(items, "startTime")));
                    sprintf(data->price.items[i].stopTime, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(items, "stopTime")));
                    if (cJSON_HasObjectItem(items, "electrPrice")) sprintf(data->price.items[i].electrPrice, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(items, "electrPrice")));
                    if (cJSON_HasObjectItem(items, "servicePrice")) sprintf(data->price.items[i].servicePrice, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(items, "servicePrice")));
                    if (cJSON_HasObjectItem(items, "durationPrice")) sprintf(data->price.items[i].durationPrice, "%s", cJSON_GetStringValue(cJSON_GetObjectItem(items, "durationPrice")));
                }
                cJSON_Delete(root);
            }else{
                printd("cJSON_Parse(%p) failed.\n", fbuff);
            }
            free(fbuff);
        }else{
            printd("malloc(%d) failed.\n", flen);
        }
        fclose(file);
    }else{
        printd("open(%s, rb) failed.\n", filename);
    }
}