#include "ui_history_report_page.h"
#include "ui_common/ui_cont.h"
#include "ui_common/ui_slider.h"
#include "data/menu/Manu_str.h"
#include <string.h>
#include <stdio.h>

/* ============================ 私有常量定义 ============================= */

// 坐标定义
#define HR_PATH_LABEL_X                 10
#define HR_PATH_LABEL_Y                 10
#define HR_LEFT_MENU_X                  12
#define HR_LEFT_MENU_BASE_Y             50
#define HR_LEFT_MENU_SPACING            32
#define HR_RIGHT_CONTENT_X              135
#define HR_RIGHT_CONTENT_Y              50
#define HR_RIGHT_LINE_SPACING           28
#define HR_RECORD_INDEX_X               380
#define HR_RECORD_INDEX_Y               50

// 颜色定义
#define HR_COLOR_DEFAULT                LV_COLOR_BLACK
#define HR_COLOR_SELECTED               LV_COLOR_RED
#define HR_COLOR_NO_RECORD              LV_COLOR_GRAY

// 页码标签偏移（相对于第0行）
#define HR_PAGE_INFO_LINE_OFFSET        0

/* ============================ 私有变量 ============================= */

static lv_obj_t *s_cont = NULL;
static lv_obj_t *s_path_label = NULL;
static lv_obj_t *s_left_menu_labels[HISTORY_REPORT_MENU_NUM] = {NULL};
static lv_obj_t *s_right_content_labels[HR_MAX_DISPLAY_LINES] = {NULL};
static lv_obj_t *s_page_info_label = NULL;
static lv_obj_t *s_slider = NULL;

/* ============================ 私有函数声明 ============================= */

static void create_path_label(const char *path_text);
static void create_left_menu(uint8_t sel_idx);
static void create_right_content(HistoryReportPageConfig *config);
static void create_page_info(const char *page_info);
static void create_slider(uint32_t total_pages, uint32_t current_page);

/* ============================ 接口函数实现 ============================= */

/**
 * @brief 创建历史报告页面
 */
bool history_report_page_create(HistoryReportPageConfig *config)
{
    if (config == NULL) {
        return false;
    }

    // 获取根容器
    s_cont = ui_get_cont();
    if (s_cont == NULL) {
        return false;
    }

    // 绘制背景
    Draw_counteara();

    // 创建路径标签
    create_path_label(config->path_text);

    // 创建左侧菜单
    create_left_menu(config->left_menu_sel_idx);

    // 创建右侧内容
    create_right_content(config);

    // 创建页码信息（多记录模式显示页码，单记录模式显示记录索引）
    if (config->has_records) {
        char page_info[32];
        if (config->records_per_page > 1) {
            // 多记录模式：显示页码
            snprintf(page_info, sizeof(page_info), "%lu/%lu",
                     (unsigned long)config->current_page,
                     (unsigned long)config->total_pages);
        } else {
            // 单记录模式：显示记录索引
            snprintf(page_info, sizeof(page_info), "%lu/%lu",
                     (unsigned long)config->current_page,
                     (unsigned long)config->total_records);
        }
        create_page_info(page_info);
    }

    // 创建滑块
    if (config->total_pages > 0) {
        create_slider(config->total_pages, config->current_page);
    }

    return true;
}

/**
 * @brief 刷新历史报告页面
 */
void history_report_page_refresh(HistoryReportPageConfig *config)
{
    uint8_t i;

    if (config == NULL || s_cont == NULL) {
        return;
    }

    // 隐藏容器减少重绘
    bool is_hidden = lv_obj_get_hidden(s_cont);
    if (!is_hidden) {
        lv_obj_set_hidden(s_cont, true);
    }

    // 刷新路径标签
    if (s_path_label != NULL) {
        lv_label_set_text(s_path_label, config->path_text);
    }

    // 刷新左侧菜单
    for (i = 0; i < HISTORY_REPORT_MENU_NUM; i++) {
        if (s_left_menu_labels[i] != NULL) {
            if (i == config->left_menu_sel_idx) {
                lv_obj_set_style_local_text_color(s_left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, HR_COLOR_SELECTED);
            } else {
                lv_obj_set_style_local_text_color(s_left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, HR_COLOR_DEFAULT);
            }
        }
    }

    // 刷新右侧内容
    lv_color_t text_color = config->has_records ? HR_COLOR_DEFAULT : HR_COLOR_NO_RECORD;
    for (i = 0; i < HR_MAX_DISPLAY_LINES; i++) {
        if (s_right_content_labels[i] != NULL) {
            lv_obj_set_style_local_text_color(s_right_content_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, text_color);
            if (i < config->line_count && config->record_content[i][0] != '\0') {
                lv_label_set_text(s_right_content_labels[i], config->record_content[i]);
                lv_obj_set_hidden(s_right_content_labels[i], false);
            } else {
                lv_obj_set_hidden(s_right_content_labels[i], true);
            }
        }
    }

    // 刷新页码信息
    if (s_page_info_label != NULL) {
        if (config->has_records) {
            char page_info[32];
            if (config->records_per_page > 1) {
                snprintf(page_info, sizeof(page_info), "%lu/%lu",
                         (unsigned long)config->current_page,
                         (unsigned long)config->total_pages);
            } else {
                snprintf(page_info, sizeof(page_info), "%lu/%lu",
                         (unsigned long)config->current_page,
                         (unsigned long)config->total_records);
            }
            lv_label_set_text(s_page_info_label, page_info);
            lv_obj_set_hidden(s_page_info_label, false);
        } else {
            lv_obj_set_hidden(s_page_info_label, true);
        }
    }

    // 刷新滑块
    if (s_slider != NULL && config->total_pages > 0) {
        uint32_t zero_based_idx = (config->current_page > 0) ? (config->current_page - 1) : 0;
        uint32_t slider_value = (config->total_pages - 1) - zero_based_idx;
        ui_slider_set_range_and_value(s_slider, config->total_pages - 1, slider_value);
    }

    // 恢复容器显示
    lv_obj_set_hidden(s_cont, is_hidden);
}

/**
 * @brief 销毁历史报告页面
 */
void history_report_page_destroy(void)
{
    if (s_cont != NULL) {
        lv_obj_clean(s_cont);
        s_cont = NULL;
    }

    s_path_label = NULL;
    s_page_info_label = NULL;
    s_slider = NULL;

    uint8_t i;
    for (i = 0; i < HISTORY_REPORT_MENU_NUM; i++) {
        s_left_menu_labels[i] = NULL;
    }
    for (i = 0; i < HR_MAX_DISPLAY_LINES; i++) {
        s_right_content_labels[i] = NULL;
    }
}

/* ============================ 私有函数实现 ============================= */

/**
 * @brief 创建路径标签和黑色横线
 */
static void create_path_label(const char *path_text)
{
    Draw_pagepath(s_path_label, path_text);
}

/**
 * @brief 创建左侧菜单
 */
static void create_left_menu(uint8_t sel_idx)
{
    uint8_t i;

    for (i = 0; i < HISTORY_REPORT_MENU_NUM; i++) {
        s_left_menu_labels[i] = lv_label_create(s_cont, NULL);
        lv_obj_set_style_local_text_font(s_left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);

        // 设置颜色：选中为红色，其他为黑色
        if (i == sel_idx) {
            lv_obj_set_style_local_text_color(s_left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, HR_COLOR_SELECTED);
        } else {
            lv_obj_set_style_local_text_color(s_left_menu_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, HR_COLOR_DEFAULT);
        }

        // 设置文本
        lv_label_set_text(s_left_menu_labels[i], Report_disp[i]);

        // 设置位置
        lv_obj_set_pos(s_left_menu_labels[i], HR_LEFT_MENU_X, HR_LEFT_MENU_BASE_Y + i * HR_LEFT_MENU_SPACING);
    }
}

/**
 * @brief 创建右侧内容
 * @note 使用行号驱动坐标计算：Y = HR_RIGHT_CONTENT_Y + line_index * HR_RIGHT_LINE_SPACING
 */
static void create_right_content(HistoryReportPageConfig *config)
{
    uint8_t i;
    lv_color_t text_color = config->has_records ? HR_COLOR_DEFAULT : HR_COLOR_NO_RECORD;

    for (i = 0; i < HR_MAX_DISPLAY_LINES; i++) {
        s_right_content_labels[i] = lv_label_create(s_cont, NULL);
        lv_obj_set_style_local_text_font(s_right_content_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(s_right_content_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, text_color);

        if (i < config->line_count && config->record_content[i][0] != '\0') {
            lv_label_set_text(s_right_content_labels[i], config->record_content[i]);
        } else {
            lv_label_set_text(s_right_content_labels[i], "");
            lv_obj_set_hidden(s_right_content_labels[i], true);
        }

        // 行号驱动坐标计算
        lv_obj_set_pos(s_right_content_labels[i], HR_RIGHT_CONTENT_X,
                       HR_RIGHT_CONTENT_Y + i * HR_RIGHT_LINE_SPACING);
    }
}

/**
 * @brief 创建页码信息显示
 * @note 页码显示在第0行右侧固定位置
 */
static void create_page_info(const char *page_info)
{
    s_page_info_label = lv_label_create(s_cont, NULL);
    lv_obj_set_style_local_text_font(s_page_info_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_obj_set_style_local_text_color(s_page_info_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, HR_COLOR_DEFAULT);

    lv_label_set_text(s_page_info_label, page_info);
    lv_obj_set_pos(s_page_info_label, HR_RECORD_INDEX_X, HR_RECORD_INDEX_Y);
}

/**
 * @brief 创建滑块
 * @note 进度条是垂直方向，size在最上端，0值在最底下
 *       所以数值需要反过来设置才会有从上到下的填充效果
 */
static void create_slider(uint32_t total_pages, uint32_t current_page)
{
    if (total_pages == 0) {
        s_slider = NULL;
        return;
    }

    // 创建滑块
    s_slider = ui_slider_create(0, total_pages - 1);

    // 设置当前值（转换为0-based索引）
    uint32_t zero_based_idx = (current_page > 0) ? (current_page - 1) : 0;
    uint32_t slider_value = (total_pages - 1) - zero_based_idx;
    lv_slider_set_value(s_slider, slider_value, LV_ANIM_OFF);
}
