#include "app_history_report.h"
#include "ui/ui_manager.h"
#include "service/service_history_report.h"
#include "data/data_history_report.h"
#include <stdio.h>

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

static HistoryReportPageConfig s_page_config = {0};
static uint8_t s_current_menu_idx = 0;
static uint32_t s_current_page_idx = 0;   // 当前页码索引（从0开始）
static bool s_cursor_in_right = false;

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

static void refresh_page(void);
static void switch_menu(uint8_t new_idx);
static void switch_page(uint32_t new_page);
static void move_to_next_page(void);
static void move_to_prev_page(void);

// 判断是否为多记录类型
static bool is_multi_record_type(uint8_t menu_idx);

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

/**
 * @brief 创建历史报告页面数据
 */
void app_history_report_create_list_data(PageCtx *ctx, uint8_t menu_sel_idx, uint32_t record_idx)
{
    if (ctx == NULL) {
        return;
    }

    // 初始化service层
    history_report_service_init();

    // 初始化状态
    s_current_menu_idx = menu_sel_idx;
    s_current_page_idx = 0;  // 页码从0开始
    s_cursor_in_right = false;

    // 获取页面数据
    history_report_service_get_page_data((HistoryReportType)s_current_menu_idx,
                                          s_current_page_idx,
                                          &s_page_config);
    s_page_config.cursor_in_right = s_cursor_in_right;
    ui_manager_history_report_create(ctx, &s_page_config);
}

/**
 * @brief 处理历史报告页面按键事件
 */
void app_history_report_handle_event(keyValue kval)
{
    switch (kval) {
        case KEY_DATA_UP:
            if (s_cursor_in_right) {
                // 光标在右侧，上一页
                move_to_prev_page();
            } else {
                // 光标在左侧，切换到上一菜单项
                if (s_current_menu_idx > 0) {
                    switch_menu(s_current_menu_idx - 1);
                } else {
                    switch_menu(HISTORY_REPORT_MENU_NUM - 1);
                }
            }
            break;

        case KEY_DATA_DOWN:
            if (s_cursor_in_right) {
                // 光标在右侧，下一页
                move_to_next_page();
            } else {
                // 光标在左侧，切换到下一菜单项
                if (s_current_menu_idx < HISTORY_REPORT_MENU_NUM - 1) {
                    switch_menu(s_current_menu_idx + 1);
                } else {
                    switch_menu(0);
                }
            }
            break;

        case KEY_DATA_LEFT:
            if (s_cursor_in_right) {
                // 光标在右侧，上一页
                move_to_prev_page();
            } else {
                // 左侧菜单循环切换
                if (s_current_menu_idx > 0) {
                    switch_menu(s_current_menu_idx - 1);
                } else {
                    switch_menu(HISTORY_REPORT_MENU_NUM - 1);
                }
            }
            break;

        case KEY_DATA_RIGHT:
            if (!s_cursor_in_right) {
                // 从左侧进入右侧内容区
                s_cursor_in_right = true;
                s_page_config.cursor_in_right = true;
                refresh_page();
            } else {
                // 光标在右侧，下一页
                move_to_next_page();
            }
            break;

        case KEY_DATA_ENTER:
            if (!s_cursor_in_right) {
                // 从左侧进入右侧内容区
                s_cursor_in_right = true;
                s_page_config.cursor_in_right = true;
                refresh_page();
            }
            // 光标在右侧时，ENTER键暂无特殊操作
            break;

        case KEY_DATA_BACK:
            if (s_cursor_in_right) {
                // 从右侧返回左侧菜单
                s_cursor_in_right = false;
                s_page_config.cursor_in_right = false;
                refresh_page();
            } else {
                // 返回主菜单
                ui_manager_switch_to_main_menu(MAIN_MENU_HISTORY_DATA);
            }
            break;

        default:
            break;
    }
}

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

/**
 * @brief 判断是否为多记录类型（每页显示两条记录）
 */
static bool is_multi_record_type(uint8_t menu_idx)
{
    return (menu_idx == HISTORY_REPORT_SOE ||
            menu_idx == HISTORY_REPORT_REMOTE ||
            menu_idx == HISTORY_REPORT_SETTING);
}

/**
 * @brief 刷新页面
 */
static void refresh_page(void)
{
    // 重新获取页面数据
    history_report_service_get_page_data((HistoryReportType)s_current_menu_idx,
                                          s_current_page_idx,
                                          &s_page_config);

    // 设置光标位置
    s_page_config.cursor_in_right = s_cursor_in_right;

    // 获取当前光标状态
    UICursorState curstate = ui_manager_get_cursor();

    // 通过ui_manager刷新历史报告页面
    ui_manager_history_report_refresh(curstate, &s_page_config);
}

/**
 * @brief 切换菜单
 */
static void switch_menu(uint8_t new_idx)
{
    if (new_idx >= HISTORY_REPORT_MENU_NUM) {
        return;
    }

    s_current_menu_idx = new_idx;
    s_current_page_idx = 0;  // 切换菜单时重置页码

    // 构建页面上下文
    PageCtx ctx = {0};
    ctx.cur_page = PAGE_HISTORY_DATA;
    ctx.cursor_state.cursor_pos = s_cursor_in_right ? CURSOR_RIGHT_DATA : CURSOR_LEFT_MENU;
    ctx.cursor_state.left_menu_idx = s_current_menu_idx;
    ctx.cursor_state.right_data_idx = 0;

    // 获取页面数据并创建页面
    history_report_service_get_page_data((HistoryReportType)s_current_menu_idx,
                                          s_current_page_idx,
                                          &s_page_config);
    s_page_config.cursor_in_right = s_cursor_in_right;
    ui_manager_history_report_create(&ctx, &s_page_config);
}

/**
 * @brief 切换页面
 */
static void switch_page(uint32_t new_page)
{
    uint32_t total_pages = history_report_service_get_total_pages((HistoryReportType)s_current_menu_idx);

    if (total_pages == 0) {
        return;
    }

    if (new_page >= total_pages) {
        new_page = 0;  // 循环到第一页
    }

    s_current_page_idx = new_page;
    refresh_page();
}

/**
 * @brief 移动到下一页
 */
static void move_to_next_page(void)
{
    uint32_t total_pages = history_report_service_get_total_pages((HistoryReportType)s_current_menu_idx);

    if (total_pages == 0) {
        return;
    }

    uint32_t new_page = s_current_page_idx + 1;
    if (new_page >= total_pages) {
        new_page = 0;  // 循环到第一页
    }

    switch_page(new_page);
}

/**
 * @brief 移动到上一页
 */
static void move_to_prev_page(void)
{
    uint32_t total_pages = history_report_service_get_total_pages((HistoryReportType)s_current_menu_idx);

    if (total_pages == 0) {
        return;
    }

    uint32_t new_page;
    if (s_current_page_idx == 0) {
        new_page = total_pages - 1;  // 循环到最后一页
    } else {
        new_page = s_current_page_idx - 1;
    }

    switch_page(new_page);
}
