#include "ui_main_menu.h"
#include "ui_common/ui_cont.h"
#include "ui_common/ui_img.h"
#include <string.h>

static UIMainMenuType s_cur_page_type = UI_MAIN_MENU_DEV_SET;
static lv_obj_t *s_labels[UI_MAIN_MENU_NUM] = {NULL};
static lv_obj_t *s_cont = NULL;

extern const char *imgpathlv[];

static void create_menu_items(const UIMainMenuConfig *config);
static void update_selected_style(uint8_t selected_idx);

void ui_main_menu_create(const UIMainMenuConfig *config)
{
    if (config == NULL) {
        return;
    }

    uint8_t i;

    s_cont = ui_get_cont();
    if (s_cont == NULL) {
        return;
    }

    Draw_counteara();

    for (i = 0; i < 4; i++) {
        ui_img_create(s_cont, imgpathlv[i], 28 + i * 120, 40);
        ui_img_create(s_cont, imgpathlv[i + 4], 28 + i * 120, 40 + 125);
    }

    create_menu_items(config);

    update_selected_style(config->selected_idx);
}

void ui_main_menu_refresh(uint8_t selected_idx)
{
    update_selected_style(selected_idx);
}

void ui_main_menu_destroy(void)
{
    if (s_cont != NULL) {
        lv_obj_clean(s_cont);
        s_cont = NULL;
    }

    uint8_t i;
    for (i = 0; i < UI_MAIN_MENU_NUM; i++) {
        s_labels[i] = NULL;
    }
}

void ui_main_menu_record_type(UIMainMenuType type)
{
    s_cur_page_type = type;
}

UIMainMenuType ui_main_menu_get_type(void)
{
    return s_cur_page_type;
}

void ui_main_menu_config_convert(UIMainMenuConfig *ui_config, const void *service_config)
{
    if (ui_config == NULL || service_config == NULL) {
        return;
    }

    (void)service_config;
    memset(ui_config, 0, sizeof(UIMainMenuConfig));
}

static void create_menu_items(const UIMainMenuConfig *config)
{
    uint8_t i;

    for (i = 0; i < 4; i++) {
        s_labels[i] = lv_label_create(s_cont, NULL);
        lv_obj_set_style_local_text_font(s_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(s_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, Default_HZ_color);
        lv_label_set_text(s_labels[i], config->items[i].name);

        if (i == 2) {
            lv_obj_set_pos(s_labels[i], 18 + 220, 43 + 75);
        } else {
            lv_obj_set_pos(s_labels[i], 18 + i * 120, 43 + 75);
        }

        s_labels[i + 4] = lv_label_create(s_cont, NULL);
        lv_obj_set_style_local_text_font(s_labels[i + 4], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &GBhzk22);
        lv_obj_set_style_local_text_color(s_labels[i + 4], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, Default_HZ_color);
        lv_label_set_text(s_labels[i + 4], config->items[i + 4].name);
        lv_obj_set_pos(s_labels[i + 4], 18 + i * 120, 43 + 195);
    }
}

static void update_selected_style(uint8_t selected_idx)
{
    uint8_t i;

    for (i = 0; i < UI_MAIN_MENU_NUM; i++) {
        if (s_labels[i] == NULL) {
            continue;
        }

        if (i == selected_idx) {
            lv_obj_set_style_local_text_color(s_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
        } else {
            lv_obj_set_style_local_text_color(s_labels[i], LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, Default_HZ_color);
        }
    }
}
