#include "app_dev_set.h"
#include "ui_manager.h"
#include "data_config.h"
#include "components/app_set_var_list.h"
#include "app_ip_address.h"
#include "service_dev_set.h"

/**
 * @brief 计算左侧菜单
 * @param  curstate         参数
 * @param  kval             参数
 * @return u8 
 * @date 2026-01-20
 */
u8 app_ds_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 = ds_service_get_menu_len_by_page(PAGE_DEV_SET_SHOW);
    }
    else if (ptype == UI_PWD_TYPE_SUPER)
    {
        menu_size = ds_service_get_menu_len_by_page(PAGE_DEV_SET);
    }
    
    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_ds_cal_right_sel_idx(UICursorState curstate, PwdType ptype , keyValue kval)
{
    u8 itemsize=0;
    u8 cur_idx = curstate.right_data_idx; // 获取当前下标

    if (ptype == UI_PWD_TYPE_NORMAL)    //普通用户
    {
        itemsize = ds_service_get_items_len_by_page(PAGE_DEV_SET_SHOW , curstate.left_menu_idx);
    }
    else if (ptype == UI_PWD_TYPE_SUPER)    //系统用户
    {
        itemsize = ds_service_get_items_len_by_page(PAGE_DEV_SET , curstate.left_menu_idx);
    }

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

        else
            cur_idx = 0;
    }
    else if (kval == KEY_DATA_UP)
    {
        if (cur_idx > 0)
            cur_idx--;
        else
            cur_idx = itemsize - 1;
    }
    else if (kval == KEY_DATA_RIGHT)
    {
        if (cur_idx < (itemsize - itemsize % 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 * ((itemsize - 1) / 8);
    }

    return cur_idx;
}

/**
 * @brief 获取当前所选数据
 * @param  curstate         光标数据
 * @return Para_OPTTypeDef 
 * @date 2026-01-12
 */
// Para_OPTTypeDef app_dev_set_get_manu_data(UICursorState curstate)
// {
//     Para_OPTTypeDef *manu ;
//     switch (curstate.left_menu_idx)      //通过左侧选项判断使用哪个数组显示数据
//     {
//     case SUB_DEV_OPT: // 设备参数
//         if (ui_manager_get_cur_pwdtype() == UI_PWD_TYPE_SUPER)
//         {
//             manu = Device_parset_opt;
//         }
//         else
//         {
//             manu = Dev_usrset_opt;
//         }
//         break;
//     case SUB_COM_OPT:
//         manu = Serial_Statute_opt;
//         break;

//     case SUB_COMSTATUTE_OPT:
//         manu = Statute_par_opt;
//         break;
//     default:
//         manu = Device_parset_opt;
//         break;
//     }

//     return manu[curstate.right_data_idx];
// }
DataModule app_ds_get_module_type(void)
{
    if (ui_manager_get_cur_pwdtype() == UI_PWD_TYPE_SUPER)
    {
        return MODULE_DEV_SET;
    }
    else
    {
        return MODULE_DEV_SET_SHOW;
    }
}

/**
 * @brief  装置设置页面处理组件点击事件
 * @param  kval             键盘值
 * @date 2026-01-12
 */
void app_ds_process_compenents_handle_event(keyValue kval)
{
    UICursorState curstate = ui_manager_get_cursor();
    float fval = 0;
    uint8_t u8val = 0;
    uint32_t u32val = 0;
    GeneralListPageConfig_t config = {0};
    PageCtx dev_info_ctx = {0};
    switch (curstate.cursor_pos)
    {
    case CURSOR_CHANGE_VAL:
        fval = app_set_var_list_handle_event(kval);
        if (kval == KEY_DATA_ENTER) //
        {            
            //修改数据
            ds_service_modify_menu_data(app_ds_get_module_type() ,curstate.left_menu_idx , curstate.right_data_idx , fval); 
            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);
        }
        else if (kval == KEY_DATA_BACK || kval == KEY_DATA_CANCEL)
        {
            //返回或者取消则返回菜单界面
            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 CURSOR_SET_IP:
        u8val = app_ip_address_handle_event(kval);
        
        if (kval == KEY_DATA_ENTER)
        {
            // printf("%s val = %d \n", __FUNCTION__ , val);
            switch (u8val)
            {
            case IP_ADDRESS_OP_OK:
                
                u32val = ui_manager_comp_get_ip_address_value();
                ds_service_modify_menu_data_ip(app_ds_get_module_type(), curstate.left_menu_idx, curstate.right_data_idx, u32val);
                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 IP_ADDRESS_OP_CANCEL:
                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;
            default:
                break;
            }
        }
        else if (kval == KEY_DATA_CANCEL || kval == KEY_DATA_BACK)
        {
            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 CURSOR_CONFIRM_SAVE:   //确认保存
        u8val = app_confirm_handle_event(kval);
        if (kval == KEY_DATA_ENTER)
        {
            // 确认保存
            if (u8val)
            {
                // 确认保存
                ds_set_data_to_system(app_ds_get_module_type());
                printf("%s confirm save\n", __FUNCTION__);
            }
            else 
            {
                // 取消保存
                ds_service_restore_default_values(app_ds_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_SET); //回去菜单页
        }
        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);
            ds_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_dev_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_ds_process_compenents_handle_event(kval);
        return;
    }

    switch (kval)
    {
    case KEY_DATA_BACK:
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)    
        {
            // 检查是否有数据差异
            if (ds_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_SET); // 回去菜单页
            }

        }
        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);
            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);
        }
        else if(curstate.cursor_pos = CURSOR_RIGHT_DATA)
        {

            UiSetValueOptDef set_opt = {0};
            ds_service_get_sel_setval_data(ui_manager_get_page_ctx() , &set_opt);

            if(set_opt.type == UISETOPT_TYPE_LIST || set_opt.type == UISETOPT_TYPE_NUMBER)
            {
                curstate.cursor_pos = CURSOR_CHANGE_VAL;
                ui_manager_cursor_set_value(curstate);
                ui_manager_comp_create_set_valist(&set_opt);
            }
            else if (set_opt.type == UISETOPT_TYPE_IP)
            {
                curstate.cursor_pos = CURSOR_SET_IP;
                ui_manager_cursor_set_value(curstate);
                ui_manager_comp_create_ip_address(set_opt.curr_ip);
            }
        }
        break;
    case KEY_DATA_DOWN:     //下键
    case KEY_DATA_RIGHT:    //右键
    case KEY_DATA_UP:       //上键
    case KEY_DATA_LEFT:     //左键
        //计算下标
        if (curstate.cursor_pos == CURSOR_LEFT_MENU)
        {   
            curstate.left_menu_idx = app_ds_cal_left_sel_idx(curstate, ui_manager_get_cur_pwdtype() , kval);
        }
        else if(curstate.cursor_pos = CURSOR_RIGHT_DATA)
        {
            curstate.right_data_idx = app_ds_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;
    }
}


void app_dev_set_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);
}