#include "ui_comp_ip_address.h"
#include "ui_cont.h"
#include "data_config.h"

// 组件变量
static lv_obj_t *win_ip_address;
static lv_obj_t *btn_ok;
static lv_obj_t *btn_cancel;
static lv_obj_t *label_ok;
static lv_obj_t *label_cancel;
static lv_obj_t *label_message;
static lv_obj_t *label_error;
static lv_obj_t *input_fields[4]; // 存储spinbox对象
static lv_obj_t *dot_labels[3];
static lv_obj_t *label_guide;
static int current_focus = 0; // 0-3: 输入框, 4: 确定按钮, 5: 取消按钮
static int current_section = 0; // 当前输入的IP段
static ip_address_op_t ip_address_result = IP_ADDRESS_OP_CANCEL;

// 初始化IP段输入
static void init_ip_sections(uint32_t ip_address) {
    // 将32位IP地址转换为四个段
    int octet1 = (ip_address >> 24) & 0xFF;
    int octet2 = (ip_address >> 16) & 0xFF;
    int octet3 = (ip_address >> 8) & 0xFF;
    int octet4 = ip_address & 0xFF;
    
    // 设置到输入框
    if (input_fields[0]) lv_spinbox_set_value(input_fields[0], octet1);
    if (input_fields[1]) lv_spinbox_set_value(input_fields[1], octet2);
    if (input_fields[2]) lv_spinbox_set_value(input_fields[2], octet3);
    if (input_fields[3]) lv_spinbox_set_value(input_fields[3], octet4);
}

// 更新输入框显示
static void update_input_fields(void) {
    for (int i = 0; i < 4; i++) {
        // 更新输入框样式
        if (current_focus == i) {
            lv_obj_set_style_local_text_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
            lv_obj_set_style_local_bg_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
            // 启用当前焦点输入框的光标闪烁
            lv_textarea_set_cursor_blink_time(input_fields[i], 400);
            lv_textarea_set_cursor_hidden(input_fields[i], false);
        } else {
            lv_obj_set_style_local_text_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
            lv_obj_set_style_local_bg_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
            // 禁用其他输入框的光标闪烁
            lv_textarea_set_cursor_blink_time(input_fields[i], 0);
            lv_textarea_set_cursor_hidden(input_fields[i], true);
        }
    }
}

// 更新按钮焦点状态
static void update_button_focus(void) {
    // 重置所有按钮状态
    if (label_ok) {
        lv_obj_set_style_local_text_color(label_ok, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    }
    if (label_cancel) {
        lv_obj_set_style_local_text_color(label_cancel, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    }
    
    // 设置当前焦点按钮字体颜色为红色
    if (current_focus == 4 && label_ok) {
        lv_obj_set_style_local_text_color(label_ok, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    } else if (current_focus == 5 && label_cancel) {
        lv_obj_set_style_local_text_color(label_cancel, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    }
}

// 更新操作指引
static void update_guide(void) {
    if (current_focus < 4) {
        lv_label_set_text(label_guide, "上/下: 增减数字 左/右: 导航光标 确定: 编辑");
    } else {
        lv_label_set_text(label_guide, "左/右: 切换按钮 确定: 选择");
    }
}

// 检查IP地址是否有效
static bool is_valid_ip_address(void) {
    for (int i = 0; i < 4; i++) {
        int value = lv_spinbox_get_value(input_fields[i]);
        if (value < 0 || value > 255) {
            return false;
        }
    }
    return true;
}

// 更新错误提示
static void update_error(void) {
    if (is_valid_ip_address()) {
        lv_label_set_text(label_error, "");
        lv_obj_set_style_local_text_color(label_error, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
    } else {
        lv_label_set_text(label_error, "IP地址格式错误");
        lv_obj_set_style_local_text_color(label_error, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    }
}

// 创建IP地址设置组件
void ui_comp_ip_address_create(uint32_t ip_address) {
    // 创建背景遮罩
    lv_obj_t *bg_mask = lv_obj_create(ui_get_cont(), NULL);
    lv_obj_set_size(bg_mask, LV_HOR_RES_MAX, LV_VER_RES_MAX);
    lv_obj_set_style_local_bg_color(bg_mask, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_bg_opa(bg_mask, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_50);
    
    // 创建窗体对象
    win_ip_address = lv_win_create(bg_mask, NULL);
    lv_obj_set_size(win_ip_address, 320, 220);
    lv_obj_set_style_local_text_font(win_ip_address, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_obj_set_style_local_bg_opa(win_ip_address, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_100);
    lv_obj_set_style_local_border_opa(win_ip_address, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_100);
    lv_obj_set_style_local_bg_color(win_ip_address, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
    lv_win_set_title(win_ip_address, "IP地址设置");
    lv_win_set_btn_width(win_ip_address, 30);
    lv_obj_align(win_ip_address, NULL, LV_ALIGN_CENTER, 0, 0);
    
    // 创建提示信息标签
    label_message = lv_label_create(win_ip_address, NULL);
    lv_obj_set_style_local_text_font(label_message, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_obj_set_style_local_text_color(label_message, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_label_set_text(label_message, "请输入IP地址：");
    lv_obj_align(label_message, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 20);
    
    // 创建IP地址输入框
    int start_x = 40;
    int y = 60;
    int input_width = 50;
    int input_height = 30;
    int spacing = 10;
    
    for (int i = 0; i < 4; i++) {
        // 创建spinbox对象
        input_fields[i] = lv_spinbox_create(win_ip_address, NULL);
        lv_obj_set_size(input_fields[i], input_width, input_height);
        lv_obj_set_style_local_text_font(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
        lv_obj_set_style_local_bg_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        lv_obj_set_style_local_border_color(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
        lv_obj_set_style_local_border_width(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 2);
        lv_obj_set_style_local_pad_all(input_fields[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 5);
        
        // 配置spinbox
        lv_spinbox_set_range(input_fields[i], 0, 255); // 设置范围为0-255
        lv_spinbox_set_step(input_fields[i], 1); // 设置步长为1
        lv_spinbox_set_digit_format(input_fields[i], 3, 0); // 设置显示格式，3位数字，无小数
        
        // 禁用光标闪烁
        lv_textarea_set_cursor_blink_time(input_fields[i], 0);
        lv_textarea_set_cursor_hidden(input_fields[i], true);
        
        // 对齐
        lv_obj_align(input_fields[i], NULL, LV_ALIGN_IN_TOP_LEFT, start_x + (input_width + spacing) * i, y);
        
        if (i < 3) {
            dot_labels[i] = lv_label_create(win_ip_address, NULL);
            lv_obj_set_style_local_text_font(dot_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
            lv_obj_set_style_local_text_color(dot_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
            lv_label_set_text(dot_labels[i], ".");
            lv_obj_align(dot_labels[i], input_fields[i], LV_ALIGN_OUT_RIGHT_MID, spacing / 2, 0);
        }
    }
    
    // 初始化IP段
    init_ip_sections(ip_address);
    
    // 创建错误提示标签
    label_error = lv_label_create(win_ip_address, NULL);
    lv_obj_set_style_local_text_font(label_error, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_obj_set_style_local_text_color(label_error, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_label_set_text(label_error, "");
    lv_obj_align(label_error, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 100);
    
    // 创建操作指引标签
    label_guide = lv_label_create(win_ip_address, NULL);
    lv_obj_set_style_local_text_font(label_guide, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_obj_set_style_local_text_color(label_guide, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_label_set_text(label_guide, "上/下: 增减数字 左/右: 切换输入框");
    lv_obj_align(label_guide, NULL, LV_ALIGN_IN_TOP_LEFT, 15, 128);
    
    // 创建确定按钮
    btn_ok = lv_btn_create(win_ip_address, NULL);
    lv_obj_set_size(btn_ok, 80, 30);
    lv_obj_align(btn_ok, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 40, -15);
    
    label_ok = lv_label_create(btn_ok, NULL);
    lv_obj_set_style_local_text_font(label_ok, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_label_set_text(label_ok, "确定");
    
    // 创建取消按钮
    btn_cancel = lv_btn_create(win_ip_address, NULL);
    lv_obj_set_size(btn_cancel, 80, 30);
    lv_obj_align(btn_cancel, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -40, -15);
    
    label_cancel = lv_label_create(btn_cancel, NULL);
    lv_obj_set_style_local_text_font(label_cancel, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_label_set_text(label_cancel, "取消");
    
    // 初始设置焦点
    current_focus = 0;
    update_input_fields();
    update_button_focus();
    update_error();
    update_guide();
}

// 处理上方向键操作
void ui_comp_ip_address_handle_up(void) {
    if (current_focus < 4) {
        // 增加当前IP段的数值
        lv_spinbox_increment(input_fields[current_focus]);
        update_error();
    }
}

// 处理下方向键操作
void ui_comp_ip_address_handle_down(void) {
    if (current_focus < 4) {
        // 减少当前IP段的数值
        lv_spinbox_decrement(input_fields[current_focus]);
        update_error();
    }
}

// 处理左方向键操作
void ui_comp_ip_address_handle_left(void) {
    if (current_focus < 4) {
        // 获取当前输入框的光标位置
        uint32_t cursor_pos = lv_textarea_get_cursor_pos(input_fields[current_focus]);
        
        if (cursor_pos == 0) {
            // 光标在输入框首位
            if (current_focus > 0) {
                // 不是第一个输入框，切换到上一个输入框的最后一位
                current_focus--;
                current_section = current_focus;
                // 设置光标到新输入框的最后一位
                lv_textarea_set_cursor_pos(input_fields[current_focus], 2);
                update_input_fields();
                update_button_focus();
                update_guide();
            } else {
                // 是第一个输入框，切换到取消按钮
                current_focus = 5;
                update_input_fields();
                update_button_focus();
                update_guide();
            }
        } else {
            // 光标在输入框内非首位，向左移动光标
            lv_spinbox_step_prev(input_fields[current_focus]);
        }
    } else if (current_focus > 4) {
        // 在按钮区域，向左切换按钮
        current_focus--;
        update_input_fields();
        update_button_focus();
        update_guide();
    }
}

// 处理右方向键操作
void ui_comp_ip_address_handle_right(void) {
    if (current_focus < 4) {
        // 获取当前输入框的光标位置
        uint32_t cursor_pos = lv_textarea_get_cursor_pos(input_fields[current_focus]);
        
        if (cursor_pos == 2) {
            // 光标在输入框末位
            if (current_focus < 3) {
                // 不是最后一个输入框，切换到下一个输入框的首位
                current_focus++;
                current_section = current_focus;
                // 设置光标到新输入框的首位
                lv_textarea_set_cursor_pos(input_fields[current_focus], 0);
                update_input_fields();
                update_button_focus();
                update_guide();
            } else {
                // 是最后一个输入框，切换到确定按钮
                current_focus = 4;
                update_input_fields();
                update_button_focus();
                update_guide();
            }
        } else {
            // 光标在输入框内非末位，向右移动光标
            lv_spinbox_step_next(input_fields[current_focus]);
        }
    } else if (current_focus < 5) {
        // 在按钮区域，向右切换按钮
        current_focus++;
        update_input_fields();
        update_button_focus();
        update_guide();
    } else if (current_focus == 5) {
        // 在取消按钮上，向右切换到第一个输入框
        current_focus = 0;
        current_section = 0;
        // 设置光标到第一个输入框的首位
        lv_textarea_set_cursor_pos(input_fields[current_focus], 0);
        update_input_fields();
        update_button_focus();
        update_guide();
    }
}

// 处理确定键操作
void ui_comp_ip_address_handle_enter(void) {
    // printf("%s current_focus = %d\n", __FUNCTION__, current_focus);
    ip_address_result = IP_ADDRESS_OP_OTHER;
    if (current_focus == 4) {
        // 点击确定按钮
        if (is_valid_ip_address()) {
            // 禁用所有输入框的光标闪烁
            for (int i = 0; i < 4; i++) {
                lv_textarea_set_cursor_blink_time(input_fields[i], 0);
                lv_textarea_set_cursor_hidden(input_fields[i], true);
            }
            ip_address_result = IP_ADDRESS_OP_OK;
        }
    } else if (current_focus == 5) {
        // 点击取消按钮
        // 禁用所有输入框的光标闪烁
        for (int i = 0; i < 4; i++) {
            lv_textarea_set_cursor_blink_time(input_fields[i], 0);
            lv_textarea_set_cursor_hidden(input_fields[i], true);
        }
        ip_address_result = IP_ADDRESS_OP_CANCEL;
    }
    
    // 只有在按钮区域按Enter键才会设置结果
    // 输入框内按Enter键不做任何操作，保持编辑状态
}

// 获取IP地址设置结果
ip_address_op_t ui_comp_ip_address_get_result(void) {
    return ip_address_result;
}

// 获取输入的IP地址
uint32_t ui_comp_ip_address_get_value(void) {
    if (!is_valid_ip_address()) {
        return 0; // 返回0作为错误值
    }
    
    // 获取四个IP段的值
    uint8_t octet1 = (uint8_t)lv_spinbox_get_value(input_fields[0]);
    uint8_t octet2 = (uint8_t)lv_spinbox_get_value(input_fields[1]);
    uint8_t octet3 = (uint8_t)lv_spinbox_get_value(input_fields[2]);
    uint8_t octet4 = (uint8_t)lv_spinbox_get_value(input_fields[3]);
    
    // 转换为32位无符号整数（网络字节序，大端模式）
    uint32_t ip_value = (uint32_t)octet1 << 24;
    ip_value |= (uint32_t)octet2 << 16;
    ip_value |= (uint32_t)octet3 << 8;
    ip_value |= (uint32_t)octet4;
    
    return ip_value;
}
