#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "parse_abnormal_json.h"
#include "tag.h"

const char *property_name[] =
{
    "realdata",
    "system",
    "alarm",
};

PROPERTY_TYPE get_property_by_string(char *tagTypeString)
{
    for (PROPERTY_TYPE i = PROPERTY_REALDATA ; i < PROPERTY_MAX ; i++)
    {
        if(strcmp(tagTypeString,property_name[i])==0)
        return i;
    }
    return PROPERTY_REALDATA;
}//TODO 上面代码和tag.c 重复了，需要合并


// 解析 JSON 文件
abnormal_point_t **parse_abnormal_points_from_json(const char *json_str, int *point_count) {
    cJSON *root = cJSON_Parse(json_str);
    if (!root) {
        fprintf(stderr, "JSON parse error: %s\n", cJSON_GetErrorPtr());
        return NULL;
    }

    // 获取 points 数组
    cJSON *points_array = cJSON_GetObjectItemCaseSensitive(root, "points");
    if (!cJSON_IsArray(points_array)) {
        fprintf(stderr, "points is not an array\n");
        cJSON_Delete(root);
        return NULL;
    }

    // 计算检测点总数
    *point_count = cJSON_GetArraySize(points_array);
    if (*point_count <= 0) {
        fprintf(stderr, "Invalid point count: %d\n", *point_count);
        cJSON_Delete(root);
        return NULL;
    }

    // 分配检测点指针数组
    abnormal_point_t **points = (abnormal_point_t **)malloc(*point_count * sizeof(abnormal_point_t *));
    if (!points) {
        fprintf(stderr, "Memory allocation failed\n");
        cJSON_Delete(root);
        *point_count = 0;
        return NULL;
    }

    // 遍历 points 数组
    int point_index = 0;
    cJSON *point_item;
    cJSON_ArrayForEach(point_item, points_array) {
        // 分配新检测点结构体
        abnormal_point_t *point = (abnormal_point_t *)calloc(1, sizeof(abnormal_point_t));
        points[point_index++] = point;

        // 解析基础字段
        cJSON *item;
        if ((item = cJSON_GetObjectItemCaseSensitive(point_item, "tag_name"))) {
            point->tag_name = strdup(item->valuestring);
        }
        if ((item = cJSON_GetObjectItemCaseSensitive(point_item, "device_name"))) {
            point->device_name = strdup(item->valuestring);
        }
        if ((item = cJSON_GetObjectItemCaseSensitive(point_item, "device_type"))) {
            point->device_type = strdup(item->valuestring);
        }
        if ((item = cJSON_GetObjectItemCaseSensitive(point_item, "property"))) {
            // 使用 get_property_by_string 将字符串属性转换为整数类型
            point->property = get_property_by_string(item->valuestring);
        }
        if ((item = cJSON_GetObjectItemCaseSensitive(point_item, "rule"))) {
            point->rule = strdup(item->valuestring);
        }
        if ((item = cJSON_GetObjectItemCaseSensitive(point_item, "description"))) {
            point->description = strdup(item->valuestring);
        }
    }

    cJSON_Delete(root);
    return points;
}

// 释放检测点内存
void free_abnormal_points(abnormal_point_t **points, int point_count) {
    for (int i = 0; i < point_count; i++) {
        abnormal_point_t *point = points[i];
        free(point->tag_name);
        free(point->device_name);
        free(point->device_type);
        // property 现在是整数类型，不需要释放
        free(point->rule);
        free(point->description);
        free(point);
    }
    free(points);
}