#include "xysdk_common.h"

// 初始化链表
xy_list_t* xy_init_list() 
{
    xy_list_t* list = (xy_list_t*)xy_common_malloc(sizeof(xy_list_t));
    if (list == NULL) 
    {
        return NULL;
    }
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
    return list;
}

xy_node_t* xy_create_node(void* data) 
{
    xy_node_t* new_node = (xy_node_t*)xy_common_malloc(sizeof(xy_node_t));
    if (new_node == NULL) 
    {
        return NULL;
    }
    
    new_node->data = data;
    new_node->next = NULL;
    new_node->prev = NULL;
    return new_node;
}

// 向链表末尾插入新节点
void xy_insert_at_end(xy_list_t* list, void* data) 
{
    if (list == NULL) 
    {
        return;
    }
    xy_node_t* new_node = xy_create_node(data);
    if (new_node == NULL) 
    {
        return;
    }
    if (list->size == 0) {
        list->head = new_node;
        list->tail = new_node;
    } else {
        new_node->prev = list->tail;
        list->tail->next = new_node;
        list->tail = new_node;
    }
    list->size++;
}

// 从链表中删除节点
void xy_delete_node(xy_list_t* list, xy_node_t* node, void (*free_func)(void*)) 
{
    if (list==NULL || node==NULL) 
    {
        return;
    }

    if (node->prev) {
        node->prev->next = node->next;
    } else {
        list->head = node->next;
    }
    if (node->next) {
        node->next->prev = node->prev;
    } else {
        list->tail = node->prev;
    }
    free_func(node);
    list->size--;
}

xy_node_t* xy_find_list_by_data(xy_list_t *list, void *data, int (*cmp_func)(void*, void*)) 
{
    int ret = 0;
    if (list == NULL) 
    {
        return NULL;
    }
    xy_node_t *current = list->head;
    while (current != NULL) 
    {
        ret = cmp_func(current->data, data);
        if (ret == 0) 
        { // 注意：这里假设data指针可以直接比较
            return current;
        }
        current = current->next;
    }
    return NULL; // 未找到匹配的节点
}

xy_node_t* xy_find_list_by_index(xy_list_t *list, int index) 
{
    if (list == NULL || index < 0 || index >= list->size) {
        return NULL;
    }
    
    xy_node_t *current = list->head;
    for (int i = 0; current != NULL && i < index; i++) {
        current = current->next;
    }
    
    return current; // 如果index有效，则返回对应的节点；否则返回NULL
}

// 遍历链表并打印数据
void xy_print_list(xy_list_t* list, void (*print_func)(void*)) 
{
    if (list == NULL) 
    {
        return;
    }
    
    xy_node_t* current = list->head;
    while (current!=NULL) 
    {
        print_func(current->data);
        current = current->next;
    }
}

// 释放链表内存
void xy_free_list(xy_list_t* list, void (*free_func)(void*)) 
{
    if (list == NULL) 
    {
        return;
    }
    xy_node_t* current = list->head;
    xy_node_t* temp;
    while (current != NULL) 
    {
        temp = current;
        current = current->next;
        free_func(temp);
    }
    xy_common_free((void **)&list);
}

size_t xy_get_list_size(xy_list_t* list) 
{
    if (list == NULL) 
    {
        return 0;
    }
    return list->size;
}
    

static xy_node_t* xy_list_change_node(xy_node_t* node, uint16_t step, void* (*dir_func)(void *)) 
{    
    while (NULL != node)
    {
        node =(xy_node_t*)dir_func(node);
        step--; 
        if (step == 0)
        {
            break;
        }
    }
    return node;    
}

static xy_node_t* dir_next(xy_node_t *node) 
{
    return node->next;
}

static xy_node_t* dir_prev(xy_node_t *node) 
{
    return node->prev;
}

xy_node_t* xy_list_skip_by_node(xy_node_t* node, uint8_t dir, uint16_t step) 
{
    if(dir == 1)
    {
        return xy_list_change_node(node, step, (void *)dir_prev);   
    }
    else if(dir == 2)
    {
        return xy_list_change_node(node, step, (void *)dir_next);
    }
    return NULL;
}