#include "ui_comp_confirm_dialog.h"
#include "ui_cont.h"
#include "data_config.h"

// 组件变量
static lv_obj_t *win_confirm;
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 int current_focus = CONFIRM_DIALOG_OP_OK;

// 默认标题和内容
static const char *default_title = "确认保存";
static const char *default_message = "参数改变,确定需要保存?";


// 更新按钮焦点状态
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 == CONFIRM_DIALOG_OP_OK && 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 == CONFIRM_DIALOG_OP_CANCEL && label_cancel) {
        lv_obj_set_style_local_text_color(label_cancel, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
    }
}

// 内部创建函数，支持自定义标题和消息
static void ui_comp_confirm_dialog_create_internal(const char *title, const char *message)
{
    // 使用传入的参数，如果为NULL则使用默认值
    const char *dlg_title = (title != NULL) ? title : default_title;
    const char *dlg_message = (message != NULL) ? message : default_message;
    
    // 创建背景遮罩
    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_confirm = lv_win_create(bg_mask, NULL);
    lv_obj_set_size(win_confirm, 300, 140);
    lv_obj_set_style_local_text_font(win_confirm, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
    lv_obj_set_style_local_bg_opa(win_confirm, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_100);
    lv_obj_set_style_local_border_opa(win_confirm, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_100);
    lv_obj_set_style_local_bg_color(win_confirm, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
    lv_win_set_title(win_confirm, dlg_title);
    lv_win_set_btn_width(win_confirm, 30);
    lv_obj_align(win_confirm, NULL, LV_ALIGN_CENTER, 0, 0);
    
    // 创建提示信息标签
    label_message = lv_label_create(win_confirm, 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, dlg_message);
    lv_obj_align(label_message, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 20);
    
    // 创建确定按钮
    btn_ok = lv_btn_create(win_confirm, 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_confirm, 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 = CONFIRM_DIALOG_OP_OK;
    update_button_focus();
}

// 创建确认对话框组件（使用默认配置）
void ui_comp_confirm_dialog_create(void)
{
    ui_comp_confirm_dialog_create_internal(NULL, NULL);
}

// 创建确认对话框组件（使用自定义配置）
void ui_comp_confirm_dialog_create_with_config(const confirm_dialog_config_t *config)
{
    if (config != NULL) {
        ui_comp_confirm_dialog_create_internal(config->title, config->message);
    } else {
        ui_comp_confirm_dialog_create_internal(NULL, NULL);
    }
}


// 切换聚焦按钮
void ui_comp_confirm_dialog_switch_focus(void)
{
    current_focus = (current_focus == CONFIRM_DIALOG_OP_OK) ? 
                   CONFIRM_DIALOG_OP_CANCEL : CONFIRM_DIALOG_OP_OK;
    update_button_focus();
}

// 获取对话框结果
confirm_dialog_op_t ui_comp_confirm_dialog_get_result(void)
{
    return current_focus;
}
