#include "app_dev_set_show.h"
#include "ui_manager.h"
#include "data_config.h"
#include "service_dev_set.h"

/**
 * @brief 计算光标所处位置
 * @param  curstate         光标数据
 * @param  kval             键盘值
 * @return u8 
 * @date 2026-01-12
 */
u8 app_dss_cal_right_sel_idx(UICursorState curstate, PwdType ptype , keyValue kval)
{
    u8 itemsize=0;
    u8 cur_idx = curstate.right_data_idx; // 获取当前下标

    //获取数据项数据
    itemsize = ds_service_get_items_len_by_page(PAGE_DEV_SET_SHOW , curstate.left_menu_idx);
    
    switch (kval)
    {
    case KEY_DATA_DOWN:
    case KEY_DATA_RIGHT:
        if (cur_idx < (itemsize - itemsize % 8))
            cur_idx = 8 * (cur_idx / 8) + 8;
        else
            cur_idx = 0;
        
        break;
    case KEY_DATA_UP:
    case KEY_DATA_LEFT:
        if (cur_idx > 7)
            cur_idx = 8 * (cur_idx / 8) - 8;
        else
            cur_idx = 8 * ((itemsize - 1) / 8);

        break;
    default:
        break;
    }
    return cur_idx;
}

/**
 * @brief 装置参数查看页面处理点击事件
 * @param  kval             参数
 * @date 2026-01-12
 */
void app_dev_set_show_handle_event(keyValue kval)
{
    UICursorState curstate = ui_manager_get_cursor();
    PageCtx dev_info_ctx = {0};
    GeneralListPageConfig_t config = {0};
    switch (kval)
    {
    case KEY_DATA_BACK:
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)        //光标在左边
        {
            ui_manager_switch_to_main_menu(MAIN_MENU_DEV_SET_SHOW);     //回到首页
        }
        else if (curstate.cursor_pos == CURSOR_RIGHT_DATA)  //光标在右边
        {
            curstate.cursor_pos = CURSOR_LEFT_MENU;
            ui_manager_cursor_set_value(curstate);
            ds_service_get_show_ui_data(ui_manager_get_page_ctx() , curstate.left_menu_idx , curstate.right_data_idx, &config);
            ui_manager_select_list_data(curstate , &config);
        }
        break;
    case KEY_DATA_ENTER:
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)
        {
            //按确定进去数据区
            curstate.cursor_pos = CURSOR_RIGHT_DATA;
            ui_manager_cursor_set_value(curstate);
            ds_service_get_show_ui_data(ui_manager_get_page_ctx() , curstate.left_menu_idx , curstate.right_data_idx, &config);
            ui_manager_select_list_data(curstate , &config);
        }
        break;
    case KEY_DATA_DOWN:
    case KEY_DATA_RIGHT:
    case KEY_DATA_UP:
    case KEY_DATA_LEFT:
        //该页面只有一个菜单，所以左边菜单不需要做操作
        if (curstate.cursor_pos == CURSOR_RIGHT_DATA)
        {
            curstate.right_data_idx = app_dss_cal_right_sel_idx(curstate, ui_manager_get_cur_pwdtype() , kval);
            ui_manager_cursor_set_value(curstate);
            ds_service_get_show_ui_data(ui_manager_get_page_ctx()  , curstate.left_menu_idx , curstate.right_data_idx, &config);
            ui_manager_select_list_data(curstate , &config);
        }
        
        break;
    default:
        break;
    }
}

/**
 * @brief  创建装置参数查看页面列表数据
 * @param  ctx              参数
 * @param  manu_sel_idx     参数
 * @param  item_sel_idex    参数
 * @date 2026-01-12
 */
void app_dev_set_show_create_list_data(PageCtx *ctx , u8 manu_sel_idx , u8 item_sel_idex)
{
    GeneralListPageConfig_t config = {0};
    
    ds_service_get_show_ui_data(ctx, 0, 0, &config);

    ui_manager_gerneral_list(ctx , &config);
}