#include "xysdk_common.h"
#include "xysdk_play_list.h"

// 示例打印函数，用于打印MyStruct类型的数据
#if 0
static void xy_print_struct(void* data) 
{
    #if 1
    if (data == NULL) {
        //XY_LOG_E("Error: data is null\n");
        return;
    }
    source_list_info_t* struct_data = (source_list_info_t*)data;
    XY_LOG_I("track_id: %s, pay_type: %d, duration: %d\n", struct_data->track_id, struct_data->pay_type, struct_data->play_duration);
    //XY_LOG_I("track_name: %s\n", struct_data->title);
    #endif
}
#endif

static void xy_free_struct(void* data) 
{
    if (data == NULL) {
        //XY_LOG_E("Error: data is null\n");
        return;
    }
    xy_node_t* struct_data = (xy_node_t*)data;
    if(struct_data != NULL)
    {
        if(struct_data->data != NULL)
        {
            xy_common_free((void **)&struct_data->data);
            struct_data->data = NULL;
        }
        xy_common_free((void **)&struct_data);
        struct_data = NULL;
    }
}

static int xy_cmp_struct_data(void* data, void *src_data) 
{
    if (data == NULL || src_data == NULL) 
    {
        //XY_LOG_E("Error: data is null\n");
        return -1;
    }

    source_list_info_t* struct_data = (source_list_info_t*)data;
    char* src_struct_data = (char*)src_data;
    if(struct_data != NULL)
    {
        //XY_LOG_I("struct_data:%s, src_struct_data: %s\n",struct_data->track_id, src_struct_data);
        if(strcmp(struct_data->track_id, src_struct_data) == 0)
        {
            //XY_LOG_I("find node success\n");
            return 0;
        }
    }
    return -1;
}

xy_list_t* xy_api_init_track_list(void)
{
    return xy_init_list();
}

int xy_api_add_info_to_list(xy_list_t* track_list, source_list_info_t *track_info) 
{
    if (track_info == NULL) 
    {
        return -1;
    }

    if (track_list == NULL) 
    {
        return -1; // 链表创建失败，无法添加数据
    }

    XY_LOG_I("track_info->track_id:%s\n",track_info->track_id);
    if(xy_find_list_by_data(track_list, (void*)track_info->track_id, xy_cmp_struct_data)==NULL)
    {
        xy_insert_at_end(track_list, (void*)track_info);
        // 遍历并打印链表中的数据
        //xy_print_list(track_list, xy_print_struct);
    }    
    else 
    {
         return 1;
    }
    
    return 0;
}

void xy_api_free_track_list(xy_list_t* track_list)
{
    if(track_list != NULL)
    {
        xy_free_list(track_list, xy_free_struct);
        track_list = NULL;
    }
}


uint16_t xy_api_get_track_total(xy_list_t* track_list)
{
    if(track_list == NULL)
    {
        return 0;
    }
    return (uint16_t)xy_get_list_size(track_list);
}

xy_node_t* xy_api_get_track_node(xy_list_t* track_list, char *track_id)
{
    if (track_id == NULL) 
    {
        return NULL;
    }
    
    if (track_list == NULL) 
    {
        return NULL; // 链表获取失败
    }
    //XY_LOG_I("xy_api_get_track_node--track_list: 0x%x\n", track_list);
    xy_node_t* node = xy_find_list_by_data(track_list, (void*)track_id, xy_cmp_struct_data);
    return node;
}


xy_node_t* xy_api_change_track_by_node(xy_list_t* track_list, xy_node_t* node, uint8_t dir, uint16_t step)
{
    if (node == NULL) 
    {
        return NULL;
    }

    if (track_list == NULL) 
    {
        return NULL; // 链表获取失败
    }
    
    return xy_list_skip_by_node(node, dir, step);
}

source_list_info_t *xy_api_get_track_info(xy_list_t* track_list, uint16_t index)
{
    XY_LOG_I("%s, index: %d\n", __func__, index);
    if(!track_list)
    {
        return NULL;
    }

    if(index >= xy_api_get_track_total(track_list))
    {
        return NULL;
    }

    xy_node_t *cur_node = xy_find_list_by_index(track_list, index);
    if(cur_node != NULL)
    {   
        //XY_LOG_I("index: %d, data: 0x%x\n", index, cur_node->data);
        return (source_list_info_t*)cur_node->data;
    }
    else
    {
        return NULL;
    }
}

source_list_info_t *xy_api_get_track_info_by_track_id(xy_list_t* track_list, char *track_id)
{
    //XY_LOG_I("%s, track_id: %s\n", __func__, track_id);
    if(track_id == NULL || !track_list)
    {
        return NULL;
    }

    xy_node_t *cur_node = xy_find_list_by_data(track_list, (void*)track_id, xy_cmp_struct_data);;
    if(cur_node != NULL)
    {   
        //XY_LOG_I("index: %d, data: 0x%x\n", index, cur_node->data);
        return (source_list_info_t*)cur_node->data;
    }
    else
    {
        return NULL;
    }
}