#include "ui_general_list_page.h"
#include "ui_cont.h"  // 你的容器创建相关头文件
#include "ui_slider.h"// 你的滑块相关头文件

/* ============================ 私有常量（通用样式，适配你的界面） ============================= */
// 坐标/尺寸（贴合你之前的UI风格）
#define PATH_LABEL_X        10
#define PATH_LABEL_Y        10
#define LEFT_MENU_X         12
#define LEFT_MENU_BASE_Y    50
#define LEFT_MENU_SPACING   32
#define RIGHT_NAME_X        135
#define RIGHT_VALUE_X       325  // 可根据页面调整，内部兼容
#define RIGHT_UNIT_X        430
#define RIGHT_BASE_Y        50
#define RIGHT_SPACING       32


/* ============================ 私有函数（内部渲染逻辑） ============================= */
// 绘制背景
static void draw_background(lv_obj_t *cont, lv_color_t bg_color) {
    lv_obj_set_style_local_bg_color(cont, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, bg_color);
    lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES);
}

// 创建顶部路径
static void create_path_label(GeneralListPage_t *page) {

    Draw_pagepath(page->path_label ,page->config.path_text );
}

// 创建左侧菜单
static void create_left_menu(GeneralListPage_t *page) {
    uint8_t i = 0;
    for (i = 0; i < page->config.left_menu_count && i < 8; i++) {
        page->left_menu_labels[i] = lv_label_create(page->cont, NULL);
        // 设置字体/颜色/文本
        lv_obj_set_style_local_text_font(page->left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(page->left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, page->config.left_menu[i].color); // 设置颜色
        lv_label_set_text(page->left_menu_labels[i], page->config.left_menu[i].text);
        // 设置位置
        lv_obj_set_pos(page->left_menu_labels[i], LEFT_MENU_X, LEFT_MENU_BASE_Y + i * LEFT_MENU_SPACING);

    }
}

// 创建右侧数据项
static void create_right_data_items(GeneralListPage_t *page) {
    for (uint8_t i = 0; i < page->config.right_data_count && i < 8; i++) {
        // 名称标签
        page->right_name_labels[i] = lv_label_create(page->cont, NULL);
        lv_obj_set_style_local_text_font(page->right_name_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(page->right_name_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 
                                         page->config.right_data[i].name_color);
        lv_label_set_text(page->right_name_labels[i], page->config.right_data[i].name);
        lv_obj_set_pos(page->right_name_labels[i], RIGHT_NAME_X, RIGHT_BASE_Y + i * RIGHT_SPACING);

        // 值标签
        page->right_value_labels[i] = lv_label_create(page->cont, NULL);
        lv_obj_set_style_local_text_font(page->right_value_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(page->right_value_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 
                                         page->config.right_data[i].value_color);
        lv_label_set_text(page->right_value_labels[i], page->config.right_data[i].value);
        //lv_obj_set_pos(page->right_value_labels[i], RIGHT_VALUE_X, RIGHT_BASE_Y + i * RIGHT_SPACING);
        lv_obj_set_pos(page->right_value_labels[i]
                    , page->config.right_data->value_posx? page->config.right_data->value_posx :RIGHT_VALUE_X  //如果没有设置则使用默认值
                    , RIGHT_BASE_Y + i * RIGHT_SPACING);
        // 单位标签
        page->right_unit_labels[i] = lv_label_create(page->cont, NULL);
        lv_obj_set_style_local_text_font(page->right_unit_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(page->right_unit_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 
                                         page->config.right_data[i].unit_color);
        lv_label_set_text(page->right_unit_labels[i], page->config.right_data[i].unit);
        //lv_obj_set_pos(page->right_unit_labels[i], RIGHT_UNIT_X, RIGHT_BASE_Y + i * RIGHT_SPACING);
        lv_obj_set_pos(page->right_unit_labels[i], page->config.right_data[i].unit_posx? page->config.right_data[i].unit_posx:RIGHT_UNIT_X
                        , RIGHT_BASE_Y + i * RIGHT_SPACING);

    }
}

// 创建分页滑块
static void create_slider(GeneralListPage_t *page) {
    //if (page->config.total_page == 0) return; // 无分页则不创建
    page->slider = ui_slider_create(0, page->config.slider_size); // 你的滑块创建函数

    //lv_slider_set_value(page->slider, page->config.slider_value, LV_ANIM_ON);   // 设置当前页
    lv_slider_set_range(page->slider , 0 , page->config.slider_size);
    //因为进度条是垂直，size在最上端，0值在最底下，所以数值需要反过来才会有从上到下的效果，否则进度条动画是从下到上
    lv_slider_set_value(page->slider, (page->config.slider_size - page->config.slider_value) , LV_ANIM_ON);

}

/* ============================ 公有方法实现 ============================= */
bool general_list_page_create(GeneralListPage_t *page, const GeneralListPageConfig_t *config) {
    if (page == NULL || config == NULL) return false;

    // 1. 初始化配置
    memset(page, 0, sizeof(GeneralListPage_t));
    page->config = *config;

    // 2. 创建根容器
    page->cont = ui_get_cont(); // 替换为你的容器创建逻辑
    if (page->cont == NULL) return false;

    // 3. 绘制背景
    //draw_background(page->cont, config->bg_color);
    Draw_counteara();

    // 4. 依次创建UI元素
    create_path_label(page);
    create_left_menu(page);
    create_right_data_items(page);
    create_slider(page);

    return true;
}

void general_list_page_refresh(GeneralListPage_t *page, const GeneralListPageConfig_t *config) {
    if (page == NULL || config == NULL) return;

    // 1. 更新配置
    page->config = *config;

    // 2. 隐藏容器减少重绘
    bool is_hidden = lv_obj_get_hidden(page->cont);
    if (!is_hidden) lv_obj_set_hidden(page->cont, true);

    // 3. 刷新路径 不需要刷新
    // if (page->path_label != NULL)
    //     lv_label_set_text(page->path_label, config->path_text);
    // else
    //     printf("%s path label is null\n", __FUNCTION__);
    
    

    // 4. 刷新左侧菜单
    for (uint8_t i = 0; i < config->left_menu_count && i < 8; i++) {
        lv_label_set_text(page->left_menu_labels[i], config->left_menu[i].text);
        lv_obj_set_style_local_text_color(page->left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, config->left_menu[i].color);
        lv_obj_set_pos(page->left_menu_labels[i], LEFT_MENU_X, LEFT_MENU_BASE_Y + i * LEFT_MENU_SPACING);
    }

    // 5. 刷新右侧数据项
    for (uint8_t i = 0; i < config->right_data_count && i < 8; i++) {

        lv_label_set_text(page->right_name_labels[i], config->right_data[i].name);
        lv_label_set_text(page->right_value_labels[i], config->right_data[i].value);
        lv_label_set_text(page->right_unit_labels[i], config->right_data[i].unit);
        
        lv_obj_set_style_local_text_color(page->right_name_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 
                                         config->right_data[i].name_color);
        lv_obj_set_style_local_text_color(page->right_value_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 
                                         config->right_data[i].value_color);
        lv_obj_set_style_local_text_color(page->right_unit_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT,
                                          config->right_data[i].unit_color);
        
        lv_obj_set_pos(page->right_name_labels[i], RIGHT_NAME_X, RIGHT_BASE_Y + i * RIGHT_SPACING);
        lv_obj_set_pos(page->right_value_labels[i]
                    , page->config.right_data->value_posx? page->config.right_data->value_posx :RIGHT_VALUE_X  //如果没有设置则使用默认值
                    , RIGHT_BASE_Y + i * RIGHT_SPACING);
        lv_obj_set_pos(page->right_unit_labels[i], page->config.right_data[i].unit_posx? page->config.right_data[i].unit_posx:RIGHT_UNIT_X
                        , RIGHT_BASE_Y + i * RIGHT_SPACING);
        
    }

    // 6. 刷新滑块 不需要刷新
    // if (config->total_page > 0 && page->slider != NULL) {
    //     ui_slider_set_range_and_value(page->slider, config->total_page - 1, config->current_page);
    // }

    // 7. 恢复容器显示
    lv_obj_set_hidden(page->cont, is_hidden);
}

void general_list_page_destroy(GeneralListPage_t *page) {
    if (page == NULL) return;

    // // 销毁所有控件
    // lv_obj_del(page->path_label);
    // for (uint8_t i = 0; i < 8; i++) {
    //     lv_obj_del(page->left_menu_labels[i]);
    //     lv_obj_del(page->right_name_labels[i]);
    //     lv_obj_del(page->right_value_labels[i]);
    //     lv_obj_del(page->right_unit_labels[i]);
    // }
    // lv_obj_del(page->slider);
    // lv_obj_del(page->cont);
    lv_obj_clean(page->cont);
    // 清空结构体
    memset(page, 0, sizeof(GeneralListPage_t));
}