#include "app_fix_show.h"
#include "service_fix_set.h"
#include "ui_manager.h"


/**
 * @brief 计算左侧菜单
 * @param  curstate         参数
 * @param  kval             参数
 * @return u8 
 * @date 2026-01-20
 */
u8 app_fshow_cal_left_sel_idx(UICursorState curstate , keyValue kval)
{
    u8 menu_size=0;
    u8 cur_idx = curstate.left_menu_idx; // 获取当前下标

    menu_size = fs_service_get_menu_len_by_page(PAGE_DEV_FIX_SHOW);
    printf("%s menu size = %d \n", __FUNCTION__,menu_size);
    switch (kval)
    {
    case KEY_DATA_LEFT:
        if (cur_idx > 7)
            cur_idx = 8 * (cur_idx / 8) - 8;
        else
            cur_idx = 8 * ((menu_size - 1) / 8);
        break;
    case KEY_DATA_UP:
        if (cur_idx > 0)
            cur_idx--;
        else
            cur_idx = menu_size - 1;
        break;
    case KEY_DATA_RIGHT:
        if (cur_idx < (menu_size - menu_size % 8))
            cur_idx = 8 * (cur_idx / 8) + 8;
        else
            cur_idx = 0;
    case KEY_DATA_DOWN:
        cur_idx ++;
        if (cur_idx > menu_size -1)
            cur_idx = 0;
        
    default:
        break;
    }
    return  cur_idx;
}
/**
 * @brief 计算光标所处位置
 * @param  curstate         光标数据
 * @param  kval             键盘值
 * @return u8 
 * @date 2026-01-12
 */
u8 app_fshow_cal_right_sel_idx(UICursorState curstate , keyValue kval)
{
    u8 menu_len=0;
    u8 cur_idx = curstate.right_data_idx; // 获取当前下标

    menu_len = fs_service_get_items_len_by_page(PAGE_DEV_FIX_SHOW , curstate.left_menu_idx);

    if (kval == KEY_DATA_DOWN)
    {
        if (cur_idx < menu_len - 1)
            cur_idx++;

        else
            cur_idx = 0;
    }
    else if (kval == KEY_DATA_UP)
    {
        if (cur_idx > 0)
            cur_idx--;
        else
            cur_idx = menu_len - 1;
    }
    else if (kval == KEY_DATA_RIGHT)
    {
        if (cur_idx < (menu_len - menu_len % 8))
            cur_idx = 8 * (cur_idx / 8) + 8;
        else
            cur_idx = 0;
    }
    else if (kval == KEY_DATA_LEFT)
    {
        if (cur_idx > 7)
            cur_idx = 8 * (cur_idx / 8) - 8;
        else
            cur_idx = 8 * ((menu_len - 1) / 8);
    }

    return cur_idx;
}

void app_fix_show_handle_event(keyValue kval)
{
    UICursorState curstate = ui_manager_get_cursor();
    PageCtx dev_info_ctx = {0};
    GeneralListPageConfig_t config = {0};

    if (curstate.cursor_pos > CURSOR_RIGHT_DATA)
    {
        app_ds_process_compenents_handle_event(kval);
        return;
    }

    switch (kval)
    {
    case KEY_DATA_BACK: //返回
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)
        {
            ui_manager_switch_to_main_menu(MAIN_MENU_DEV_FIX_SHOW); //回去菜单页
        }
        else if (curstate.cursor_pos == CURSOR_RIGHT_DATA)
        {
            curstate.cursor_pos = CURSOR_LEFT_MENU; //光标回左边
            curstate.right_data_idx = 0;            //下标值清空
            ui_manager_cursor_set_value(curstate);
            fs_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;
        break;
    case KEY_DATA_ENTER: //确定
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)
        {
            //按确定进去数据区
            curstate.cursor_pos = CURSOR_RIGHT_DATA;
            ui_manager_cursor_set_value(curstate);
            fs_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);
        }
        else if(curstate.cursor_pos = CURSOR_RIGHT_DATA)
        {
            if (!fs_service_is_menu_enabled(ui_manager_get_page_ctx(), curstate.left_menu_idx))
            {
                fs_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;
            }
        }
        break;
    case KEY_DATA_UP:   //上
    case KEY_DATA_DOWN: //下
    case KEY_DATA_LEFT: //左
    case KEY_DATA_RIGHT://右
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)
        {
            curstate.left_menu_idx = app_fshow_cal_left_sel_idx(curstate , kval);
        }
        else if(curstate.cursor_pos = CURSOR_RIGHT_DATA)
        {
            curstate.right_data_idx = app_fshow_cal_right_sel_idx(curstate , kval);
        }

        ui_manager_cursor_set_value(curstate);
        fs_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;
    }
}

void app_fix_show_create_list_data(PageCtx *ctx , u8 menu_sel_idx , u8 item_sel_idex)
{
    printf("%s \n", __FUNCTION__);
    GeneralListPageConfig_t config = {0};

    fs_service_get_show_ui_data(ctx , menu_sel_idx , item_sel_idex, &config);

    ui_manager_gerneral_list(ctx, &config);
}