/*********************************************************************************************
    *   Filename        : xysdk_main.c

    *   Description     : Optimize code structure to avoid stack overflow

    *   Author          : ZhangWeiZe

    *   Last modifiled  : 2025-03-19
*********************************************************************************************/
#include "xysdk_common.h"
#include "xysdk_http_request.h"

char test_qrcode_id[XY_MAX_TOKEN_LEN] = {0};
// ------------------------- 获取首页信息函数 -------------------------
int xy_http_get_homepage_info()
{
    xy_get_homepage_info_resp_model *homepage_resp_data = malloc(sizeof(xy_get_homepage_info_resp_model));
    if (homepage_resp_data == NULL) {
        return -1; 
    }
    memset(homepage_resp_data, 0, sizeof(xy_get_homepage_info_resp_model)); 
    int result = xy_api_http_get_homepage_info(homepage_resp_data);
    free(homepage_resp_data);
    return result;
}

// ------------------------- 获取弹窗信息函数 -------------------------
int xy_http_get_popup_screen_info()
{
    popup_screen_info_resp_model *popup_resp_data = malloc(sizeof(popup_screen_info_resp_model));
    if (popup_resp_data == NULL) {
        return -1;
    }
    memset(popup_resp_data, 0, sizeof(popup_screen_info_resp_model));
    int ret = xy_api_http_get_popup_screen_info(0, popup_resp_data);
    free(popup_resp_data); 
    return ret;
}

// ------------------------- 登录二维码函数 -------------------------
int xy_http_get_login_qrcode(void) {
    int ret = -1;
    xy_get_qrcode_req_model *qrcode_req_model = malloc(sizeof(xy_get_qrcode_req_model));
    if (!qrcode_req_model) return -1;
    memset(qrcode_req_model, 0, sizeof(xy_get_qrcode_req_model));
	strncpy(qrcode_req_model->action,"login",XY_MAX_STR_LEN_16-1);
    qrcode_req_model->action[XY_MAX_STR_LEN_16 - 1] = '\0';
    strncpy(qrcode_req_model->device_info.device_name, "test_01", XY_MAX_STR_LEN_64 - 1);
    qrcode_req_model->device_info.device_name[XY_MAX_STR_LEN_64 - 1] = '\0';
    strncpy(qrcode_req_model->device_info.device_model, "watch", XY_MAX_STR_LEN_32 - 1);
    qrcode_req_model->device_info.device_model[XY_MAX_STR_LEN_32 - 1] = '\0';
    xy_get_qrcode_resp_model *qrcode_resp_data = malloc(sizeof(xy_get_qrcode_resp_model));
    if (!qrcode_resp_data) {
        free(qrcode_req_model);
        return -1;
    }
    memset(qrcode_resp_data, 0, sizeof(xy_get_qrcode_resp_model));
    ret = xy_api_http_get_login_pay_qrcode(qrcode_req_model, qrcode_resp_data);
    snprintf(test_qrcode_id, sizeof(test_qrcode_id), "%s", qrcode_resp_data->qrcode_id);
    free(qrcode_req_model);
    free(qrcode_resp_data);
    return ret;
}

// ------------------------- 用户历史记录函数 -------------------------
int xy_http_get_user_history_list() {
    xy_get_history_req_model *history_req_data = malloc(sizeof(xy_get_history_req_model));
    if (!history_req_data) return -1;
    memset(history_req_data, 0, sizeof(xy_get_history_req_model));
    history_req_data->page = 1;
    history_req_data->limit = XY_ALBUM_NUM_PER_PAGE;
    history_req_data->contains_paid = true;

    xy_get_source_list_resp_model *history_resp_data = malloc(sizeof(xy_get_source_list_resp_model));
    if (!history_resp_data) {
        free(history_req_data);
        return -1;
    }
    memset(history_resp_data, 0, sizeof(xy_get_source_list_resp_model));

    int ret = xy_api_http_get_user_history_list(history_req_data, history_resp_data);
    free(history_req_data);
    free(history_resp_data);
    return ret;
}

// ------------------------- 用户订阅列表函数 -------------------------
int xy_http_get_user_subscribe_list() {
    xy_get_subscribe_req_model *subscribe_req_data = malloc(sizeof(xy_get_subscribe_req_model));
    if (!subscribe_req_data) return -1;
    memset(subscribe_req_data, 0, sizeof(xy_get_subscribe_req_model));
	strncpy(subscribe_req_data->timeline,"0",XY_MAX_STR_LEN_16-1);
    subscribe_req_data->page = 1;
    subscribe_req_data->limit = XY_ALBUM_NUM_PER_PAGE;
    subscribe_req_data->contains_paid = true;
    xy_get_source_list_resp_model *subscribe_resp_data = malloc(sizeof(xy_get_source_list_resp_model));
    if (!subscribe_resp_data) {
        free(subscribe_req_data);
        return -1;
    }
    memset(subscribe_resp_data, 0, sizeof(xy_get_source_list_resp_model));
    int ret = xy_api_http_get_user_subscribe_list(subscribe_req_data, subscribe_resp_data);
    free(subscribe_req_data);
    free(subscribe_resp_data);
    return ret;
}

// ------------------------- 收听ID获取音轨列表函数 -------------------------
int xy_http_get_track_list_by_listen_id(u8 *listen_id) {
    xy_get_listen_list_req_model *tracklist_req_data = malloc(sizeof(xy_get_listen_list_req_model));
    if (!tracklist_req_data) return -1;
    memset(tracklist_req_data, 0, sizeof(xy_get_listen_list_req_model));
    tracklist_req_data->page = 1;
    tracklist_req_data->limit = XY_ALBUM_NUM_PER_PAGE;
    size_t id_len = strlen(listen_id);
    size_t max_copy = (id_len < XY_MAX_ID_LEN) ? id_len : XY_MAX_ID_LEN - 1;
    strncpy(tracklist_req_data->id, listen_id, max_copy);
    tracklist_req_data->id[max_copy] = '\0';
    xy_get_source_list_resp_model *tracklist_resp_data = malloc(sizeof(xy_get_source_list_resp_model));
    if (!tracklist_resp_data) {
        free(tracklist_req_data);
        return -1;
    }
    memset(tracklist_resp_data, 0, sizeof(xy_get_source_list_resp_model));
    int ret = xy_api_http_get_listen_info(tracklist_req_data, tracklist_resp_data);
    free(tracklist_req_data);
    free(tracklist_resp_data);
    return ret;
}


// ------------------------- 专辑ID获取音轨列表函数 -------------------------
int xy_http_get_track_list_by_album_id(u8 *id) {
    xy_get_track_list_req_model *tracklist_req_data = malloc(sizeof(xy_get_track_list_req_model));
    if (!tracklist_req_data) return -1;
    memset(tracklist_req_data, 0, sizeof(xy_get_track_list_req_model));
    tracklist_req_data->page = 1;
    tracklist_req_data->limit = XY_ALBUM_NUM_PER_PAGE;
    tracklist_req_data->source_type = 1;
	strncpy(tracklist_req_data->sort ,"asc",XY_MAX_STR_LEN_8-1);
	tracklist_req_data->sort[XY_MAX_STR_LEN_8-1]='\0';
    strncpy(tracklist_req_data->album_id, id, XY_MAX_ID_LEN - 1);
    tracklist_req_data->album_id[XY_MAX_ID_LEN - 1] = '\0';
    printf(">>>>>>>>>p_albumid %s \n", tracklist_req_data->album_id);
    xy_get_source_list_resp_model *tracklist_resp_data = malloc(sizeof(xy_get_source_list_resp_model));
    if (!tracklist_resp_data) {
        free(tracklist_req_data);
        return -1;
    }
    memset(tracklist_resp_data, 0, sizeof(xy_get_source_list_resp_model));
    int ret = xy_api_http_get_track_list_by_albumId(tracklist_req_data, tracklist_resp_data);
    free(tracklist_req_data);
    free(tracklist_resp_data);
    return ret;
}


// ------------------------- 收听ID获取播放链接函数 -------------------------
int xy_http_get_play_url_by_track_id(u8* p_trackid, u8 pay_type, u8* album_id, u8 album_type)
{
    printf(">>>>>>trackid %s ,pay_type %d album_id %s album_type %d \n", p_trackid, pay_type, album_id, album_type);
    xy_get_url_req_model* url_req_data = malloc(sizeof(xy_get_url_req_model));
    if (!url_req_data) {
        printf("Memory allocation failed for request model\n");
        return -1;
    }
    memset(url_req_data, 0, sizeof(xy_get_url_req_model));
    url_req_data->source_type = 1;
    url_req_data->pay_type = pay_type;
    if (strlen(p_trackid) >= XY_MAX_ID_LEN) {
        printf("Track ID exceeds maximum allowed length\n");
        free(url_req_data);
        return -1;
    }
    strncpy(url_req_data->track_id, p_trackid, XY_MAX_ID_LEN - 1);
    url_req_data->track_id[XY_MAX_ID_LEN - 1] = '\0'; // 确保字符串终止
    xy_get_url_resp_model* url_resp_data = malloc(sizeof(xy_get_url_resp_model));
    if (!url_resp_data) {
        printf("Memory allocation failed for response model\n");
        free(url_req_data);
        return -1;
    }
    memset(url_resp_data, 0, sizeof(xy_get_url_resp_model));
    int ret = xy_api_http_get_play_url(url_req_data, url_resp_data);
    if (ret == XY_HTTP_PLEASE_PAY) {
		int xy_http_get_pay_qrcode(u8* album_id, u8 album_type);
        xy_http_get_pay_qrcode(album_id, album_type);
    }
    free(url_req_data);
    free(url_resp_data);
    return ret;
}

// ------------------------- 获取支付二维码函数 -------------------------
static int xy_http_get_pay_qrcode(u8* album_id, u8 album_type)
{
    xy_get_qrcode_req_model *qrcode_req_model = malloc(sizeof(xy_get_qrcode_req_model));
    if (!qrcode_req_model) return -1;
    memset(qrcode_req_model, 0, sizeof(xy_get_qrcode_req_model));

    strncpy(qrcode_req_model->device_info.device_name, "test_01", XY_MAX_STR_LEN_64 - 1);
    qrcode_req_model->device_info.device_name[XY_MAX_STR_LEN_64 - 1] = '\0';
    strncpy(qrcode_req_model->device_info.device_model, "watch", XY_MAX_STR_LEN_64 - 1);
    qrcode_req_model->device_info.device_model[XY_MAX_STR_LEN_64 - 1] = '\0';

    if (album_type == 1) {
        strncpy(qrcode_req_model->action, "personal_center", XY_MAX_STR_LEN_16 - 1);
        qrcode_req_model->action[XY_MAX_STR_LEN_16 - 1] = '\0';
    } else if (album_type == 2) {
        strncpy(qrcode_req_model->action, "buy", XY_MAX_STR_LEN_16 - 1);
        qrcode_req_model->action[XY_MAX_STR_LEN_16 - 1] = '\0';
        qrcode_req_model->pay_info.album_type = 1;
        qrcode_req_model->pay_info.distribute_type = 2;
        strncpy(qrcode_req_model->pay_info.pay_content, album_id, XY_MAX_ID_LEN - 1);
        qrcode_req_model->pay_info.pay_content[XY_MAX_ID_LEN - 1] = '\0';
        strncpy(qrcode_req_model->pay_info.album_id, album_id, XY_MAX_ID_LEN - 1);
        qrcode_req_model->pay_info.album_id[XY_MAX_ID_LEN - 1] = '\0';
    }
    xy_get_qrcode_resp_model *qrcode_resp_data = malloc(sizeof(xy_get_qrcode_resp_model));
    if (!qrcode_resp_data) {
        free(qrcode_req_model);
        return -1;
    }
    memset(qrcode_resp_data, 0, sizeof(xy_get_qrcode_resp_model));

    int ret = xy_api_http_get_login_pay_qrcode(qrcode_req_model, qrcode_resp_data);
    free(qrcode_req_model);
    free(qrcode_resp_data);
    return ret;
}

// ------------------------- 上传历史记录函数 -------------------------
int xy_http_upload_play_history(char* start_time, char* end_time, char* track_id, char* album_id, int duration, int break_time)
{
    if (!start_time || !end_time || !track_id || !album_id) return -1;
    xy_play_info_upload_model *play_info_upload_data = malloc(sizeof(xy_play_info_upload_model));
    if (!play_info_upload_data) return -1;
    memset(play_info_upload_data, 0, sizeof(xy_play_info_upload_model));
    play_info_upload_data->count = 1;
    strncpy(play_info_upload_data->play_array[0].started_at, start_time, XY_MAX_STR_LEN_16 - 1);
    play_info_upload_data->play_array[0].started_at[XY_MAX_STR_LEN_16 - 1] = '\0';
    strncpy(play_info_upload_data->play_array[0].end_at, end_time, XY_MAX_STR_LEN_16 - 1);
    play_info_upload_data->play_array[0].end_at[XY_MAX_STR_LEN_16 - 1] = '\0';
    strncpy(play_info_upload_data->play_array[0].track_id, track_id, XY_MAX_ID_LEN - 1);
    play_info_upload_data->play_array[0].track_id[XY_MAX_ID_LEN - 1] = '\0';
    strncpy(play_info_upload_data->play_array[0].album_id, album_id, XY_MAX_ID_LEN - 1);
    play_info_upload_data->play_array[0].album_id[XY_MAX_ID_LEN - 1] = '\0';
    play_info_upload_data->play_array[0].played_duration = duration;
    play_info_upload_data->play_array[0].break_secs = break_time;
    int ret = xy_api_http_upload_play_history(play_info_upload_data);
    free(play_info_upload_data);
    return ret;
}


// ------------------------- 获取二维码状态函数 -------------------------
int xy_http_get_qrcode_status(void)
{
    xy_qrcode_status_resp_model *qrcode_status_resp_data = malloc(sizeof(xy_qrcode_status_resp_model));
    if (!qrcode_status_resp_data) {
        return -1;
    }
    memset(qrcode_status_resp_data, 0, sizeof(xy_qrcode_status_resp_model));
    int ret = xy_api_http_get_login_pay_status(test_qrcode_id, qrcode_status_resp_data);
    free(qrcode_status_resp_data);
    return ret;
}
