#include "utils.h" 
#include "esp_log.h"
#include "esp_http_client.h"
#include <stdio.h>

static char *TAG = "utils";

// 在这里定义函数
void exampleFunction() {
   ESP_LOGW(TAG, "[exampleFunction] test");
}
 
char *Utils_STT(uint8_t *data, size_t len)
{
    ESP_LOGD(TAG, "start http len=%u", len);
    esp_http_client_config_t config = {
        .url = "http://110.42.98.230:8456/post",
        .timeout_ms = 60000, // 增加超时时间
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);
    char *result = NULL;

    esp_http_client_set_method(client, HTTP_METHOD_POST);

    esp_err_t err = esp_http_client_open(client, len);
    if (err == ESP_OK) {
        ESP_LOGW(TAG, "Failed to open client! - %d", err);
    }

    if (len > 0) {
        ESP_LOGW(TAG, "write client");
        esp_http_client_write(client, (const char *)data, len);
    }

    int content_length = esp_http_client_fetch_headers(client);
    if (esp_http_client_is_chunked_response(client)) {
        ESP_LOGW(TAG, "http_client_is_chunked - YES");
        esp_http_client_get_chunk_length(client, &content_length);
    }
    ESP_LOGW(TAG, "content_length=%d", content_length);
    result = (char *)malloc(content_length + 1);
    int read = esp_http_client_read_response(client, result, content_length);
    if (read != content_length) {
        ESP_LOGE(TAG, "HTTP_ERROR: read=%d, length=%d", read, content_length);
        free(result);
        result = NULL;
    } else {
        result[content_length] = 0;
        ESP_LOGW(TAG, "result: %s, size: %d", result, strlen(result));
    }
    ESP_LOGW(TAG, "return - %s", result);
    return result;
}

// 定义函数，用于通过GET请求获取音频数据
esp_err_t Utils_GetAudio(const char *voiceId, uint8_t **audioData, size_t *audioLen) {
    esp_err_t err;
    esp_http_client_config_t config = {
        .url = "http://110.42.98.230:8456/voice/",
        .timeout_ms = 60000, // 增加超时时间
    };

    // 构建完整的URL
    char fullUrl[256];
    snprintf(fullUrl, sizeof(fullUrl), "%s%s", config.url, voiceId);
    config.url = fullUrl;

    esp_http_client_handle_t client = esp_http_client_init(&config);
    if (client == NULL) {
        ESP_LOGE(TAG, "Failed to initialize HTTP client");
        return ESP_FAIL;
    }

    // 设置HTTP方法为GET
    esp_http_client_set_method(client, HTTP_METHOD_GET);

    // 打开HTTP连接
    err = esp_http_client_open(client, 0);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
        goto cleanup;
    }

    // 获取响应头
    int content_length = esp_http_client_fetch_headers(client);
    if (esp_http_client_is_chunked_response(client)) {
        ESP_LOGW(TAG, "http_client_is_chunked - YES");
        esp_http_client_get_chunk_length(client, &content_length);
    }
    ESP_LOGW(TAG, "content_length=%d", content_length);

    // 分配内存以存储音频数据
    *audioData = (uint8_t *)malloc(content_length);
    if (*audioData == NULL) {
        ESP_LOGE(TAG, "Failed to allocate memory for audio data");
        err = ESP_ERR_NO_MEM;
        goto cleanup;
    }

    // 读取音频数据
    int read_len = esp_http_client_read(client, (char *)*audioData, content_length);
    if (read_len != content_length) {
        ESP_LOGE(TAG, "Failed to read audio data: read_len=%d, content_length=%d", read_len, content_length);
        free(*audioData);
        *audioData = NULL;
        err = ESP_FAIL;
        goto cleanup;
    }

    // 设置音频数据长度
    *audioLen = read_len;

cleanup:
    esp_http_client_cleanup(client);
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "Successfully fetched audio data, length: %d", *audioLen);
    }
    return err;
}