#include "service_main_menu.h"
#include <string.h>

static bool s_initialized = false;

static const char *s_menu_names[MAIN_MENU_NUM] = {
    [MAIN_MENU_DEV_SET]      = "装置参数",
    [MAIN_MENU_DEV_FIX]      = "定值设置",
    [MAIN_MENU_DEV_SET_SHOW] = "装置参数查看",
    [MAIN_MENU_DEV_FIX_SHOW] = "定值查看",
    [MAIN_MENU_REAL_TIME_DATA] = "实时数据",
    [MAIN_MENU_HISTORY_DATA] = "历史数据",
    [MAIN_MENU_DEBUG_TEST]   = "出厂调试",
    [MAIN_MENU_DEV_INFO] = "版本信息",
};

static MainMenuItemConfig s_menu_items[MAIN_MENU_NUM];

void main_menu_service_init(void)
{
    if (s_initialized) {
        return;
    }

    uint8_t i;

    for (i = 0; i < MAIN_MENU_NUM; i++) {
        s_menu_items[i].name = s_menu_names[i];
        s_menu_items[i].icon_path = NULL;
        s_menu_items[i].type = (MainMenuType)i;

        if (i == MAIN_MENU_DEV_SET || i == MAIN_MENU_DEV_FIX) {
            s_menu_items[i].need_password = true;
        } else {
            s_menu_items[i].need_password = false;
        }
    }

    s_initialized = true;
}

const char* main_menu_service_get_type_name(MainMenuType type)
{
    if (type >= MAIN_MENU_NUM) {
        return NULL;
    }
    return s_menu_names[type];
}

const MainMenuItemConfig* main_menu_service_get_item(uint8_t idx)
{
    if (idx >= MAIN_MENU_NUM) {
        return NULL;
    }
    return &s_menu_items[idx];
}

uint8_t main_menu_service_get_item_count(void)
{
    return MAIN_MENU_NUM;
}

bool main_menu_service_need_password(uint8_t idx)
{
    const MainMenuItemConfig *item = main_menu_service_get_item(idx);
    if (item == NULL) {
        return false;
    }
    return item->need_password;
}

void main_menu_service_get_page_config(MainMenuPageConfig *config, uint8_t selected_idx)
{
    if (config == NULL) {
        return;
    }

    memcpy(config->items, s_menu_items, sizeof(s_menu_items));
    config->selected_idx = selected_idx;
}
