import menu_page
import menu_utils as utils


def func_comm_test_leak_exam(object_device, topic_item, topic, message) :
    return None

def func_comm_table_trigger_item_exam(object_comm_table, comm_table_item) :
    return False


exam_menu_alias_table = [
    {"alias_bool": {0:"关闭", 1:"打开"}},
]

exam_mqtt_topic_table = [
    {"name": "test_exam", "execute": func_comm_test_leak_exam},
]

exam_measure_pack = [
    {
        "name": "OnStatus",
		"addr": "8162.0",
        "alias": "alias_onoff",
	},
]

exam_comm_table = [
    {"name":"measure",       "reg_addr":8139,   "reg_count":35,     "cycle": 200,  "mqtt_pack": "exam_measure_pack"},
]

exam_menu_top = [
    {
        "name": "退出",
        "action": "exit",
	},
]

exam_menu_caption = [
   {"name" : "exam_menu_top",  "menu" : exam_menu_top,  "next" : exam_menu_top, "prev" : exam_menu_top, "caption": "测试菜单", "page" : 10,  "max_items" : 8},
]

#通讯设备配置基类, 不同的modbus设备需要重载各个函数
class class_comm_device_config :
    def __init__(self):
        pass
    
    #获取别名表
    def get_alias_table(self) :
        return exam_menu_alias_table

    #获取通讯数据表, 及触发函数
    def get_comm_table(self) :
        return exam_comm_table, func_comm_table_trigger_item_exam

    #获取主菜单
    def get_menu_top(self) :
        return exam_menu_top

    #获取mqtt表， 实现mqtt通讯
    def get_mqtt_table(self) :
        return exam_mqtt_topic_table
    
    #获取昆仑通态显示页面
    def menu_get_display_page(self, active_menu):
        return menu_page.KUNLUN_GRAOUP_PAGE_MENU
    
    def get_menu_caption_info(self) :
        return exam_menu_caption
    
    def modify_menu_item(self, menu_item, value) :
        return False
    #获取菜单相关信息
    def search_menu_info(self, menu_name) :
        search_dict = None
        list_caption_info = self.get_menu_caption_info()
        for menu_dict in list_caption_info :
            if "name" in menu_dict.keys() :
                if menu_dict["name"] == menu_name :
                    search_dict = menu_dict
                    break 
        return search_dict

    #获取菜单相关信息
    def search_object_menu_info(self, menu_object) :
        search_dict = None
        list_caption_info = self.get_menu_caption_info()
        for menu_dict in list_caption_info :
            search_menu_object = utils.dict_or_object_get_attr(menu_dict, "menu", None)
            if search_menu_object == menu_object :
                search_dict = menu_dict
                break 
        return search_dict

    #获取自身菜单
    def get_menu(self, menu_name) :
        if menu_name != None :
            search_dict = self.search_menu_info(menu_name)
            if search_dict != None :
                return utils.dict_or_object_get_attr(search_dict, "menu", None)
        return None

    #获取自身菜单
    def get_menu_name(self, menu_object) :
        menu_name = None
        search_dict = self.search_object_menu_info(menu_object)
        if search_dict != None :
            menu_name = utils.dict_or_object_get_attr(search_dict, "name", None)
        return menu_name

    #获取下一个兄弟菜单
    def get_menu_next(self, menu_name) :
        search_dict = self.search_menu_info(menu_name)
        menu_name = utils.dict_or_object_get_attr(search_dict, "next", None)
        return self.get_menu(menu_name)

    #获取上一个兄弟菜单
    def get_menu_next(self, menu_name) :
        search_dict = self.search_menu_info(menu_name)
        menu_name = utils.dict_or_object_get_attr(search_dict, "prev", None)
        return self.get_menu(menu_name)
    
    #获取菜单显示页面
    def get_menu_page(self, menu_name) :
        search_dict = self.search_menu_info(menu_name)
        page = utils.dict_or_object_get_attr(search_dict, "page", menu_page.KUNLUN_GRAOUP_PAGE_MENU)
        return page