#include "ui_home_page.h"
#include "ui_common/ui_cont.h"
#include "data_config.h"
#include <string.h>

/* ====================================================================================================================
 * 文件名称: ui_home_page.c
 * 文件职责: 首页UI层实现 - 纯渲染逻辑，无业务判断，根据Service配置绘制UI
 * 架构位置: UI层 (View)
 * ==================================================================================================================== */

/* 控件定义 */
static lv_obj_t *s_cont = NULL;
static lv_obj_t *s_title_label = NULL;
static lv_obj_t *s_item_labels[HP_MAX_ITEMS_PER_GROUP] = {NULL};
static lv_obj_t *s_value_labels[HP_MAX_ITEMS_PER_GROUP] = {NULL};
static lv_obj_t *s_unit_labels[HP_MAX_ITEMS_PER_GROUP] = {NULL};
static lv_obj_t *s_img[4] = {NULL};

/* 布局常量 */
#define HP_TITLE_X          195
#define HP_TITLE_Y          55
#define HP_ITEM_START_X     190
#define HP_ITEM_START_Y     90
#define HP_ITEM_Y_GAP       35
#define HP_VALUE_X          280
#define HP_UNIT_X           450

/**
 * @brief  绘制背景区域
 */
static void hp_draw_background(void)
{
    Draw_counteara();
}

/**
 * @brief  创建图片资源
 */
static void hp_create_images(void)
{
    extern const char *imgpathlv[15];
    
    for (int i = 0; i < 4; i++)
    {
        s_img[i] = lv_img_create(s_cont, NULL);
        lv_img_set_src(s_img[i], imgpathlv[8 + i]);
    }

    lv_obj_set_pos(s_img[0], 20, 10);
    lv_obj_set_pos(s_img[1], 20, 18);
    lv_obj_set_pos(s_img[2], 85, 68);
    lv_obj_set_pos(s_img[3], 32, 243);
}

/**
 * @brief  创建标题标签
 */
static void hp_create_title(void)
{
    s_title_label = lv_label_create(s_cont, NULL);
    lv_obj_set_style_local_text_font(s_title_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk30);
    lv_obj_set_style_local_text_color(s_title_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, Default_HZ_color);
    lv_obj_set_pos(s_title_label, HP_TITLE_X, HP_TITLE_Y);
}

/**
 * @brief  创建数据项控件
 * @param  index     项索引
 * @param  item_cfg  项配置
 */
static void hp_create_item(uint8_t index, const HomePageItemConfig *item_cfg)
{
    if (index >= HP_MAX_ITEMS_PER_GROUP)
    {
        return;
    }

    int y_pos = HP_ITEM_START_Y + index * HP_ITEM_Y_GAP;

    /* 根据是否使用中文选择字体 - 设备信息组整组使用GBhzk22 */
    lv_font_t *label_font = item_cfg->use_chinese_font ? &GBhzk22 : &lv_font_montserrat_32;
    lv_font_t *value_font = item_cfg->use_chinese_font ? &GBhzk22 : &lv_font_montserrat_32;
    lv_font_t *unit_font = item_cfg->use_chinese_font ? &GBhzk22 : &lv_font_montserrat_32;

    /* 计算各控件的坐标，如果 item_cfg 中设置了自定义坐标则使用，否则使用默认值 */
    int16_t label_x = (item_cfg->label_x >= 0) ? item_cfg->label_x : HP_ITEM_START_X;
    int16_t label_y = (item_cfg->label_y >= 0) ? item_cfg->label_y : y_pos;
    int16_t value_x = (item_cfg->value_x >= 0) ? item_cfg->value_x : HP_VALUE_X;
    int16_t value_y = (item_cfg->value_y >= 0) ? item_cfg->value_y : y_pos;
    int16_t unit_x = (item_cfg->unit_x >= 0) ? item_cfg->unit_x : HP_UNIT_X;
    int16_t unit_y = (item_cfg->unit_y >= 0) ? item_cfg->unit_y : y_pos;
    printf("%s label_x: %d, label_y: %d, value_x: %d, value_y: %d, unit_x: %d, unit_y: %d\n", __FUNCTION__,
           label_x, label_y, value_x, value_y, unit_x, unit_y);
    /* 标签 */
    s_item_labels[index] = lv_label_create(s_cont, NULL);
    lv_obj_set_style_local_text_font(s_item_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, label_font);
    lv_obj_set_style_local_text_color(s_item_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, item_cfg->color);
    lv_obj_set_pos(s_item_labels[index], label_x, label_y);

    /* 值 */
    s_value_labels[index] = lv_label_create(s_cont, NULL);
    lv_obj_set_style_local_text_font(s_value_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, value_font);
    lv_obj_set_style_local_text_color(s_value_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, item_cfg->color);
    lv_obj_set_pos(s_value_labels[index], value_x, value_y);

    /* 单位 */
    s_unit_labels[index] = lv_label_create(s_cont, NULL);
    lv_obj_set_style_local_text_font(s_unit_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, unit_font);
    lv_obj_set_style_local_text_color(s_unit_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, item_cfg->color);
    lv_obj_set_pos(s_unit_labels[index], unit_x, unit_y);
}

/**
 * @brief  更新数据项显示
 * @param  index     项索引
 * @param  item_cfg  项配置
 */
static void hp_update_item(uint8_t index, const HomePageItemConfig *item_cfg)
{
    if (index >= HP_MAX_ITEMS_PER_GROUP)
    {
        return;
    }

    if (!item_cfg->visible)
    {
        /* 隐藏该项 */
        if (s_item_labels[index])
        {
            lv_label_set_text(s_item_labels[index], "");
        }
        if (s_value_labels[index])
        {
            lv_label_set_text(s_value_labels[index], "");
        }
        if (s_unit_labels[index])
        {
            lv_label_set_text(s_unit_labels[index], "");
        }
        return;
    }

    /* 根据是否使用中文选择字体 - 设备信息组整组使用GBhzk22 */
    lv_font_t *label_font = item_cfg->use_chinese_font ? &GBhzk22 : &lv_font_montserrat_32;
    lv_font_t *value_font = item_cfg->use_chinese_font ? &GBhzk22 : &lv_font_montserrat_32;
    lv_font_t *unit_font = item_cfg->use_chinese_font ? &GBhzk22 : &lv_font_montserrat_32;

    /* 计算Y坐标和自定义坐标 */
    int y_pos = HP_ITEM_START_Y + index * HP_ITEM_Y_GAP;
    int16_t label_x = (item_cfg->label_x >= 0) ? item_cfg->label_x : HP_ITEM_START_X;
    int16_t label_y = (item_cfg->label_y >= 0) ? item_cfg->label_y : y_pos;
    int16_t value_x = (item_cfg->value_x >= 0) ? item_cfg->value_x : HP_VALUE_X;
    int16_t value_y = (item_cfg->value_y >= 0) ? item_cfg->value_y : y_pos;
    int16_t unit_x = (item_cfg->unit_x >= 0) ? item_cfg->unit_x : HP_UNIT_X;
    int16_t unit_y = (item_cfg->unit_y >= 0) ? item_cfg->unit_y : y_pos;

    /* 更新标签 */
    if (s_item_labels[index])
    {
        lv_label_set_text(s_item_labels[index], item_cfg->label);
        lv_obj_set_style_local_text_color(s_item_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, item_cfg->color);
        lv_obj_set_style_local_text_font(s_item_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, label_font);
        lv_obj_set_pos(s_item_labels[index], label_x, label_y);
    }

    /* 更新值 */
    if (s_value_labels[index])
    {
        lv_label_set_text(s_value_labels[index], item_cfg->value);
        lv_obj_set_style_local_text_color(s_value_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, item_cfg->color);
        lv_obj_set_style_local_text_font(s_value_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, value_font);
        lv_obj_set_pos(s_value_labels[index], value_x, value_y);
    }

    /* 更新单位 */
    if (s_unit_labels[index])
    {
        lv_label_set_text(s_unit_labels[index], item_cfg->unit);
        lv_obj_set_style_local_text_color(s_unit_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, item_cfg->color);
        lv_obj_set_style_local_text_font(s_unit_labels[index], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, unit_font);
        lv_obj_set_pos(s_unit_labels[index], unit_x, unit_y);
    }
}

bool home_page_ui_create(const HomePageViewConfig *config)
{
    if (config == NULL)
    {
        return false;
    }
    
    /* 获取容器 */
    s_cont = ui_get_cont();
    
    /* 绘制背景 */
    hp_draw_background();
    
    /* 创建图片 */
    hp_create_images();
    
    /* 创建标题 */
    hp_create_title();
    if (s_title_label)
    {
        lv_label_set_text(s_title_label, config->group_title);
    }
    
    /* 创建数据项 */
    for (uint8_t i = 0; i < HP_MAX_ITEMS_PER_GROUP; i++)
    {
        hp_create_item(i, &config->items[i]);
        hp_update_item(i, &config->items[i]);
    }
    
    return true;
}

void home_page_ui_refresh(const HomePageViewConfig *config)
{
    if (config == NULL || s_cont == NULL)
    {
        return;
    }
    
    /* 更新标题 */
    if (s_title_label)
    {
        lv_label_set_text(s_title_label, config->group_title);
    }
    
    /* 更新数据项 */
    for (uint8_t i = 0; i < HP_MAX_ITEMS_PER_GROUP; i++)
    {
        hp_update_item(i, &config->items[i]);
    }
}

void home_page_ui_destroy(void)
{
    /* 清理容器内所有子控件 */
    if (s_cont != NULL)
    {
        lv_obj_clean(s_cont);
        s_cont = NULL;
    }
    
    s_title_label = NULL;
    for (int i = 0; i < HP_MAX_ITEMS_PER_GROUP; i++)
    {
        s_item_labels[i] = NULL;
        s_value_labels[i] = NULL;
        s_unit_labels[i] = NULL;
    }
    for (int i = 0; i < 4; i++)
    {
        s_img[i] = NULL;
    }
}

/* ====================================================================================================================
 * 兼容旧接口实现 - 内部调用新的MVVM架构
 * ==================================================================================================================== */

void ui_home_page_create(void)
{
    /* 通过Service层获取配置，然后创建UI */
    HomePageViewConfig config;
    if (home_page_service_get_config(HP_GROUP_MCB, &config))
    {
        home_page_ui_create(&config);
    }
}

void ui_home_page_select(int index)
{
    /* 将旧索引映射到新的组类型 */
    HomePageGroupType group = HP_GROUP_MCB;
    
    switch (index)
    {
    case 0:
        group = HP_GROUP_MCB;
        break;
    case 1:
        group = HP_GROUP_LCB;
        break;
    case 2:
        group = HP_GROUP_LOAD_I;
        break;
    case 3:
        group = HP_GROUP_ZERO_I;
        break;
    case 4:
        group = HP_GROUP_DEV_INFO;
        break;
    default:
        group = HP_GROUP_MCB;
        break;
    }
    
    /* 获取配置并刷新UI */
    HomePageViewConfig config;
    if (home_page_service_get_config(group, &config))
    {
        home_page_ui_refresh(&config);
    }
}
