
#include "cJSON.h"
#include "config.h"
#include "logger.h"
#include "tcXHHW.h"

char* config_filename = "/home/root/etc/tcu_config.json";

tcu_config_t tcu_config;

static char* read_file(const char *filename) {
    FILE *file = NULL;
    long length = 0;
    char *content = NULL;
    size_t read_chars = 0;

    /* open in read binary mode */
    file = fopen(filename, "rb");
    if (file == NULL)
    {
        printd("filanem:%s\n", __FILE__, __LINE__, __FUNCTION__, filename);
        goto cleanup;
    }

    /* get the length */
    if (fseek(file, 0, SEEK_END) != 0)
    {
        printd("filanem:%s\n", __FILE__, __LINE__, __FUNCTION__, filename);
        goto cleanup;
    }
    length = ftell(file);
    if (length < 0)
    {
        printd("filanem:%s\n", __FILE__, __LINE__, __FUNCTION__, filename);
        goto cleanup;
    }
    if (fseek(file, 0, SEEK_SET) != 0)
    {
        printd("filanem:%s\n", __FILE__, __LINE__, __FUNCTION__, filename);
        goto cleanup;
    }

    /* allocate content buffer */
    content = (char*)malloc((size_t)length + sizeof(""));
    if (content == NULL)
    {
        printd("filanem:%s\n", __FILE__, __LINE__, __FUNCTION__, filename);
        goto cleanup;
    }

    /* read the file into memory */
    read_chars = fread(content, sizeof(char), (size_t)length, file);
    if ((long)read_chars != length)
    {
        printd("filanem:%s\n", __FILE__, __LINE__, __FUNCTION__, filename);
        free(content);
        content = NULL;
        goto cleanup;
    }
    content[read_chars] = '\0';


cleanup:
    if (file != NULL)
    {
        fclose(file);
    }

    return content;
}

static char* itoa_c(int value, char *buff, int radix)
{
    sprintf(buff, "%d", value);
    return buff;
}

static void tcu_config_load(cJSON *root, tcu_config_t* config)
{
    cJSON *modbus_hw  = cJSON_GetObjectItem(root,"modbus_hw");
    config->tcu_id = atoi(cJSON_GetObjectItem(root, "tcu_id")->valuestring);
    if (config->tcu_id == -1)
    {
        config->tcu_id = get_TCUID();
    }
    printi("tcu_id =%d\n",config->tcu_id);
    config->enable_modbus_hw = atoi(cJSON_GetObjectItem(root, "enable_modbus_hw")->valuestring);
    config->enable_hmi = atoi(cJSON_GetObjectItem(root, "enable_hmi")->valuestring);
    config->enable_ocpp = atoi(cJSON_GetObjectItem(root, "enable_ocpp")->valuestring);
    config->ast_tty = strdup(cJSON_GetObjectItem(root, "ast_tty")->valuestring);
    config->ifname = strdup(cJSON_GetObjectItem(root, "ifname")->valuestring);

    config->xhlog_ip = strdup(cJSON_GetObjectItem(root, "xhlog_ip")->valuestring);
    config->xhlog_port = atoi(cJSON_GetObjectItem(root, "xhlog_port")->valuestring);

    config->modbus_hw.serverip = strdup(cJSON_GetObjectItem(modbus_hw, "serverip")->valuestring);
    config->modbus_hw.port = strdup(cJSON_GetObjectItem(modbus_hw, "port")->valuestring);
    config->modbus_hw.enable_ssl = atoi(cJSON_GetObjectItem(modbus_hw, "enable_ssl")->valuestring);
    config->modbus_hw.maxI_GUNA = atoi(cJSON_GetObjectItem(modbus_hw, "maxI_GUNA")->valuestring);
    config->modbus_hw.maxI_GUNB = atoi(cJSON_GetObjectItem(modbus_hw, "maxI_GUNB")->valuestring);
}

void tcu_config_init(const char* filename)
{
    memset(&tcu_config, 0, sizeof(tcu_config));
    char *content=read_file(filename);
	if (content!=NULL)
	{
		cJSON *root = cJSON_Parse(content);
        if (root != NULL)
		{
			tcu_config_load(root, &tcu_config);
            cJSON_Delete(root);
		}
        free(content);
	}
}

void tcu_config_save(const char* filename)
{
    char buff[256];

    tcu_config_t* config = &tcu_config;
    cJSON* root = cJSON_CreateObject();
    cJSON* modbus_hw = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "tcu_id", itoa_c(config->tcu_id, buff, 10));
    cJSON_AddStringToObject(root, "enable_modbus_hw", itoa_c(config->enable_modbus_hw, buff, 10));
    cJSON_AddStringToObject(root, "enable_hmi", itoa_c(config->enable_hmi, buff, 10));
    cJSON_AddStringToObject(root, "enable_ocpp", itoa_c(config->enable_ocpp, buff, 10));
    cJSON_AddStringToObject(root, "ast_tty", config->ast_tty);
    cJSON_AddStringToObject(root, "ifname", config->ifname);

    cJSON_AddStringToObject(root, "xhlog_ip", config->xhlog_ip);
    cJSON_AddStringToObject(root, "xhlog_port", itoa_c(config->xhlog_port, buff, 10));

    cJSON_AddStringToObject(modbus_hw, "serverip", config->modbus_hw.serverip);
    cJSON_AddStringToObject(modbus_hw, "port", config->modbus_hw.port);
    cJSON_AddStringToObject(modbus_hw, "enable_ssl", itoa_c(config->modbus_hw.enable_ssl, buff, 10));

    cJSON_AddItemToObject(root, "modbus_hw", modbus_hw);

    char *content = cJSON_Print(root);
    FILE *fp = fopen(filename, "wb");
    if (fp !=NULL)
    {
        fwrite(content, 1, strlen(content), fp);
        fclose(fp);
    }else{
        printd("\n", __FILE__, __LINE__, __FUNCTION__);
    }
    free(content);
}

void tcu_config_exit(void)
{
    tcu_config_save(config_filename);
}