#include "app_fix_set.h"
#include "service_fix_set.h"
#include "ui_manager.h"
#include "app_set_var_list.h"
#include "app_confirm.h"

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

    if (ptype == UI_PWD_TYPE_NORMAL)
    {
        menu_size = fs_service_get_menu_len_by_page(PAGE_DEV_FIX_SHOW);
    }
    else if (ptype == UI_PWD_TYPE_SUPER)
    {
        menu_size = fs_service_get_menu_len_by_page(PAGE_DEV_FIX);
    }
    
    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_fs_cal_right_sel_idx(UICursorState curstate, PwdType ptype  , keyValue kval)
{
    u8 menu_len=0;
    u8 cur_idx = curstate.right_data_idx; // 获取当前下标


    
    if (ptype == UI_PWD_TYPE_NORMAL)    //普通用户
    {
        menu_len = fs_service_get_items_len_by_page(PAGE_DEV_FIX_SHOW , curstate.left_menu_idx);
    }
    else if (ptype == UI_PWD_TYPE_SUPER)    //系统用户
    {
        menu_len = fs_service_get_items_len_by_page(PAGE_DEV_FIX , 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;
}

DataModule app_fs_get_module_type(void)
{
    if (ui_manager_get_cur_pwdtype() == UI_PWD_TYPE_SUPER)
    {
        return MODULE_DEV_FIX;
    }
    else
    {
        return MODULE_DEV_FIX_SHOW;
    }
}

/**
 * @brief  装置设置页面处理组件点击事件
 * @param  kval             键盘值
 * @date 2026-01-12
 */
void app_fs_process_compenents_handle_event(keyValue kval)
{
    UICursorState curstate = ui_manager_get_cursor();
    float val = 0;
    GeneralListPageConfig_t config = {0};
    PageCtx dev_info_ctx = {0};
    switch (curstate.cursor_pos)
    {
    case CURSOR_CHANGE_VAL:     //设置值
        val = app_set_var_list_handle_event(kval);
        if (kval == KEY_DATA_ENTER) //确认修改
        {
            printf("%s touch enter key , left menu index = %d , val = %f\n", __FUNCTION__, curstate.left_menu_idx, val);
            //修改数据
            fs_service_modify_menu_data(app_fs_get_module_type() ,curstate.left_menu_idx , curstate.right_data_idx , val); 
            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 (kval == KEY_DATA_BACK || kval == KEY_DATA_CANCEL) 
        {
            //返回或者取消则返回菜单界面
            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);
        }
        
        break;
    case CURSOR_CONFIRM_SAVE:   //确认保存
        val = app_confirm_handle_event(kval);
        if (kval == KEY_DATA_ENTER)
        {
            // 确认保存
            if (val)
            {
                // 确认保存
                fs_set_data_to_system(app_fs_get_module_type());
                printf("%s confirm save\n", __FUNCTION__);
            }
            else 
            {
                // 取消保存
                fs_service_restore_default_values(app_fs_get_module_type());
                printf("%s cancel save\n", __FUNCTION__);
            }
            // ui_manager_comp_show_confirm_save(false);
            ui_manager_switch_to_main_menu(MAIN_MENU_DEV_FIX); //回去菜单页
        }
        else if (kval == KEY_DATA_BACK || kval == KEY_DATA_CANCEL)
        {
            // ui_manager_comp_show_confirm_save(false);
            // 返回或取消，返回菜单界面
            curstate.cursor_pos = CURSOR_LEFT_MENU;
            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);
        }
        break;
    default:
        break;
    }
}

/**
 * @brief  装置设置页面处理页面点击事件
 * @param  kval             键盘值
 * @date 2026-01-12
 */
void app_fix_set_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_fs_process_compenents_handle_event(kval);
        return;
    }

    switch (kval)
    {
    case KEY_DATA_BACK: //返回
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)
        {
            // 检查是否有数据差异
            if (fs_service_check_diff())
            {
                // 有差异，显示确认保存弹窗
                curstate.cursor_pos = CURSOR_CONFIRM_SAVE;
                ui_manager_cursor_set_value(curstate);
                ui_manager_comp_create_confirm_save();
            }
            else
            {
                // 无差异，直接返回菜单页
                ui_manager_switch_to_main_menu(MAIN_MENU_DEV_FIX); //回去菜单页
            }
        }
        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)
        {
            printf("%s touch enter!!\n", __FUNCTION__);

            // 检查菜单是否启用
            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;
            }

            // 检查是否为"不停电传动投入"软压板，设置为只读
            if (fs_service_is_trasw_yb_selected(ui_manager_get_page_ctx()))
            {
                printf("%s: SET_TRASW_YB is read-only, cannot modify\n", __FUNCTION__);
                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;
            }

            UiSetValueOptDef set_opt = {0};
            fs_service_get_sel_setval_data(ui_manager_get_page_ctx() , &set_opt); 
            curstate.cursor_pos = CURSOR_CHANGE_VAL;
            ui_manager_cursor_set_value(curstate);
            ui_manager_comp_create_set_valist(&set_opt);
        }
        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_fs_cal_left_sel_idx(curstate, ui_manager_get_cur_pwdtype() , kval);
        }
        else if(curstate.cursor_pos = CURSOR_RIGHT_DATA)
        {
            curstate.right_data_idx = app_fs_cal_right_sel_idx(curstate, ui_manager_get_cur_pwdtype() , 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_set_create_list_data(PageCtx *ctx , u8 menu_sel_idx , u8 item_sel_idex)
{
    GeneralListPageConfig_t config = {0};

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

    ui_manager_gerneral_list(ctx, &config);
}